Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / maintenance / benchmarks / benchmarkPurge.php
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 require_once __DIR__ . '/Benchmarker.php';
25
26 /**
27 * Maintenance script that benchmarks CDN purge.
28 *
29 * @ingroup Benchmark
30 */
31 class BenchmarkPurge extends Benchmarker {
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( 'Benchmark the CDN purge functions.' );
35 }
36
37 public function execute() {
38 global $wgUseCdn, $wgCdnServers;
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
57 /**
58 * Run a bunch of URLs through CdnCacheUpdate::purge()
59 * to benchmark CDN response times.
60 * @param array $urls A bunch of URLs to purge
61 * @param int $trials How many times to run the test?
62 * @return string
63 */
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
77 /**
78 * Get an array of randomUrl()'s.
79 * @param int $length How many urls to add to the array
80 * @return array
81 */
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
91 /**
92 * Return a random URL of the wiki. Not necessarily an actual title in the
93 * database, but at least a URL that looks like one.
94 * @return string
95 */
96 private function randomUrl() {
97 global $wgServer, $wgArticlePath;
98
99 return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
100 }
101
102 /**
103 * Create a random title string (not necessarily a Title object).
104 * For use with randomUrl().
105 * @return string
106 */
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;
119 require_once RUN_MAINTENANCE_IF_MAIN;