MediaWiki  master
benchmarkLruHash.php
Go to the documentation of this file.
1 <?php
22 require_once __DIR__ . '/../includes/Benchmarker.php';
23 
30  protected $defaultCount = 1000;
31 
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Benchmarks HashBagOStuff and MapCacheLRU.' );
35  $this->addOption( 'method', 'One of "construct" or "set". Default: [All]', false, true );
36  }
37 
38  public function execute() {
39  $exampleKeys = [];
40  $max = 100;
41  $count = 500;
42  while ( $count-- ) {
43  $exampleKeys[] = wfRandomString();
44  }
45  // 1000 keys (1...500, 500...1)
46  $keys = array_merge( $exampleKeys, array_reverse( $exampleKeys ) );
47 
48  $method = $this->getOption( 'method' );
49  $benches = [];
50 
51  if ( !$method || $method === 'construct' ) {
52  $benches['HashBagOStuff::__construct'] = [
53  'function' => static function () use ( $max ) {
54  $obj = new HashBagOStuff( [ 'maxKeys' => $max ] );
55  },
56  ];
57  $benches['MapCacheLRU::__construct'] = [
58  'function' => static function () use ( $max ) {
59  $obj = new MapCacheLRU( $max );
60  },
61  ];
62  }
63 
64  if ( !$method || $method === 'set' ) {
65  $hObj = null;
66  $benches['HashBagOStuff::set'] = [
67  'setup' => static function () use ( &$hObj, $max ) {
68  $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
69  },
70  'function' => static function () use ( &$hObj, $keys ) {
71  foreach ( $keys as $i => $key ) {
72  $hObj->set( $key, $i );
73  }
74  }
75  ];
76  $mObj = null;
77  $benches['MapCacheLRU::set'] = [
78  'setup' => static function () use ( &$mObj, $max ) {
79  $mObj = new MapCacheLRU( $max );
80  },
81  'function' => static function () use ( &$mObj, $keys ) {
82  foreach ( $keys as $i => $key ) {
83  $mObj->set( $key, $i );
84  }
85  }
86  ];
87  }
88 
89  if ( !$method || $method === 'get' ) {
90  $hObj = null;
91  $benches['HashBagOStuff::get'] = [
92  'setup' => static function () use ( &$hObj, $max, $keys ) {
93  $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
94  foreach ( $keys as $i => $key ) {
95  $hObj->set( $key, $i );
96  }
97  },
98  'function' => static function () use ( &$hObj, $keys ) {
99  foreach ( $keys as $key ) {
100  $hObj->get( $key );
101  }
102  }
103  ];
104  $mObj = null;
105  $benches['MapCacheLRU::get'] = [
106  'setup' => static function () use ( &$mObj, $max, $keys ) {
107  $mObj = new MapCacheLRU( $max );
108  foreach ( $keys as $i => $key ) {
109  $mObj->set( $key, $i );
110  }
111  },
112  'function' => static function () use ( &$mObj, $keys ) {
113  foreach ( $keys as $key ) {
114  $mObj->get( $key );
115  }
116  }
117  ];
118  }
119 
120  $this->bench( $benches );
121  }
122 }
123 
124 $maintClass = BenchmarkLruHash::class;
125 require_once RUN_MAINTENANCE_IF_MAIN;
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:32
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
Benchmarker\bench
bench(array $benchs)
Definition: Benchmarker.php:49
BenchmarkLruHash\$defaultCount
$defaultCount
Definition: benchmarkLruHash.php:30
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
MapCacheLRU
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:36
BenchmarkLruHash\__construct
__construct()
Default constructor.
Definition: benchmarkLruHash.php:32
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
$keys
$keys
Definition: testCompression.php:72
$maintClass
$maintClass
Definition: benchmarkLruHash.php:124
BenchmarkLruHash
Maintenance script that benchmarks HashBagOStuff and MapCacheLRU.
Definition: benchmarkLruHash.php:29
BenchmarkLruHash\execute
execute()
Do the actual work.
Definition: benchmarkLruHash.php:38
wfRandomString
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Definition: GlobalFunctions.php:257