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