]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/benchmarks/benchmarkLruHash.php
MediaWiki 1.30.2-scripts
[autoinstalls/mediawiki.git] / maintenance / benchmarks / benchmarkLruHash.php
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 require_once __DIR__ . '/Benchmarker.php';
23
24 /**
25  * Maintenance script that benchmarks HashBagOStuff and MapCacheLRU.
26  *
27  * @ingroup Benchmark
28  */
29 class BenchmarkLruHash extends Benchmarker {
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;
97 require_once RUN_MAINTENANCE_IF_MAIN;