MediaWiki  master
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++ ) {
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;
Maintenance script that benchmarks CDN purge.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
static purge(array $urlArr)
Purges a list of CDN nodes defined in $wgCdnServers.
error( $err, $die=0)
Throw an error to the user.
getOption( $name, $default=null)
Get an option, or return the default.
benchCdn( $urls, $trials=1)
Run a bunch of URLs through CdnCacheUpdate::purge() to benchmark CDN response times.
hasOption( $name)
Checks to see if a particular option exists.
$wgCdnServers
List of proxy servers to purge on changes; default port is 80.
$wgArticlePath
Definition: img_auth.php:47
randomUrlList( $length)
Get an array of randomUrl()&#39;s.
addDescription( $text)
Set the description text.
output( $out, $channel=null)
Throw some output to the user.
randomUrl()
Return a random URL of the wiki.
Base class for benchmark scripts.
Definition: Benchmarker.php:40
$maintClass
randomTitle()
Create a random title string (not necessarily a Title object).
$wgServer
URL of the server.
$wgUseCdn
Enable/disable CDN.