1
|
|
|
<?php |
2
|
|
|
namespace Scientist; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Execution |
8
|
|
|
* |
9
|
|
|
* An execution records the state of a callback, including how long it |
10
|
|
|
* took to execute, and what values were returned. |
11
|
|
|
* |
12
|
|
|
* @package \Scientist |
13
|
|
|
*/ |
14
|
|
|
class Result |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Callback result value. |
18
|
|
|
* |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
protected $value; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The time the callback was executed. |
25
|
|
|
* |
26
|
|
|
* @var float |
27
|
|
|
*/ |
28
|
|
|
protected $startTime; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The time the callback finished executing. |
32
|
|
|
* |
33
|
|
|
* @var float |
34
|
|
|
*/ |
35
|
|
|
protected $endTime; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The memory usage before the callback is executed. |
39
|
|
|
* |
40
|
|
|
* @var float |
41
|
|
|
*/ |
42
|
|
|
protected $startMemory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The memory usage after the callback is executed. |
46
|
|
|
* |
47
|
|
|
* @var float |
48
|
|
|
*/ |
49
|
|
|
protected $endMemory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Exception thrown by callback. |
53
|
|
|
* |
54
|
|
|
* @var \Exception|null |
55
|
|
|
*/ |
56
|
|
|
protected $exception; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Does the callback result value match the control. |
60
|
|
|
* |
61
|
|
|
* @var boolean |
62
|
|
|
*/ |
63
|
|
|
protected $match = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var mixed |
67
|
|
|
*/ |
68
|
|
|
protected $context; |
69
|
|
|
|
70
|
|
|
public function __construct($context = null) |
71
|
15 |
|
{ |
72
|
|
|
$this->context = $context; |
73
|
15 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the callback result value. |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function getValue() |
81
|
|
|
{ |
82
|
|
|
return $this->value; |
83
|
18 |
|
} |
84
|
|
|
|
85
|
18 |
|
/** |
86
|
|
|
* Set the callback result value. |
87
|
18 |
|
* |
88
|
|
|
* @param mixed $value |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function setValue($value) |
93
|
|
|
{ |
94
|
|
|
$this->value = $value; |
95
|
3 |
|
|
96
|
|
|
return $this; |
97
|
3 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the callback execution start time. |
101
|
|
|
* |
102
|
|
|
* @return float |
103
|
|
|
*/ |
104
|
|
|
public function getStartTime() |
105
|
|
|
{ |
106
|
|
|
return $this->startTime; |
107
|
21 |
|
} |
108
|
|
|
|
109
|
21 |
|
/** |
110
|
|
|
* Set the callback execution start time. |
111
|
21 |
|
* |
112
|
|
|
* @param float $startTime |
113
|
|
|
* |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function setStartTime($startTime) |
117
|
|
|
{ |
118
|
|
|
$this->startTime = $startTime; |
119
|
3 |
|
|
120
|
|
|
return $this; |
121
|
3 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the callback execution end time. |
125
|
|
|
* |
126
|
|
|
* @return float |
127
|
|
|
*/ |
128
|
|
|
public function getEndTime() |
129
|
|
|
{ |
130
|
|
|
return $this->endTime; |
131
|
19 |
|
} |
132
|
|
|
|
133
|
19 |
|
/** |
134
|
|
|
* Set the callback execution end time. |
135
|
19 |
|
* |
136
|
|
|
* @param float $endTime |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setEndTime($endTime) |
141
|
|
|
{ |
142
|
|
|
$this->endTime = $endTime; |
143
|
2 |
|
|
144
|
|
|
return $this; |
145
|
2 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the execution time of the callback. |
149
|
|
|
* |
150
|
|
|
* @return float |
151
|
|
|
*/ |
152
|
|
|
public function getTime() |
153
|
3 |
|
{ |
154
|
|
|
return $this->endTime - $this->startTime; |
155
|
3 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the callback execution starting memory usage. |
159
|
|
|
* |
160
|
|
|
* @return float |
161
|
|
|
*/ |
162
|
|
|
public function getStartMemory() |
163
|
|
|
{ |
164
|
|
|
return $this->startMemory; |
165
|
21 |
|
} |
166
|
|
|
|
167
|
21 |
|
/** |
168
|
|
|
* Set the callback execution starting memory usage. |
169
|
21 |
|
* |
170
|
|
|
* @param float $startMemory |
171
|
|
|
* |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function setStartMemory($startMemory) |
175
|
|
|
{ |
176
|
|
|
$this->startMemory = $startMemory; |
177
|
3 |
|
|
178
|
|
|
return $this; |
179
|
3 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the callback execution ending memory usage. |
183
|
|
|
* |
184
|
|
|
* @return float |
185
|
|
|
*/ |
186
|
|
|
public function getEndMemory() |
187
|
|
|
{ |
188
|
|
|
return $this->endMemory; |
189
|
19 |
|
} |
190
|
|
|
|
191
|
19 |
|
/** |
192
|
|
|
* Set the callback execution ending memory usage. |
193
|
19 |
|
* |
194
|
|
|
* @param float $endMemory |
195
|
|
|
* |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function setEndMemory($endMemory) |
199
|
|
|
{ |
200
|
|
|
$this->endMemory = $endMemory; |
201
|
2 |
|
|
202
|
|
|
return $this; |
203
|
2 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the memory spike amount of the callback. |
207
|
|
|
* |
208
|
|
|
* @return float |
209
|
|
|
*/ |
210
|
|
|
public function getMemory() |
211
|
3 |
|
{ |
212
|
|
|
return $this->endMemory - $this->startMemory; |
213
|
3 |
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get the exception thrown by the callback. |
217
|
|
|
* |
218
|
|
|
* @return Exception|null |
219
|
|
|
*/ |
220
|
|
|
public function getException() |
221
|
|
|
{ |
222
|
|
|
return $this->exception; |
223
|
3 |
|
} |
224
|
|
|
|
225
|
3 |
|
/** |
226
|
|
|
* Set the exception thrown by the callback. |
227
|
3 |
|
* |
228
|
|
|
* @param Exception|null $exception |
229
|
|
|
* |
230
|
|
|
* @return $this |
231
|
|
|
*/ |
232
|
|
|
public function setException($exception) |
233
|
|
|
{ |
234
|
|
|
$this->exception = $exception; |
235
|
6 |
|
|
236
|
|
|
return $this; |
237
|
6 |
|
} |
238
|
|
|
|
239
|
|
|
public function getContext() |
240
|
|
|
{ |
241
|
|
|
return $this->context; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Determine whether the callback result matches the control. |
246
|
|
|
* |
247
|
4 |
|
* @return boolean |
248
|
|
|
*/ |
249
|
4 |
|
public function isMatch() |
250
|
|
|
{ |
251
|
4 |
|
return $this->match; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set whether the callback result matches the control. |
256
|
|
|
* |
257
|
|
|
* @param boolean $match |
258
|
|
|
* |
259
|
|
|
* @return $this |
260
|
|
|
*/ |
261
|
|
|
public function setMatch($match) |
262
|
|
|
{ |
263
|
|
|
$this->match = $match; |
264
|
|
|
|
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|