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