| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use Wikimedia\ObjectCache\BagOStuff; |
| 23 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 24 |
|
| 25 |
|
| 26 |
require_once __DIR__ . '/../includes/Benchmarker.php'; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class BenchmarkLruHash extends Benchmarker { |
| 35 |
|
| 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 |
|
| 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 |
|
| 135 |
$maintClass = BenchmarkLruHash::class; |
| 136 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 137 |
|