Repository navigation
Expand file tree
/
Copy pathOperatorTypeSpecifyingExtensionBench.php
More file actions
99 lines (84 loc) · 3.36 KB
/
Copy pathOperatorTypeSpecifyingExtensionBench.php
File metadata and controls
99 lines (84 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php declare(strict_types = 1);
namespace PHPStan\Benchmark;
use LogicException;
use PHPStan\Php\PhpVersion;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\OperatorTypeSpecifyingExtension;
use PHPStan\Type\Php\BcMathNumberOperatorTypeSpecifyingExtension;
use PHPStan\Type\Php\GmpOperatorTypeSpecifyingExtension;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Tally\Attribute\Before;
use Tally\Attribute\DataProvider;
use Tally\Attribute\ExpectedFor;
use Tally\ScratchDirectory;
/**
* The operator extensions are asked about each arithmetic and comparison operation in the analysed code.
*/
final class OperatorTypeSpecifyingExtensionBench
{
private const OPERATIONS = 64;
private ?OperatorTypeSpecifyingExtension $extension = null;
/** @var list<array{Type, Type}> */
private array $operands = [];
public function __construct(private ScratchDirectory $scratch)
{
}
/**
* @return iterable<string, array{class-string<OperatorTypeSpecifyingExtension>, string}>
*/
public static function extensions(): iterable
{
foreach (['gmp' => GmpOperatorTypeSpecifyingExtension::class, 'bcmath' => BcMathNumberOperatorTypeSpecifyingExtension::class] as $name => $extension) {
// most operations in the code have no operand of the extension's class
yield $name . '-integers' => [$extension, 'integers'];
yield $name . '-objects' => [$extension, 'objects'];
}
}
/**
* @param class-string<OperatorTypeSpecifyingExtension> $extension
*/
#[Before]
public function boot(string $extension, string $operands): void
{
BenchContainer::create($this->scratch);
$this->extension = $extension === BcMathNumberOperatorTypeSpecifyingExtension::class
? new BcMathNumberOperatorTypeSpecifyingExtension(new PhpVersion(80400))
: new $extension();
$class = $extension === BcMathNumberOperatorTypeSpecifyingExtension::class ? 'BcMath\Number' : 'GMP';
// reflecting the class from its stub happens once per analysis
$this->extension->isOperatorSupported('+', new IntegerType(), new StringType());
$this->extension->isOperatorSupported('+', new ObjectType($class), new IntegerType());
$this->extension->specifyType('+', new ObjectType($class), new IntegerType());
for ($i = 0; $i < self::OPERATIONS; $i++) {
$this->operands[] = $operands === 'integers'
? [new ConstantIntegerType($i), new IntegerType()]
: [new ObjectType($class), new ConstantIntegerType($i)];
}
}
/**
* @param class-string<OperatorTypeSpecifyingExtension> $extension
*/
#[DataProvider(method: 'extensions')]
#[ExpectedFor(dataSet: 'bcmath-integers', instructions: '317.4K', cycles: '383.8K', memory: '0B')]
#[ExpectedFor(dataSet: 'bcmath-objects', instructions: '665.4K', cycles: '781.7K', memory: '216B')]
#[ExpectedFor(dataSet: 'gmp-integers', instructions: '302.1K', cycles: '367.6K', memory: '0B')]
#[ExpectedFor(dataSet: 'gmp-objects', instructions: '606.5K', cycles: '721.9K', memory: '216B')]
public function benchSpecify(string $extension, string $operands): int
{
if ($this->extension === null) {
throw new LogicException('boot() did not run.');
}
$specified = 0;
foreach ($this->operands as [$left, $right]) {
if (!$this->extension->isOperatorSupported('+', $left, $right)) {
continue;
}
$this->extension->specifyType('+', $left, $right);
$specified++;
}
return $specified;
}
}