-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathVariantBuilder.php
More file actions
129 lines (103 loc) · 3.07 KB
/
VariantBuilder.php
File metadata and controls
129 lines (103 loc) · 3.07 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace PhpBench\Tests\Util;
use DateTime;
use PhpBench\Model\Benchmark;
use PhpBench\Model\Error;
use PhpBench\Model\ParameterSet;
use PhpBench\Model\Result\TimeResult;
use PhpBench\Model\Subject;
use PhpBench\Model\Suite;
use PhpBench\Model\Variant;
use RuntimeException;
final class VariantBuilder
{
/**
* @var IterationBuilder[]
*/
private array $iterations = [];
private int $revs = 1;
/**
* @var Error[]
*/
private array $errors = [];
private ?ParameterSet $parameterSet = null;
/**
* @param string $name @deprecated Variants are not named, and this was used as the parameter set name.
*/
public function __construct(private readonly ?SubjectBuilder $subjectBuilder, private readonly ?string $name = null)
{
}
public static function create(string $name = 'foo'): self
{
return new self(null, $name);
}
public function setRevs(int $revs): self
{
$this->revs = $revs;
return $this;
}
public function addIterationWithTimeResult(int $netTime, int $revs): VariantBuilder
{
$this->iteration()->setResult(new TimeResult($netTime, $revs));
return $this;
}
public function iteration(): IterationBuilder
{
return (function (IterationBuilder $builder) {
$this->iterations[] = $builder;
return $builder;
})(new IterationBuilder($this));
}
/**
* @param array<string,mixed> $parameters
*/
public function withParameterSet(string $name, array $parameters): VariantBuilder
{
$this->parameterSet = ParameterSet::fromUnserializedValues($name, $parameters);
return $this;
}
public function build(?Subject $subject = null): Variant
{
if (null === $subject) {
$suite = new Suite(
'testSuite',
new DateTime()
);
$benchmark = new Benchmark($suite, 'testBenchmark');
$subject = new Subject($benchmark, 'foo');
}
$variant = new Variant(
$subject,
$this->parameterSet ?? ParameterSet::fromSerializedParameters($this->name ?? '0', []),
$this->revs,
1,
[]
);
foreach ($this->iterations as $iteration) {
$iteration->build($variant);
}
$variant->computeStats();
if ($this->errors) {
$variant->createErrorStack($this->errors);
}
return $variant;
}
public function end(): SubjectBuilder
{
if (null === $this->subjectBuilder) {
throw new RuntimeException(
'This variant builder was not created by a subject builder, end() cannot return anything'
);
}
return $this->subjectBuilder;
}
public static function forSubjectBuilder(SubjectBuilder $subjectBuilder, ?string $name = null): self
{
return new self($subjectBuilder, $name);
}
public function withError(Error $error): self
{
$this->errors[] = $error;
return $this;
}
}