Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / benchmarks / benchmarkPurge.php
blob348a4b3bb18c52e799fdf588b85a8a1e1fddbdbd
1 <?php
2 /**
3 * Benchmark for CDN purge.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Benchmark
24 // @codeCoverageIgnoreStart
25 require_once __DIR__ . '/../includes/Benchmarker.php';
26 // @codeCoverageIgnoreEnd
28 use MediaWiki\Deferred\CdnCacheUpdate;
30 /**
31 * Maintenance script that benchmarks CDN purge.
33 * @ingroup Benchmark
35 class BenchmarkPurge extends Benchmarker {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Benchmark the CDN purge functions.' );
41 public function execute() {
42 global $wgUseCdn, $wgCdnServers;
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 ];
53 foreach ( $lengths as $length ) {
54 $urls = $this->randomUrlList( $length );
55 $trial = $this->benchCdn( $urls );
56 $this->output( $trial . "\n" );
61 /**
62 * Run a bunch of URLs through CdnCacheUpdate::purge()
63 * to benchmark CDN response times.
64 * @param array $urls A bunch of URLs to purge
65 * @param int $trials How many times to run the test?
66 * @return string
68 private function benchCdn( $urls, $trials = 1 ) {
69 $start = microtime( true );
70 for ( $i = 0; $i < $trials; $i++ ) {
71 CdnCacheUpdate::purge( $urls );
73 $delta = microtime( true ) - $start;
74 $pertrial = $delta / $trials;
75 $pertitle = $pertrial / count( $urls );
77 return sprintf( "%4d titles in %6.2fms (%6.2fms each)",
78 count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 );
81 /**
82 * Get an array of randomUrl()'s.
83 * @param int $length How many urls to add to the array
84 * @return array
86 private function randomUrlList( $length ) {
87 $list = [];
88 for ( $i = 0; $i < $length; $i++ ) {
89 $list[] = $this->randomUrl();
92 return $list;
95 /**
96 * Return a random URL of the wiki. Not necessarily an actual title in the
97 * database, but at least a URL that looks like one.
98 * @return string
100 private function randomUrl() {
101 global $wgServer, $wgArticlePath;
103 return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
107 * Create a random title string (not necessarily a Title object).
108 * For use with randomUrl().
109 * @return string
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' ) ) );
118 return ucfirst( $str );
122 // @codeCoverageIgnoreStart
123 $maintClass = BenchmarkPurge::class;
124 require_once RUN_MAINTENANCE_IF_MAIN;
125 // @codeCoverageIgnoreEnd