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.
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.
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
22 require_once __DIR__
. '/Benchmarker.php';
25 * Maintenance script that benchmarks HashBagOStuff and MapCacheLRU.
29 class BenchmarkLruHash
extends Benchmarker
{
30 protected $defaultCount = 1000;
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 );
39 public function execute() {
44 $exampleKeys[] = wfRandomString();
46 // 1000 keys (1...500, 500...1)
47 $keys = array_merge( $exampleKeys, array_reverse( $exampleKeys ) );
49 $fill = $this->hasOption( 'fill' ) ||
!$this->hasOption( 'construct' );
50 $construct = $this->hasOption( 'construct' ) ||
!$this->hasOption( 'fill' );
54 $benches['HashBagOStuff-construct'] = [
55 'function' => function () use ( $max ) {
56 $obj = new HashBagOStuff( [ 'maxKeys' => $max ] );
59 $benches['MapCacheLRU-construct'] = [
60 'function' => function () use ( $max ) {
61 $obj = new MapCacheLRU( $max );
67 // For the fill bechmark, ensure object creation is not measured.
69 $benches['HashBagOStuff-fill'] = [
70 'setup' => function () use ( &$hObj, $max ) {
71 $hObj = new HashBagOStuff( [ 'maxKeys' => $max ] );
73 'function' => function () use ( &$hObj, &$keys ) {
74 foreach ( $keys as $i => $key ) {
75 $hObj->set( $key, $i );
80 $benches['MapCacheLRU-fill'] = [
81 'setup' => function () use ( &$mObj, $max ) {
82 $mObj = new MapCacheLRU( $max );
84 'function' => function () use ( &$mObj, &$keys ) {
85 foreach ( $keys as $i => $key ) {
86 $mObj->set( $key, $i );
92 $this->bench( $benches );
96 $maintClass = BenchmarkLruHash
::class;
97 require_once RUN_MAINTENANCE_IF_MAIN
;