]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/benchmarkPurge.php
MediaWiki 1.15.1
[autoinstallsdev/mediawiki.git] / maintenance / benchmarkPurge.php
1 <?php
2 /**
3  * Squid purge benchmark script
4  *
5  * @file
6  * @ingroup Maintenance
7  */
8
9 /** */
10 require_once( "commandLine.inc" );
11
12 /** 
13  * Run a bunch of URLs through SquidUpdate::purge()
14  * to benchmark Squid response times.
15  * @param $urls array A bunch of URLs to purge
16  * @param $trials int How many times to run the test?
17  */
18 function benchSquid( $urls, $trials = 1 ) {
19         $start = wfTime();
20         for( $i = 0; $i < $trials; $i++) {
21                 SquidUpdate::purge( $urls );
22         }
23         $delta = wfTime() - $start;
24         $pertrial = $delta / $trials;
25         $pertitle = $pertrial / count( $urls );
26         return sprintf( "%4d titles in %6.2fms (%6.2fms each)",
27                 count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 );
28 }
29
30 /** 
31  * Get an array of randomUrl()'s.
32  * @param $length int How many urls to add to the array
33  */
34 function randomUrlList( $length ) {
35         $list = array();
36         for( $i = 0; $i < $length; $i++ ) {
37                 $list[] = randomUrl();
38         }
39         return $list;
40 }
41
42 /** 
43  * Return a random URL of the wiki. Not necessarily an actual title in the
44  * database, but at least a URL that looks like one. 
45  */
46 function randomUrl() {
47         global $wgServer, $wgArticlePath;
48         return $wgServer . str_replace( '$1', randomTitle(), $wgArticlePath );
49 }
50
51 /** 
52  * Create a random title string (not necessarily a Title object). 
53  * For use with randomUrl().
54  */
55 function randomTitle() {
56         $str = '';
57         $length = mt_rand( 1, 20 );
58         for( $i = 0; $i < $length; $i++ ) {
59                 $str .= chr( mt_rand( ord('a'), ord('z') ) );
60         }
61         return ucfirst( $str );
62 }
63
64 if( !$wgUseSquid ) {
65         wfDie( "Squid purge benchmark doesn't do much without squid support on.\n" );
66 } else {
67         printf( "There are %d defined squid servers:\n", count( $wgSquidServers ) );
68         #echo implode( "\n", $wgSquidServers ) . "\n";
69         if( isset( $options['count'] ) ) {
70                 $lengths = array( intval( $options['count'] ) );
71         } else {
72                 $lengths = array( 1, 10, 100 );
73         }
74         foreach( $lengths as $length ) {
75                 $urls = randomUrlList( $length );
76                 $trial = benchSquid( $urls );
77                 print "$trial\n";
78         }
79 }