MediaWiki  1.34.0
benchmarkPurge.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Benchmarker.php';
25 
31 class BenchmarkPurge extends Benchmarker {
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Benchmark the CDN purge functions.' );
35  }
36 
37  public function execute() {
38  global $wgUseCdn, $wgCdnServers;
39 
40  if ( !$wgUseCdn ) {
41  $this->error( "CDN purge benchmark doesn't do much without CDN support on." );
42  } else {
43  $this->output( "There are " . count( $wgCdnServers ) . " defined CDN servers:\n" );
44  if ( $this->hasOption( 'count' ) ) {
45  $lengths = [ intval( $this->getOption( 'count' ) ) ];
46  } else {
47  $lengths = [ 1, 10, 100 ];
48  }
49  foreach ( $lengths as $length ) {
50  $urls = $this->randomUrlList( $length );
51  $trial = $this->benchCdn( $urls );
52  $this->output( $trial . "\n" );
53  }
54  }
55  }
56 
64  private function benchCdn( $urls, $trials = 1 ) {
65  $start = microtime( true );
66  for ( $i = 0; $i < $trials; $i++ ) {
67  CdnCacheUpdate::purge( $urls );
68  }
69  $delta = microtime( true ) - $start;
70  $pertrial = $delta / $trials;
71  $pertitle = $pertrial / count( $urls );
72 
73  return sprintf( "%4d titles in %6.2fms (%6.2fms each)",
74  count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 );
75  }
76 
82  private function randomUrlList( $length ) {
83  $list = [];
84  for ( $i = 0; $i < $length; $i++ ) {
85  $list[] = $this->randomUrl();
86  }
87 
88  return $list;
89  }
90 
96  private function randomUrl() {
97  global $wgServer, $wgArticlePath;
98 
99  return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
100  }
101 
107  private function randomTitle() {
108  $str = '';
109  $length = mt_rand( 1, 20 );
110  for ( $i = 0; $i < $length; $i++ ) {
111  $str .= chr( mt_rand( ord( 'a' ), ord( 'z' ) ) );
112  }
113 
114  return ucfirst( $str );
115  }
116 }
117 
118 $maintClass = BenchmarkPurge::class;
119 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
$wgUseCdn
$wgUseCdn
Enable/disable CDN.
Definition: DefaultSettings.php:2751
CdnCacheUpdate\purge
static purge(array $urlArr)
Purges a list of CDN nodes defined in $wgCdnServers.
Definition: CdnCacheUpdate.php:89
$wgCdnServers
$wgCdnServers
List of proxy servers to purge on changes; default port is 80.
Definition: DefaultSettings.php:2832
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
BenchmarkPurge\__construct
__construct()
Default constructor.
Definition: benchmarkPurge.php:32
BenchmarkPurge\randomUrl
randomUrl()
Return a random URL of the wiki.
Definition: benchmarkPurge.php:96
BenchmarkPurge\execute
execute()
Do the actual work.
Definition: benchmarkPurge.php:37
BenchmarkPurge
Maintenance script that benchmarks CDN purge.
Definition: benchmarkPurge.php:31
BenchmarkPurge\benchCdn
benchCdn( $urls, $trials=1)
Run a bunch of URLs through CdnCacheUpdate::purge() to benchmark CDN response times.
Definition: benchmarkPurge.php:64
$wgServer
$wgServer
URL of the server.
Definition: DefaultSettings.php:105
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
$wgArticlePath
$wgArticlePath
Definition: img_auth.php:47
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
BenchmarkPurge\randomTitle
randomTitle()
Create a random title string (not necessarily a Title object).
Definition: benchmarkPurge.php:107
$maintClass
$maintClass
Definition: benchmarkPurge.php:118
BenchmarkPurge\randomUrlList
randomUrlList( $length)
Get an array of randomUrl()'s.
Definition: benchmarkPurge.php:82