00001 <?php
00023 require_once( dirname(__FILE__) . '/Maintenance.php' );
00024
00025 class BenchmarkPurge extends Maintenance {
00026
00027 public function __construct() {
00028 parent::__construct();
00029 $this->addOption( "count", "How many URLs to feed to Squid for purging", false, true );
00030 $this->mDescription = "Benchmark the Squid purge functions.";
00031 }
00032
00033 public function execute() {
00034 global $wgUseSquid;
00035 if( !$wgUseSquid ) {
00036 $this->error( "Squid purge benchmark doesn't do much without squid support on.". true );
00037 } else {
00038 $this->output( "There are " . count( $wgSquidServers ) . " defined squid servers:\n" );
00039 if( $this->hasOption( 'count' ) ) {
00040 $lengths = array( intval( $this->getOption('count') ) );
00041 } else {
00042 $lengths = array( 1, 10, 100 );
00043 }
00044 foreach( $lengths as $length ) {
00045 $urls = $this->randomUrlList( $length );
00046 $trial = $this->benchSquid( $urls );
00047 $this->output( $trial . "\n" );
00048 }
00049 }
00050 }
00051
00058 private function benchSquid( $urls, $trials = 1 ) {
00059 $start = wfTime();
00060 for( $i = 0; $i < $trials; $i++) {
00061 SquidUpdate::purge( $urls );
00062 }
00063 $delta = wfTime() - $start;
00064 $pertrial = $delta / $trials;
00065 $pertitle = $pertrial / count( $urls );
00066 return sprintf( "%4d titles in %6.2fms (%6.2fms each)",
00067 count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 );
00068 }
00069
00074 private function randomUrlList( $length ) {
00075 $list = array();
00076 for( $i = 0; $i < $length; $i++ ) {
00077 $list[] = $this->randomUrl();
00078 }
00079 return $list;
00080 }
00081
00086 private function randomUrl() {
00087 global $wgServer, $wgArticlePath;
00088 return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
00089 }
00090
00095 private function randomTitle() {
00096 $str = '';
00097 $length = mt_rand( 1, 20 );
00098 for( $i = 0; $i < $length; $i++ ) {
00099 $str .= chr( mt_rand( ord('a'), ord('z') ) );
00100 }
00101 return ucfirst( $str );
00102 }
00103 }
00104
00105 $maintClass = "BenchmarkPurge";
00106 require_once( DO_MAINTENANCE );