MediaWiki master
benchmarkPurge.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/../includes/Benchmarker.php';
29// @codeCoverageIgnoreEnd
30
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Benchmark the CDN purge functions.' );
40 }
41
42 public function execute() {
44
45 if ( !$wgUseCdn ) {
46 $this->error( "CDN purge benchmark doesn't do much without CDN support on." );
47 } else {
48 $this->output( "There are " . count( $wgCdnServers ) . " defined CDN servers:\n" );
49 if ( $this->hasOption( 'count' ) ) {
50 $lengths = [ intval( $this->getOption( 'count' ) ) ];
51 } else {
52 $lengths = [ 1, 10, 100 ];
53 }
54 foreach ( $lengths as $length ) {
55 $urls = $this->randomUrlList( $length );
56 $trial = $this->benchCdn( $urls );
57 $this->output( $trial . "\n" );
58 }
59 }
60 }
61
69 private function benchCdn( $urls, $trials = 1 ) {
70 $start = microtime( true );
71 for ( $i = 0; $i < $trials; $i++ ) {
72 CdnCacheUpdate::purge( $urls );
73 }
74 $delta = microtime( true ) - $start;
75 $pertrial = $delta / $trials;
76 $pertitle = $pertrial / count( $urls );
77
78 return sprintf( "%4d titles in %6.2fms (%6.2fms each)",
79 count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 );
80 }
81
87 private function randomUrlList( $length ) {
88 $list = [];
89 for ( $i = 0; $i < $length; $i++ ) {
90 $list[] = $this->randomUrl();
91 }
92
93 return $list;
94 }
95
101 private function randomUrl() {
103
104 return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
105 }
106
112 private function randomTitle() {
113 $str = '';
114 $length = mt_rand( 1, 20 );
115 for ( $i = 0; $i < $length; $i++ ) {
116 $str .= chr( mt_rand( ord( 'a' ), ord( 'z' ) ) );
117 }
118
119 return ucfirst( $str );
120 }
121}
122
123// @codeCoverageIgnoreStart
124$maintClass = BenchmarkPurge::class;
125require_once RUN_MAINTENANCE_IF_MAIN;
126// @codeCoverageIgnoreEnd
$maintClass
Maintenance script that benchmarks CDN purge.
__construct()
Default constructor.
execute()
Do the actual work.
Handles purging the appropriate CDN objects given a list of URLs or Title instances.
Base class for benchmark scripts.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
addDescription( $text)
Set the description text.
$wgUseCdn
Config variable stub for the UseCdn setting, for use by phpdoc and IDEs.
$wgArticlePath
Config variable stub for the ArticlePath setting, for use by phpdoc and IDEs.
$wgServer
Config variable stub for the Server setting, for use by phpdoc and IDEs.
$wgCdnServers
Config variable stub for the CdnServers setting, for use by phpdoc and IDEs.