|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools\Internal; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Tools\Timer; |
|
6
|
|
|
use AlecRabbit\Traits\GettableName; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class BenchmarkFunction |
|
10
|
|
|
* @package AlecRabbit |
|
11
|
|
|
* @internal |
|
12
|
|
|
*/ |
|
13
|
|
|
class BenchmarkFunction |
|
14
|
|
|
{ |
|
15
|
|
|
use GettableName; |
|
16
|
|
|
|
|
17
|
|
|
/** @var callable */ |
|
18
|
|
|
private $callable; |
|
19
|
|
|
|
|
20
|
|
|
/** @var int */ |
|
21
|
|
|
private $index; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
private $args; |
|
25
|
|
|
|
|
26
|
|
|
/** @var mixed */ |
|
27
|
|
|
private $result; |
|
28
|
|
|
|
|
29
|
|
|
/** @var null|string */ |
|
30
|
|
|
private $comment; |
|
31
|
|
|
|
|
32
|
|
|
/** @var null|string */ |
|
33
|
|
|
private $humanReadableName; |
|
34
|
|
|
|
|
35
|
|
|
/** @var \Throwable|null */ |
|
36
|
|
|
private $exception; |
|
37
|
|
|
|
|
38
|
|
|
/** @var Timer */ |
|
39
|
|
|
private $timer; |
|
40
|
|
|
|
|
41
|
|
|
/** @var null|BenchmarkRelative */ |
|
42
|
|
|
private $benchmarkRelative; |
|
43
|
|
|
|
|
44
|
|
|
/** @var bool */ |
|
45
|
|
|
protected $showReturns = true; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* BenchmarkFunction constructor. |
|
49
|
|
|
* @param callable $func |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @param int $index |
|
52
|
|
|
* @param array $args |
|
53
|
|
|
* @param null|string $comment |
|
54
|
|
|
* @param null|string $humanReadableName |
|
55
|
|
|
* @throws \Exception |
|
56
|
|
|
*/ |
|
57
|
13 |
|
public function __construct( |
|
58
|
|
|
$func, |
|
59
|
|
|
string $name, |
|
60
|
|
|
int $index, |
|
61
|
|
|
array $args, |
|
62
|
|
|
?string $comment = null, |
|
63
|
|
|
?string $humanReadableName = null |
|
64
|
|
|
) { |
|
65
|
13 |
|
$this->callable = $func; |
|
66
|
13 |
|
$this->name = $name; |
|
67
|
13 |
|
$this->index = $index; |
|
68
|
13 |
|
$this->args = $args; |
|
69
|
13 |
|
$this->comment = $comment; |
|
70
|
13 |
|
$this->humanReadableName = $humanReadableName; |
|
71
|
13 |
|
$this->timer = new Timer($this->getIndexedName()); |
|
72
|
13 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
13 |
|
public function getIndexedName(): string |
|
78
|
|
|
{ |
|
79
|
13 |
|
return "⟨{$this->getIndex()}⟩ {$this->getName()}"; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return int |
|
84
|
|
|
*/ |
|
85
|
13 |
|
public function getIndex(): int |
|
86
|
|
|
{ |
|
87
|
13 |
|
return $this->index; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
12 |
|
public function comment(): string |
|
94
|
|
|
{ |
|
95
|
12 |
|
return $this->comment ?? ''; |
|
96
|
|
|
// return $this->comment ? str_decorate($this->comment, '"') : ''; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
13 |
|
public function getArgs(): array |
|
103
|
|
|
{ |
|
104
|
13 |
|
return $this->args; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return callable |
|
109
|
|
|
*/ |
|
110
|
13 |
|
public function getCallable(): callable |
|
111
|
|
|
{ |
|
112
|
13 |
|
return $this->callable; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
13 |
|
public function enumeratedName(): string |
|
116
|
|
|
{ |
|
117
|
13 |
|
return $this->getIndexedName(); |
|
118
|
|
|
// return |
|
119
|
|
|
// brackets((string)$this->index, BRACKETS_ANGLE) . ' ' . $this->name; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
12 |
|
public function getReturn() |
|
126
|
|
|
{ |
|
127
|
12 |
|
return $this->result; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param mixed $result |
|
132
|
|
|
*/ |
|
133
|
13 |
|
public function setResult($result): void |
|
134
|
|
|
{ |
|
135
|
13 |
|
$this->result = $result; |
|
136
|
13 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return Timer |
|
140
|
|
|
*/ |
|
141
|
13 |
|
public function getTimer(): Timer |
|
142
|
|
|
{ |
|
143
|
13 |
|
return $this->timer; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return null|string |
|
148
|
|
|
*/ |
|
149
|
12 |
|
public function humanReadableName(): ?string |
|
150
|
|
|
{ |
|
151
|
12 |
|
return $this->humanReadableName ?? $this->getIndexedName(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return null|\Throwable |
|
156
|
|
|
*/ |
|
157
|
12 |
|
public function getException(): ?\Throwable |
|
158
|
|
|
{ |
|
159
|
12 |
|
return $this->exception; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param \Throwable $exception |
|
164
|
|
|
* @return BenchmarkFunction |
|
165
|
|
|
*/ |
|
166
|
5 |
|
public function setException(\Throwable $exception): BenchmarkFunction |
|
167
|
|
|
{ |
|
168
|
5 |
|
$this->exception = $exception; |
|
169
|
5 |
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return null|BenchmarkRelative |
|
174
|
|
|
*/ |
|
175
|
12 |
|
public function getBenchmarkRelative(): ?BenchmarkRelative |
|
176
|
|
|
{ |
|
177
|
12 |
|
return $this->benchmarkRelative; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param null|BenchmarkRelative $benchmarkRelative |
|
182
|
|
|
* @return BenchmarkFunction |
|
183
|
|
|
*/ |
|
184
|
13 |
|
public function setBenchmarkRelative(?BenchmarkRelative $benchmarkRelative): BenchmarkFunction |
|
185
|
|
|
{ |
|
186
|
13 |
|
$this->benchmarkRelative = $benchmarkRelative; |
|
187
|
13 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return bool |
|
192
|
|
|
*/ |
|
193
|
13 |
|
public function execute(): bool |
|
194
|
|
|
{ |
|
195
|
|
|
try { |
|
196
|
13 |
|
$this->setResult( |
|
197
|
13 |
|
($this->callable)(...$this->args) |
|
198
|
|
|
); |
|
199
|
13 |
|
return true; |
|
200
|
5 |
|
} catch (\Throwable $e) { |
|
201
|
5 |
|
$this->setException($e); |
|
202
|
|
|
} |
|
203
|
5 |
|
return false; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param bool $showReturns |
|
208
|
|
|
*/ |
|
209
|
13 |
|
public function setShowReturns(bool $showReturns): void |
|
210
|
|
|
{ |
|
211
|
13 |
|
$this->showReturns = $showReturns; |
|
212
|
13 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return bool |
|
216
|
|
|
*/ |
|
217
|
2 |
|
public function isShowReturns(): bool |
|
218
|
|
|
{ |
|
219
|
2 |
|
return $this->showReturns; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return bool |
|
224
|
|
|
*/ |
|
225
|
2 |
|
public function isNotShowReturns(): bool |
|
226
|
|
|
{ |
|
227
|
2 |
|
return !$this->isShowReturns(); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|