BenchmarkPurge::randomUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Benchmark for Squid 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 Squid purge.
28
 *
29
 * @ingroup Benchmark
30
 */
31
class BenchmarkPurge extends Benchmarker {
32
	public function __construct() {
33
		parent::__construct();
34
		$this->addDescription( 'Benchmark the Squid purge functions.' );
35
	}
36
37
	public function execute() {
38
		global $wgUseSquid, $wgSquidServers;
39
		if ( !$wgUseSquid ) {
40
			$this->error( "Squid purge benchmark doesn't do much without squid support on.", true );
41
		} else {
42
			$this->output( "There are " . count( $wgSquidServers ) . " defined squid servers:\n" );
43
			if ( $this->hasOption( 'count' ) ) {
44
				$lengths = [ intval( $this->getOption( 'count' ) ) ];
45
			} else {
46
				$lengths = [ 1, 10, 100 ];
47
			}
48
			foreach ( $lengths as $length ) {
49
				$urls = $this->randomUrlList( $length );
50
				$trial = $this->benchSquid( $urls );
51
				$this->output( $trial . "\n" );
52
			}
53
		}
54
	}
55
56
	/**
57
	 * Run a bunch of URLs through SquidUpdate::purge()
58
	 * to benchmark Squid response times.
59
	 * @param array $urls A bunch of URLs to purge
60
	 * @param int $trials How many times to run the test?
61
	 * @return string
62
	 */
63
	private function benchSquid( $urls, $trials = 1 ) {
64
		$start = microtime( true );
65
		for ( $i = 0; $i < $trials; $i++ ) {
66
			CdnCacheUpdate::purge( $urls );
67
		}
68
		$delta = microtime( true ) - $start;
69
		$pertrial = $delta / $trials;
70
		$pertitle = $pertrial / count( $urls );
71
72
		return sprintf( "%4d titles in %6.2fms (%6.2fms each)",
73
			count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 );
74
	}
75
76
	/**
77
	 * Get an array of randomUrl()'s.
78
	 * @param int $length How many urls to add to the array
79
	 * @return array
80
	 */
81
	private function randomUrlList( $length ) {
82
		$list = [];
83
		for ( $i = 0; $i < $length; $i++ ) {
84
			$list[] = $this->randomUrl();
85
		}
86
87
		return $list;
88
	}
89
90
	/**
91
	 * Return a random URL of the wiki. Not necessarily an actual title in the
92
	 * database, but at least a URL that looks like one.
93
	 * @return string
94
	 */
95
	private function randomUrl() {
96
		global $wgServer, $wgArticlePath;
97
98
		return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
99
	}
100
101
	/**
102
	 * Create a random title string (not necessarily a Title object).
103
	 * For use with randomUrl().
104
	 * @return string
105
	 */
106
	private function randomTitle() {
107
		$str = '';
108
		$length = mt_rand( 1, 20 );
109
		for ( $i = 0; $i < $length; $i++ ) {
110
			$str .= chr( mt_rand( ord( 'a' ), ord( 'z' ) ) );
111
		}
112
113
		return ucfirst( $str );
114
	}
115
}
116
117
$maintClass = "BenchmarkPurge";
118
require_once RUN_MAINTENANCE_IF_MAIN;
119