Debian Codegraph @

maintenance/benchmarks/benchmarkPurge.php

raw symbols SCIP
1 <?php
2 /**
3 * Benchmark for CDN purge.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Benchmark
22 */
23
24 // @codeCoverageIgnoreStart
25 require_once __DIR__ . '/../includes/Benchmarker.php';
26 // @codeCoverageIgnoreEnd
27
28 use MediaWiki\Deferred\CdnCacheUpdate;
29
30 /**
31 * Maintenance script that benchmarks CDN purge.
32 *
33 * @ingroup Benchmark
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 * 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
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 * Get an array of randomUrl()'s.
83 * @param int $length How many urls to add to the array
84 * @return array
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 * 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
99 */
100 private function randomUrl() {
101 global $wgServer, $wgArticlePath;
102
103 return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
104 }
105
106 /**
107 * Create a random title string (not necessarily a Title object).
108 * For use with randomUrl().
109 * @return string
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 // @codeCoverageIgnoreStart
123 $maintClass = BenchmarkPurge::class;
124 require_once RUN_MAINTENANCE_IF_MAIN;
125 // @codeCoverageIgnoreEnd