|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of Esi\ConsistentHash. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Eric Sizemore <[email protected]> |
|
9
|
|
|
* (c) Paul Annesley <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* This source file is subject to the MIT license. For the full copyright and |
|
12
|
|
|
* license information, please view the LICENSE file that was distributed with |
|
13
|
|
|
* this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Esi\ConsistentHash\Tests\Bench; |
|
17
|
|
|
|
|
18
|
|
|
use Esi\ConsistentHash\ConsistentHash; |
|
19
|
|
|
use Esi\ConsistentHash\Hasher\Md5Hasher; |
|
20
|
|
|
use Generator; |
|
21
|
|
|
use PhpBench\Attributes\ParamProviders; |
|
22
|
|
|
|
|
23
|
|
|
use function bin2hex; |
|
24
|
|
|
use function random_bytes; |
|
25
|
|
|
use function range; |
|
26
|
|
|
|
|
27
|
|
|
final class LookupBench |
|
28
|
|
|
{ |
|
29
|
|
|
private ConsistentHash $hasher; |
|
30
|
|
|
|
|
31
|
|
|
/** @var list<string> */ |
|
32
|
|
|
private array $randomKeys = []; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->hasher = new ConsistentHash(new Md5Hasher(), 64); |
|
37
|
|
|
|
|
38
|
|
|
foreach (range(1, 10) as $i) { |
|
39
|
|
|
$this->hasher->addTarget('target_' . $i, 1); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Generate random lookup keys outside the measurement function. |
|
43
|
|
|
foreach (range(1, 100_000) as $ignored) { |
|
|
|
|
|
|
44
|
|
|
$this->randomKeys[] = bin2hex(random_bytes(12)); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array{count: int} $params |
|
50
|
|
|
*/ |
|
51
|
|
|
#[ParamProviders(['provideLookupCount'])] |
|
52
|
|
|
public function benchLookup(array $params): void |
|
53
|
|
|
{ |
|
54
|
|
|
foreach (range(0, $params['count'] - 1) as $i) { |
|
55
|
|
|
$this->hasher->lookup($this->randomKeys[$i]); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function provideLookupCount(): Generator |
|
60
|
|
|
{ |
|
61
|
|
|
yield '10_000 lookups' => ['count' => 10_000]; |
|
62
|
|
|
yield '20_000 lookups' => ['count' => 20_000]; |
|
63
|
|
|
yield '40_000 lookups' => ['count' => 40_000]; |
|
64
|
|
|
yield '80_000 lookups' => ['count' => 80_000]; |
|
65
|
|
|
yield '100_000 lookups' => ['count' => 100_000]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|