MediaWiki REL1_31
benchmarkLruHash.php
Go to the documentation of this file.
1<?php
22require_once __DIR__ . '/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' => function () use ( $max ) {
54 $obj = new HashBagOStuff( [ 'maxKeys' => $max ] );
55 },
56 ];
57 $benches['MapCacheLRU::__construct'] = [
58 'function' => function () use ( $max ) {
59 $obj = new MapCacheLRU( $max );
60 },
61 ];
62 }
63
64 if ( !$method || $method === 'set' ) {
65 // For the set bechmark, do object creation in setup (not measured)
66 $hObj = null;
67 $benches['HashBagOStuff::set'] = [
68 'setup' => function () use ( &$hObj, $max ) {
69 $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
70 },
71 'function' => function () use ( &$hObj, &$keys ) {
72 foreach ( $keys as $i => $key ) {
73 $hObj->set( $key, $i );
74 }
75 }
76 ];
77 $mObj = null;
78 $benches['MapCacheLRU::set'] = [
79 'setup' => function () use ( &$mObj, $max ) {
80 $mObj = new MapCacheLRU( $max );
81 },
82 'function' => function () use ( &$mObj, &$keys ) {
83 foreach ( $keys as $i => $key ) {
84 $mObj->set( $key, $i );
85 }
86 }
87 ];
88 }
89
90 $this->bench( $benches );
91 }
92}
93
94$maintClass = BenchmarkLruHash::class;
95require_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.
bench(array $benchs)
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.
Handles a simple LRU key/value map with a maximum number of entries.
require_once RUN_MAINTENANCE_IF_MAIN