MediaWiki master
benchmarkPurge.php
Go to the documentation of this file.
1<?php
12
13// @codeCoverageIgnoreStart
14require_once __DIR__ . '/../includes/Benchmarker.php';
15// @codeCoverageIgnoreEnd
16
23 public function __construct() {
24 parent::__construct();
25 $this->addDescription( 'Benchmark the CDN purge functions.' );
26 }
27
28 public function execute() {
30
31 if ( !$wgUseCdn ) {
32 $this->error( "CDN purge benchmark doesn't do much without CDN support on." );
33 } else {
34 $this->output( "There are " . count( $wgCdnServers ) . " defined CDN servers:\n" );
35 if ( $this->hasOption( 'count' ) ) {
36 $lengths = [ intval( $this->getOption( 'count' ) ) ];
37 } else {
38 $lengths = [ 1, 10, 100 ];
39 }
40 foreach ( $lengths as $length ) {
41 $urls = $this->randomUrlList( $length );
42 $trial = $this->benchCdn( $urls );
43 $this->output( $trial . "\n" );
44 }
45 }
46 }
47
55 private function benchCdn( $urls, $trials = 1 ) {
56 $start = microtime( true );
57 for ( $i = 0; $i < $trials; $i++ ) {
58 CdnCacheUpdate::purge( $urls );
59 }
60 $delta = microtime( true ) - $start;
61 $pertrial = $delta / $trials;
62 $pertitle = $pertrial / count( $urls );
63
64 return sprintf( "%4d titles in %6.2fms (%6.2fms each)",
65 count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 );
66 }
67
73 private function randomUrlList( $length ) {
74 $list = [];
75 for ( $i = 0; $i < $length; $i++ ) {
76 $list[] = $this->randomUrl();
77 }
78
79 return $list;
80 }
81
87 private function randomUrl() {
89
90 return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
91 }
92
98 private function randomTitle() {
99 $str = '';
100 $length = mt_rand( 1, 20 );
101 for ( $i = 0; $i < $length; $i++ ) {
102 $str .= chr( mt_rand( ord( 'a' ), ord( 'z' ) ) );
103 }
104
105 return ucfirst( $str );
106 }
107}
108
109// @codeCoverageIgnoreStart
110$maintClass = BenchmarkPurge::class;
111require_once RUN_MAINTENANCE_IF_MAIN;
112// @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.