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