3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 require_once __DIR__
. '/Benchmarker.php';
25 * Maintenance script that benchmarks HashBagOStuff and MapCacheLRU.
29 class BenchmarkLruHash
extends Benchmarker
{
30 protected $defaultCount = 1000;
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 );
38 public function execute() {
43 $exampleKeys[] = wfRandomString();
45 // 1000 keys (1...500, 500...1)
46 $keys = array_merge( $exampleKeys, array_reverse( $exampleKeys ) );
48 $method = $this->getOption( 'method' );
51 if ( !$method ||
$method === 'construct' ) {
52 $benches['HashBagOStuff::__construct'] = [
53 'function' => function () use ( $max ) {
54 $obj = new HashBagOStuff( [ 'maxKeys' => $max ] );
57 $benches['MapCacheLRU::__construct'] = [
58 'function' => function () use ( $max ) {
59 $obj = new MapCacheLRU( $max );
64 if ( !$method ||
$method === 'set' ) {
65 // For the set bechmark, do object creation in setup (not measured)
67 $benches['HashBagOStuff::set'] = [
68 'setup' => function () use ( &$hObj, $max ) {
69 $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
71 'function' => function () use ( &$hObj, &$keys ) {
72 foreach ( $keys as $i => $key ) {
73 $hObj->set( $key, $i );
78 $benches['MapCacheLRU::set'] = [
79 'setup' => function () use ( &$mObj, $max ) {
80 $mObj = new MapCacheLRU( $max );
82 'function' => function () use ( &$mObj, &$keys ) {
83 foreach ( $keys as $i => $key ) {
84 $mObj->set( $key, $i );
90 $this->bench( $benches );
94 $maintClass = BenchmarkLruHash
::class;
95 require_once RUN_MAINTENANCE_IF_MAIN
;