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