MediaWiki
master
Toggle main menu visibility
benchmarkLruHash.php
Go to the documentation of this file.
1
<?php
7
8
use
MediaWiki\Maintenance\Benchmarker
;
9
use
Wikimedia\ObjectCache\BagOStuff
;
10
use
Wikimedia\ObjectCache\HashBagOStuff
;
11
use
Wikimedia\ObjectCache\MapCacheLRU
;
12
13
// @codeCoverageIgnoreStart
14
require_once __DIR__ .
'/../includes/Benchmarker.php'
;
15
// @codeCoverageIgnoreEnd
16
22
class
BenchmarkLruHash
extends
Benchmarker
{
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;
124
require_once RUN_MAINTENANCE_IF_MAIN;
125
// @codeCoverageIgnoreEnd
wfRandomString
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Definition
GlobalFunctions.php:169
$maintClass
$maintClass
Definition
addChangeTag.php:56
BenchmarkLruHash
Maintenance script that benchmarks HashBagOStuff and MapCacheLRU.
Definition
benchmarkLruHash.php:22
BenchmarkLruHash\__construct
__construct()
Default constructor.
Definition
benchmarkLruHash.php:26
BenchmarkLruHash\execute
execute()
Do the actual work.
Definition
benchmarkLruHash.php:32
BenchmarkLruHash\$defaultCount
$defaultCount
Definition
benchmarkLruHash.php:24
MediaWiki\Maintenance\Benchmarker
Base class for benchmark scripts.
Definition
Benchmarker.php:29
MediaWiki\Maintenance\Benchmarker\bench
bench(array $benchs)
Definition
Benchmarker.php:39
MediaWiki\Maintenance\Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition
Maintenance.php:265
MediaWiki\Maintenance\Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition
Maintenance.php:299
MediaWiki\Maintenance\Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition
Maintenance.php:335
Wikimedia\ObjectCache\BagOStuff
Abstract class for any ephemeral data store.
Definition
BagOStuff.php:73
Wikimedia\ObjectCache\HashBagOStuff
Store data in a memory for the current request/process only.
Definition
HashBagOStuff.php:21
Wikimedia\ObjectCache\MapCacheLRU
Store key-value entries in a size-limited in-memory LRU cache.
Definition
MapCacheLRU.php:24
maintenance
benchmarks
benchmarkLruHash.php
Generated on
for MediaWiki by
1.18.0