MediaWiki REL1_30
benchmarkLruHash.php
Go to the documentation of this file.
1<?php
22require_once __DIR__ . '/Benchmarker.php';
23
30 protected $defaultCount = 1000;
31
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( 'Benchmarks HashBagOStuff and MapCacheLRU.' );
35 $this->addOption( 'construct', 'Run construct only', false, false );
36 $this->addOption( 'fill', 'Run fill only', false, false );
37 }
38
39 public function execute() {
40 $exampleKeys = [];
41 $max = 100;
42 $count = 500;
43 while ( $count-- ) {
44 $exampleKeys[] = wfRandomString();
45 }
46 // 1000 keys (1...500, 500...1)
47 $keys = array_merge( $exampleKeys, array_reverse( $exampleKeys ) );
48
49 $fill = $this->hasOption( 'fill' ) || !$this->hasOption( 'construct' );
50 $construct = $this->hasOption( 'construct' ) || !$this->hasOption( 'fill' );
51 $benches = [];
52
53 if ( $construct ) {
54 $benches['HashBagOStuff-construct'] = [
55 'function' => function () use ( $max ) {
56 $obj = new HashBagOStuff( [ 'maxKeys' => $max ] );
57 },
58 ];
59 $benches['MapCacheLRU-construct'] = [
60 'function' => function () use ( $max ) {
61 $obj = new MapCacheLRU( $max );
62 },
63 ];
64 }
65
66 if ( $fill ) {
67 // For the fill bechmark, ensure object creation is not measured.
68 $hObj = null;
69 $benches['HashBagOStuff-fill'] = [
70 'setup' => function () use ( &$hObj, $max ) {
71 $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
72 },
73 'function' => function () use ( &$hObj, &$keys ) {
74 foreach ( $keys as $i => $key ) {
75 $hObj->set( $key, $i );
76 }
77 }
78 ];
79 $mObj = null;
80 $benches['MapCacheLRU-fill'] = [
81 'setup' => function () use ( &$mObj, $max ) {
82 $mObj = new MapCacheLRU( $max );
83 },
84 'function' => function () use ( &$mObj, &$keys ) {
85 foreach ( $keys as $i => $key ) {
86 $mObj->set( $key, $i );
87 }
88 }
89 ];
90 }
91
92 $this->bench( $benches );
93 }
94}
95
96$maintClass = BenchmarkLruHash::class;
97require_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)
Simple store for keeping values in an associative array for the current process.
hasOption( $name)
Checks to see if a particular param exists.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Handles a simple LRU key/value map with a maximum number of entries.
require_once RUN_MAINTENANCE_IF_MAIN