| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/../includes/Benchmarker.php'; |
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\Deferred\CdnCacheUpdate; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class BenchmarkPurge extends Benchmarker { |
| 36 |
public function __construct() { |
| 37 |
parent::__construct(); |
| 38 |
$this->addDescription( 'Benchmark the CDN purge functions.' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function execute() { |
| 42 |
global $wgUseCdn, $wgCdnServers; |
| 43 |
|
| 44 |
if ( !$wgUseCdn ) { |
| 45 |
$this->error( "CDN purge benchmark doesn't do much without CDN support on." ); |
| 46 |
} else { |
| 47 |
$this->output( "There are " . count( $wgCdnServers ) . " defined CDN servers:\n" ); |
| 48 |
if ( $this->hasOption( 'count' ) ) { |
| 49 |
$lengths = [ intval( $this->getOption( 'count' ) ) ]; |
| 50 |
} else { |
| 51 |
$lengths = [ 1, 10, 100 ]; |
| 52 |
} |
| 53 |
foreach ( $lengths as $length ) { |
| 54 |
$urls = $this->randomUrlList( $length ); |
| 55 |
$trial = $this->benchCdn( $urls ); |
| 56 |
$this->output( $trial . "\n" ); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private function benchCdn( $urls, $trials = 1 ) { |
| 69 |
$start = microtime( true ); |
| 70 |
for ( $i = 0; $i < $trials; $i++ ) { |
| 71 |
CdnCacheUpdate::purge( $urls ); |
| 72 |
} |
| 73 |
$delta = microtime( true ) - $start; |
| 74 |
$pertrial = $delta / $trials; |
| 75 |
$pertitle = $pertrial / count( $urls ); |
| 76 |
|
| 77 |
return sprintf( "%4d titles in %6.2fms (%6.2fms each)", |
| 78 |
count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private function randomUrlList( $length ) { |
| 87 |
$list = []; |
| 88 |
for ( $i = 0; $i < $length; $i++ ) { |
| 89 |
$list[] = $this->randomUrl(); |
| 90 |
} |
| 91 |
|
| 92 |
return $list; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private function randomUrl() { |
| 101 |
global $wgServer, $wgArticlePath; |
| 102 |
|
| 103 |
return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private function randomTitle() { |
| 112 |
$str = ''; |
| 113 |
$length = mt_rand( 1, 20 ); |
| 114 |
for ( $i = 0; $i < $length; $i++ ) { |
| 115 |
$str .= chr( mt_rand( ord( 'a' ), ord( 'z' ) ) ); |
| 116 |
} |
| 117 |
|
| 118 |
return ucfirst( $str ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
$maintClass = BenchmarkPurge::class; |
| 124 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 125 |
|