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