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