MediaWiki REL1_40
benchmarkPurge.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/../includes/Benchmarker.php';
25
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( 'Benchmark the CDN purge functions.' );
35 }
36
37 public function execute() {
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() {
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;
119require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
Maintenance script that benchmarks CDN purge.
__construct()
Default constructor.
execute()
Do the actual work.
Base class for benchmark scripts.
static purge(array $urls)
Purges a list of CDN nodes defined in $wgCdnServers.
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
getOption( $name, $default=null)
Get an option, or return the default.
$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.