addDescription( 'Benchmarks HashBagOStuff and MapCacheLRU.' ); $this->addOption( 'method', 'One of "construct" or "set". Default: [All]', false, true ); } public function execute() { $exampleKeys = []; $max = 100; $count = 500; while ( $count-- ) { $exampleKeys[] = wfRandomString(); } // 1000 keys (1...500, 500...1) $keys = array_merge( $exampleKeys, array_reverse( $exampleKeys ) ); $method = $this->getOption( 'method' ); $benches = []; if ( !$method || $method === 'construct' ) { $benches['HashBagOStuff::__construct'] = [ 'function' => function () use ( $max ) { $obj = new HashBagOStuff( [ 'maxKeys' => $max ] ); }, ]; $benches['MapCacheLRU::__construct'] = [ 'function' => function () use ( $max ) { $obj = new MapCacheLRU( $max ); }, ]; } if ( !$method || $method === 'set' ) { // For the set bechmark, do object creation in setup (not measured) $hObj = null; $benches['HashBagOStuff::set'] = [ 'setup' => function () use ( &$hObj, $max ) { $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] ); }, 'function' => function () use ( &$hObj, &$keys ) { foreach ( $keys as $i => $key ) { $hObj->set( $key, $i ); } } ]; $mObj = null; $benches['MapCacheLRU::set'] = [ 'setup' => function () use ( &$mObj, $max ) { $mObj = new MapCacheLRU( $max ); }, 'function' => function () use ( &$mObj, &$keys ) { foreach ( $keys as $i => $key ) { $mObj->set( $key, $i ); } } ]; } $this->bench( $benches ); } } $maintClass = BenchmarkLruHash::class; require_once RUN_MAINTENANCE_IF_MAIN;