MediaWiki master
benchmarkLruHash.php
Go to the documentation of this file.
1<?php
23
24require_once __DIR__ . '/../includes/Benchmarker.php';
25
32 protected $defaultCount = 1000;
33
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Benchmarks HashBagOStuff and MapCacheLRU.' );
37 $this->addOption( 'method', 'One of "construct" or "set". Default: [All]', false, true );
38 }
39
40 public function execute() {
41 $exampleKeys = [];
42 $max = 100;
43 $count = 500;
44 while ( $count-- ) {
45 $exampleKeys[] = wfRandomString();
46 }
47 // 1000 keys (1...500, 500...1)
48 $keys = array_merge( $exampleKeys, array_reverse( $exampleKeys ) );
49
50 $method = $this->getOption( 'method' );
51 $benches = [];
52
53 if ( !$method || $method === 'construct' ) {
54 $benches['HashBagOStuff::__construct'] = [
55 'function' => static function () use ( $max ) {
56 $obj = new HashBagOStuff( [ 'maxKeys' => $max ] );
57 },
58 ];
59 $benches['MapCacheLRU::__construct'] = [
60 'function' => static function () use ( $max ) {
61 $obj = new MapCacheLRU( $max );
62 },
63 ];
64 }
65
66 if ( !$method || $method === 'set' ) {
67 $hObj = null;
68 '@phan-var BagOStuff $hObj';
69 $benches['HashBagOStuff::set'] = [
70 'setup' => static function () use ( &$hObj, $max ) {
71 $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
72 },
73 'function' => static function () use ( &$hObj, $keys ) {
74 foreach ( $keys as $i => $key ) {
75 $hObj->set( $key, $i );
76 }
77 }
78 ];
79 $mObj = null;
80 '@phan-var MapCacheLRU $mObj';
81 $benches['MapCacheLRU::set'] = [
82 'setup' => static function () use ( &$mObj, $max ) {
83 $mObj = new MapCacheLRU( $max );
84 },
85 'function' => static function () use ( &$mObj, $keys ) {
86 foreach ( $keys as $i => $key ) {
87 $mObj->set( $key, $i );
88 }
89 }
90 ];
91 }
92
93 if ( !$method || $method === 'get' ) {
94 $hObj = null;
95 '@phan-var BagOStuff $hObj';
96 $benches['HashBagOStuff::get'] = [
97 'setup' => static function () use ( &$hObj, $max, $keys ) {
98 $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
99 foreach ( $keys as $i => $key ) {
100 $hObj->set( $key, $i );
101 }
102 },
103 'function' => static function () use ( &$hObj, $keys ) {
104 foreach ( $keys as $key ) {
105 $hObj->get( $key );
106 }
107 }
108 ];
109 $mObj = null;
110 '@phan-var MapCacheLRU $mObj';
111 $benches['MapCacheLRU::get'] = [
112 'setup' => static function () use ( &$mObj, $max, $keys ) {
113 $mObj = new MapCacheLRU( $max );
114 foreach ( $keys as $i => $key ) {
115 $mObj->set( $key, $i );
116 }
117 },
118 'function' => static function () use ( &$mObj, $keys ) {
119 foreach ( $keys as $key ) {
120 $mObj->get( $key );
121 }
122 }
123 ];
124 }
125
126 $this->bench( $benches );
127 }
128}
129
130$maintClass = BenchmarkLruHash::class;
131require_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)
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.
Simple store for keeping values in an associative array for the current process.