Passed
Push — main ( 937a73...5a2554 )
by Eric
01:54
created

LookupBench::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 10
rs 10
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) {
1 ignored issue
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting ',' or ')' on line 43 at column 29
Loading history...
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