1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Tools\Reports\Formatters\BenchmarkFunctionFormatter; |
6
|
|
|
use AlecRabbit\Tools\Reports\Formatters\BenchmarkReportFormatter; |
7
|
|
|
use AlecRabbit\Tools\Reports\Formatters\ExtendedCounterReportFormatter; |
8
|
|
|
use AlecRabbit\Tools\Reports\Formatters\Formatter; |
9
|
|
|
use AlecRabbit\Tools\Reports\Formatters\ProfilerReportFormatter; |
10
|
|
|
use AlecRabbit\Tools\Reports\Formatters\SimpleCounterReportFormatter; |
11
|
|
|
use AlecRabbit\Tools\Reports\Formatters\TimerReportFormatter; |
12
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
13
|
|
|
use function AlecRabbit\typeOf; |
14
|
|
|
|
15
|
|
|
class Factory |
16
|
|
|
{ |
17
|
|
|
/** @var null|TimerReportFormatter */ |
18
|
|
|
protected static $timerReportFormatter; |
19
|
|
|
|
20
|
|
|
/** @var null|SimpleCounterReportFormatter */ |
21
|
|
|
protected static $simpleCounterReportFormatter; |
22
|
|
|
|
23
|
|
|
/** @var null|ExtendedCounterReportFormatter */ |
24
|
|
|
protected static $extendedCounterReportFormatter; |
25
|
|
|
|
26
|
|
|
/** @var null|ProfilerReportFormatter */ |
27
|
|
|
protected static $profilerReportFormatter; |
28
|
|
|
|
29
|
|
|
/** @var null|BenchmarkReportFormatter */ |
30
|
|
|
protected static $benchmarkReportFormatter; |
31
|
|
|
|
32
|
|
|
/** @var null|BenchmarkFunctionFormatter */ |
33
|
|
|
protected static $benchmarkFunctionFormatter; |
34
|
|
|
|
35
|
|
|
/** @var int */ |
36
|
|
|
protected static $defaultIterations = 1000000; |
37
|
|
|
|
38
|
|
|
/** @codeCoverageIgnore */ |
39
|
|
|
private function __construct() |
40
|
|
|
{ |
41
|
|
|
// Static class |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $defaultIterations |
46
|
|
|
*/ |
47
|
|
|
public static function setDefaultIterations(int $defaultIterations): void |
48
|
|
|
{ |
49
|
|
|
self::$defaultIterations = $defaultIterations; |
50
|
|
|
} |
51
|
|
|
|
52
|
8 |
|
public static function setFormatter(Formatter $formatter): Formatter |
53
|
|
|
{ |
54
|
8 |
|
if ($formatter instanceof SimpleCounterReportFormatter) { |
55
|
1 |
|
static::setSimpleCounterReportFormatter($formatter); |
56
|
|
|
return |
57
|
1 |
|
static::getSimpleCounterReportFormatter(); |
58
|
|
|
} |
59
|
7 |
|
if ($formatter instanceof ExtendedCounterReportFormatter) { |
60
|
1 |
|
static::setExtendedCounterReportFormatter($formatter); |
61
|
|
|
return |
62
|
1 |
|
static::getExtendedCounterReportFormatter(); |
63
|
|
|
} |
64
|
6 |
|
if ($formatter instanceof TimerReportFormatter) { |
65
|
1 |
|
static::setTimerReportFormatter($formatter); |
66
|
|
|
return |
67
|
1 |
|
static::getTimerReportFormatter(); |
68
|
|
|
} |
69
|
5 |
|
if ($formatter instanceof ProfilerReportFormatter) { |
70
|
1 |
|
static::setProfilerReportFormatter($formatter); |
71
|
|
|
return |
72
|
1 |
|
static::getProfilerReportFormatter(); |
73
|
|
|
} |
74
|
4 |
|
if ($formatter instanceof BenchmarkReportFormatter) { |
75
|
2 |
|
static::setBenchmarkReportFormatter($formatter); |
76
|
|
|
return |
77
|
2 |
|
static::getBenchmarkReportFormatter(); |
78
|
|
|
} |
79
|
2 |
|
if ($formatter instanceof BenchmarkFunctionFormatter) { |
80
|
1 |
|
static::setBenchmarkFunctionFormatter($formatter); |
81
|
|
|
return |
82
|
1 |
|
static::getBenchmarkFunctionFormatter(); |
83
|
|
|
} |
84
|
1 |
|
throw new \RuntimeException('Formatter [' . typeOf($formatter) . '] is not accepted.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return SimpleCounterReportFormatter |
89
|
|
|
*/ |
90
|
6 |
|
public static function getSimpleCounterReportFormatter(): SimpleCounterReportFormatter |
91
|
|
|
{ |
92
|
6 |
|
if (null === static::$simpleCounterReportFormatter) { |
93
|
1 |
|
static::$simpleCounterReportFormatter = new SimpleCounterReportFormatter(); |
94
|
|
|
} |
95
|
|
|
return |
96
|
6 |
|
static::$simpleCounterReportFormatter; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param null|SimpleCounterReportFormatter $simpleCounterReportFormatter |
101
|
|
|
*/ |
102
|
2 |
|
public static function setSimpleCounterReportFormatter( |
103
|
|
|
?SimpleCounterReportFormatter $simpleCounterReportFormatter |
104
|
|
|
): void { |
105
|
2 |
|
static::$simpleCounterReportFormatter = $simpleCounterReportFormatter; |
106
|
2 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return ExtendedCounterReportFormatter |
110
|
|
|
*/ |
111
|
5 |
|
public static function getExtendedCounterReportFormatter(): ExtendedCounterReportFormatter |
112
|
|
|
{ |
113
|
5 |
|
if (null === static::$extendedCounterReportFormatter) { |
114
|
1 |
|
static::$extendedCounterReportFormatter = new ExtendedCounterReportFormatter(); |
115
|
|
|
} |
116
|
|
|
return |
117
|
5 |
|
static::$extendedCounterReportFormatter; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param null|ExtendedCounterReportFormatter $extendedCounterReportFormatter |
122
|
|
|
*/ |
123
|
2 |
|
public static function setExtendedCounterReportFormatter( |
124
|
|
|
?ExtendedCounterReportFormatter $extendedCounterReportFormatter |
125
|
|
|
): void { |
126
|
2 |
|
static::$extendedCounterReportFormatter = $extendedCounterReportFormatter; |
127
|
2 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return TimerReportFormatter |
131
|
|
|
*/ |
132
|
15 |
|
public static function getTimerReportFormatter(): TimerReportFormatter |
133
|
|
|
{ |
134
|
15 |
|
if (null === static::$timerReportFormatter) { |
135
|
1 |
|
static::$timerReportFormatter = new TimerReportFormatter(); |
136
|
|
|
} |
137
|
|
|
return |
138
|
15 |
|
static::$timerReportFormatter; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param null|TimerReportFormatter $timerReportFormatter |
143
|
|
|
*/ |
144
|
2 |
|
public static function setTimerReportFormatter(?TimerReportFormatter $timerReportFormatter): void |
145
|
|
|
{ |
146
|
2 |
|
static::$timerReportFormatter = $timerReportFormatter; |
147
|
2 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return ProfilerReportFormatter |
151
|
|
|
*/ |
152
|
4 |
|
public static function getProfilerReportFormatter(): ProfilerReportFormatter |
153
|
|
|
{ |
154
|
4 |
|
if (null === static::$profilerReportFormatter) { |
155
|
1 |
|
static::$profilerReportFormatter = new ProfilerReportFormatter(); |
156
|
|
|
} |
157
|
|
|
return |
158
|
4 |
|
static::$profilerReportFormatter; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param null|ProfilerReportFormatter $profilerReportFormatter |
163
|
|
|
*/ |
164
|
2 |
|
public static function setProfilerReportFormatter( |
165
|
|
|
?ProfilerReportFormatter $profilerReportFormatter |
166
|
|
|
): void { |
167
|
2 |
|
static::$profilerReportFormatter = $profilerReportFormatter; |
168
|
2 |
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return BenchmarkReportFormatter |
172
|
|
|
*/ |
173
|
12 |
|
public static function getBenchmarkReportFormatter(): BenchmarkReportFormatter |
174
|
|
|
{ |
175
|
12 |
|
if (null === static::$benchmarkReportFormatter) { |
176
|
1 |
|
static::$benchmarkReportFormatter = new BenchmarkReportFormatter(); |
177
|
|
|
} |
178
|
|
|
return |
179
|
12 |
|
static::$benchmarkReportFormatter; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param null|BenchmarkReportFormatter $benchmarkReportFormatter |
184
|
|
|
*/ |
185
|
3 |
|
public static function setBenchmarkReportFormatter( |
186
|
|
|
?BenchmarkReportFormatter $benchmarkReportFormatter |
187
|
|
|
): void { |
188
|
3 |
|
static::$benchmarkReportFormatter = $benchmarkReportFormatter; |
189
|
3 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return BenchmarkFunctionFormatter |
193
|
|
|
*/ |
194
|
11 |
|
public static function getBenchmarkFunctionFormatter(): BenchmarkFunctionFormatter |
195
|
|
|
{ |
196
|
11 |
|
if (null === static::$benchmarkFunctionFormatter) { |
197
|
1 |
|
static::$benchmarkFunctionFormatter = new BenchmarkFunctionFormatter(); |
198
|
|
|
} |
199
|
|
|
return |
200
|
11 |
|
static::$benchmarkFunctionFormatter->resetEqualReturns(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param null|BenchmarkFunctionFormatter $benchmarkFunctionFormatter |
205
|
|
|
*/ |
206
|
2 |
|
public static function setBenchmarkFunctionFormatter( |
207
|
|
|
?BenchmarkFunctionFormatter $benchmarkFunctionFormatter |
208
|
|
|
): void { |
209
|
2 |
|
static::$benchmarkFunctionFormatter = $benchmarkFunctionFormatter; |
210
|
2 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param null|int $iterations |
214
|
|
|
* @param bool $withProgressBar |
215
|
|
|
* @return Benchmark |
216
|
|
|
* @throws \Exception |
217
|
|
|
*/ |
218
|
|
|
public static function createBenchmark(?int $iterations = null, bool $withProgressBar = true): Benchmark |
219
|
|
|
{ |
220
|
|
|
// TODO where to set colored formatters? |
221
|
|
|
|
222
|
|
|
$iterations = $iterations ?? static::$defaultIterations; |
223
|
|
|
if (!$withProgressBar) { |
224
|
|
|
return |
225
|
|
|
new Benchmark($iterations); |
226
|
|
|
} |
227
|
|
|
if (\class_exists(ProgressBar::class)) { |
228
|
|
|
return |
229
|
|
|
new BenchmarkSymfonyProgressBar($iterations); |
230
|
|
|
} |
231
|
|
|
return |
232
|
|
|
new BenchmarkSimpleProgressBar($iterations); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|