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  '@phan-var BagOStuff $hObj';
67  $benches['HashBagOStuff::set'] = [
68  'setup' => static function () use ( &$hObj, $max ) {
69  $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
70  },
71  'function' => static function () use ( &$hObj, $keys ) {
72  foreach ( $keys as $i => $key ) {
73  $hObj->set( $key, $i );
74  }
75  }
76  ];
77  $mObj = null;
78  '@phan-var MapCacheLRU $mObj';
79  $benches['MapCacheLRU::set'] = [
80  'setup' => static function () use ( &$mObj, $max ) {
81  $mObj = new MapCacheLRU( $max );
82  },
83  'function' => static function () use ( &$mObj, $keys ) {
84  foreach ( $keys as $i => $key ) {
85  $mObj->set( $key, $i );
86  }
87  }
88  ];
89  }
90 
91  if ( !$method || $method === 'get' ) {
92  $hObj = null;
93  '@phan-var BagOStuff $hObj';
94  $benches['HashBagOStuff::get'] = [
95  'setup' => static function () use ( &$hObj, $max, $keys ) {
96  $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
97  foreach ( $keys as $i => $key ) {
98  $hObj->set( $key, $i );
99  }
100  },
101  'function' => static function () use ( &$hObj, $keys ) {
102  foreach ( $keys as $key ) {
103  $hObj->get( $key );
104  }
105  }
106  ];
107  $mObj = null;
108  '@phan-var MapCacheLRU $mObj';
109  $benches['MapCacheLRU::get'] = [
110  'setup' => static function () use ( &$mObj, $max, $keys ) {
111  $mObj = new MapCacheLRU( $max );
112  foreach ( $keys as $i => $key ) {
113  $mObj->set( $key, $i );
114  }
115  },
116  'function' => static function () use ( &$mObj, $keys ) {
117  foreach ( $keys as $key ) {
118  $mObj->get( $key );
119  }
120  }
121  ];
122  }
123 
124  $this->bench( $benches );
125  }
126 }
127 
128 $maintClass = BenchmarkLruHash::class;
129 require_once RUN_MAINTENANCE_IF_MAIN;
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Maintenance script that benchmarks HashBagOStuff and MapCacheLRU.
__construct()
Default constructor.
execute()
Do the actual work.
Base class for benchmark scripts.
Definition: Benchmarker.php:40
bench(array $benchs)
Definition: Benchmarker.php:49
Simple store for keeping values in an associative array for the current process.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Store key-value entries in a size-limited in-memory LRU cache.
Definition: MapCacheLRU.php:34