MediaWiki master
benchmarkLruHash.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/../includes/Benchmarker.php';
29// @codeCoverageIgnoreEnd
30
38 protected $defaultCount = 1000;
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Benchmarks HashBagOStuff and MapCacheLRU.' );
43 $this->addOption( 'method', 'One of "construct" or "set". Default: [All]', false, true );
44 }
45
46 public function execute() {
47 $exampleKeys = [];
48 $max = 100;
49 $count = 500;
50 while ( $count-- ) {
51 $exampleKeys[] = wfRandomString();
52 }
53 // 1000 keys (1...500, 500...1)
54 $keys = array_merge( $exampleKeys, array_reverse( $exampleKeys ) );
55
56 $method = $this->getOption( 'method' );
57 $benches = [];
58
59 if ( !$method || $method === 'construct' ) {
60 $benches['HashBagOStuff::__construct'] = [
61 'function' => static function () use ( $max ) {
62 $obj = new HashBagOStuff( [ 'maxKeys' => $max ] );
63 },
64 ];
65 $benches['MapCacheLRU::__construct'] = [
66 'function' => static function () use ( $max ) {
67 $obj = new MapCacheLRU( $max );
68 },
69 ];
70 }
71
72 if ( !$method || $method === 'set' ) {
73 $hObj = null;
74 '@phan-var BagOStuff $hObj';
75 $benches['HashBagOStuff::set'] = [
76 'setup' => static function () use ( &$hObj, $max ) {
77 $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
78 },
79 'function' => static function () use ( &$hObj, $keys ) {
80 foreach ( $keys as $i => $key ) {
81 $hObj->set( $key, $i );
82 }
83 }
84 ];
85 $mObj = null;
86 '@phan-var MapCacheLRU $mObj';
87 $benches['MapCacheLRU::set'] = [
88 'setup' => static function () use ( &$mObj, $max ) {
89 $mObj = new MapCacheLRU( $max );
90 },
91 'function' => static function () use ( &$mObj, $keys ) {
92 foreach ( $keys as $i => $key ) {
93 $mObj->set( $key, $i );
94 }
95 }
96 ];
97 }
98
99 if ( !$method || $method === 'get' ) {
100 $hObj = null;
101 '@phan-var BagOStuff $hObj';
102 $benches['HashBagOStuff::get'] = [
103 'setup' => static function () use ( &$hObj, $max, $keys ) {
104 $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
105 foreach ( $keys as $i => $key ) {
106 $hObj->set( $key, $i );
107 }
108 },
109 'function' => static function () use ( &$hObj, $keys ) {
110 foreach ( $keys as $key ) {
111 $hObj->get( $key );
112 }
113 }
114 ];
115 $mObj = null;
116 '@phan-var MapCacheLRU $mObj';
117 $benches['MapCacheLRU::get'] = [
118 'setup' => static function () use ( &$mObj, $max, $keys ) {
119 $mObj = new MapCacheLRU( $max );
120 foreach ( $keys as $i => $key ) {
121 $mObj->set( $key, $i );
122 }
123 },
124 'function' => static function () use ( &$mObj, $keys ) {
125 foreach ( $keys as $key ) {
126 $mObj->get( $key );
127 }
128 }
129 ];
130 }
131
132 $this->bench( $benches );
133 }
134}
135
136// @codeCoverageIgnoreStart
137$maintClass = BenchmarkLruHash::class;
138require_once RUN_MAINTENANCE_IF_MAIN;
139// @codeCoverageIgnoreEnd
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.
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.
addDescription( $text)
Set the description text.
Store key-value entries in a size-limited in-memory LRU cache.
Abstract class for any ephemeral data store.
Definition BagOStuff.php:87
Store data in a memory for the current request/process only.