addDescription( 'Benchmarks HashBagOStuff and MapCacheLRU.' ); $this->addOption( 'construct', 'Run construct only', false, false ); $this->addOption( 'fill', 'Run fill only', false, false ); } 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 ) ); $fill = $this->hasOption( 'fill' ) || !$this->hasOption( 'construct' ); $construct = $this->hasOption( 'construct' ) || !$this->hasOption( 'fill' ); $benches = []; if ( $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 ( $fill ) { // For the fill bechmark, ensure object creation is not measured. $hObj = null; $benches['HashBagOStuff-fill'] = [ '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-fill'] = [ '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;