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