Debian Codegraph @

maintenance/benchmarks/benchmarkLruHash.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Benchmark
20 */
21
22 use Wikimedia\ObjectCache\BagOStuff;
23 use Wikimedia\ObjectCache\HashBagOStuff;
24
25 // @codeCoverageIgnoreStart
26 require_once __DIR__ . '/../includes/Benchmarker.php';
27 // @codeCoverageIgnoreEnd
28
29 /**
30 * Maintenance script that benchmarks HashBagOStuff and MapCacheLRU.
31 *
32 * @ingroup Benchmark
33 */
34 class BenchmarkLruHash extends Benchmarker {
35 /** @inheritDoc */
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;
136 require_once RUN_MAINTENANCE_IF_MAIN;
137 // @codeCoverageIgnoreEnd