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