-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspeed-trap-listener.php
More file actions
306 lines (268 loc) · 7.25 KB
/
speed-trap-listener.php
File metadata and controls
306 lines (268 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* A PHPUnit TestListener that exposes your slowest running tests by outputting
* results directly to the console.
*/
class SpeedTrapListener implements PHPUnit_Framework_TestListener {
/**
* Internal tracking for test suites.
*
* Increments as more suites are run, then decremented as they finish. All
* suites have been run when returns to 0.
*
* @var integer
*/
protected $suites = 0;
/**
* Time in milliseconds at which a test will be considered "slow" and be
* reported by this listener.
*
* @var int
*/
protected $slow_threshold;
/**
* Number of tests to report on for slowness.
*
* @var int
*/
protected $report_length;
/**
* Collection of slow tests.
*
* @var array
*/
protected $slow = array();
/**
* Construct a new instance.
*
* @param array $options
*/
public function __construct( array $options = array() ) {
$this->loadOptions( $options );
}
/**
* An error occurred.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addError( PHPUnit_Framework_Test $test, Exception $e, $time ) {
}
/**
* A warning occurred.
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_Warning $e
* @param float $time
* @since Method available since Release 5.1.0
*/
public function addWarning( PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time ) {
}
/**
* A failure occurred.
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_AssertionFailedError $e
* @param float $time
*/
public function addFailure( PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time ) {
}
/**
* Incomplete test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addIncompleteTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
}
/**
* Risky test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
* @since Method available since Release 4.0.0
*/
public function addRiskyTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
}
/**
* Skipped test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addSkippedTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
}
/**
* A test started.
*
* @param PHPUnit_Framework_Test $test
*/
public function startTest( PHPUnit_Framework_Test $test ) {
}
/**
* A test ended.
*
* @param PHPUnit_Framework_Test $test
* @param float $time
*/
public function endTest( PHPUnit_Framework_Test $test, $time ) {
if ( ! $test instanceof PHPUnit_Framework_TestCase ) {
return;
}
$time = $this->toMilliseconds( $time );
$threshold = $this->getSlowThreshold( $test );
if ( $this->isSlow( $time, $threshold ) ) {
$this->addSlowTest( $test, $time );
}
}
/**
* A test suite started.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function startTestSuite( PHPUnit_Framework_TestSuite $suite ) {
$this->suites++;
}
/**
* A test suite ended.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function endTestSuite( PHPUnit_Framework_TestSuite $suite ) {
$this->suites--;
if ( 0 === $this->suites && $this->hasSlowTests() ) {
arsort( $this->slow ); // Sort longest running tests to the top.
$this->renderHeader();
$this->renderBody();
$this->renderFooter();
}
}
/**
* Whether the given test execution time is considered slow.
*
* @param int $time Test execution time in milliseconds
* @param int $slow_threshold Test execution time at which a test should be considered slow (milliseconds)
* @return bool
*/
protected function isSlow( $time, $slow_threshold ) {
return $time >= $slow_threshold;
}
/**
* Stores a test as slow.
*
* @param PHPUnit_Framework_TestCase $test
* @param int $time Test execution time in milliseconds
*/
protected function addSlowTest( PHPUnit_Framework_TestCase $test, $time ) {
$label = $this->makeLabel( $test );
$this->slow[ $label ] = $time;
}
/**
* Whether at least one test has been considered slow.
*
* @return bool
*/
protected function hasSlowTests() {
return ! empty( $this->slow );
}
/**
* Convert PHPUnit's reported test time (microseconds) to milliseconds.
*
* @param float $time
* @return int
*/
protected function toMilliseconds( $time ) {
return (int) round( $time * 1000 );
}
/**
* Label for describing a test.
*
* @param PHPUnit_Framework_TestCase $test
* @return string
*/
protected function makeLabel( PHPUnit_Framework_TestCase $test ) {
return sprintf( '%s:%s', get_class( $test ), $test->getName() );
}
/**
* Calculate number of slow tests to report about.
*
* @return int
*/
protected function getReportLength() {
return min( count( $this->slow ), $this->report_length );
}
/**
* Find how many slow tests occurred that won't be shown due to list length.
*
* @return int Number of hidden slow tests
*/
protected function getHiddenCount() {
$total = count( $this->slow );
$showing = $this->getReportLength( $this->slow );
$hidden = 0;
if ( $total > $showing ) {
$hidden = $total - $showing;
}
return $hidden;
}
/**
* Renders slow test report header.
*/
protected function renderHeader() {
echo sprintf( "\n\nYou should really fix these slow tests (>%sms)...\n", $this->slow_threshold );
}
/**
* Renders slow test report body.
*/
protected function renderBody() {
$slow_tests = $this->slow;
$length = $this->getReportLength( $slow_tests );
for ( $i = 1; $i <= $length; ++$i ) {
$label = key( $slow_tests );
$time = array_shift( $slow_tests );
echo sprintf( " %s. %sms to run %s\n", $i, $time, $label );
}
}
/**
* Renders slow test report footer.
*/
protected function renderFooter() {
if ( $hidden = $this->getHiddenCount( $this->slow ) ) {
echo sprintf( '...and there %s %s more above your threshold hidden from view', 1 === $hidden ? 'is' : 'are', $hidden );
}
}
/**
* Populate options into class internals.
*
* @param array $options
*/
protected function loadOptions( array $options ) {
$this->slow_threshold = isset( $options['slowThreshold'] ) ? $options['slowThreshold'] : 500;
$this->report_length = isset( $options['reportLength'] ) ? $options['reportLength'] : 10;
}
/**
* Get slow test threshold for given test. A TestCase can override the
* suite-wide slow threshold by using the annotation @slowThreshold with
* the threshold value in milliseconds.
*
* The following test will only be considered slow when its execution time
* reaches 5000ms (5 seconds):
*
* <code>
*
* @slowThreshold 5000
* public function testLongRunningProcess() {}
* </code>
*
* @param PHPUnit_Framework_TestCase $test
* @return int
*/
protected function getSlowThreshold( PHPUnit_Framework_TestCase $test ) {
$ann = $test->getAnnotations();
return isset( $ann['method']['slowThreshold'][0] ) ? $ann['method']['slowThreshold'][0] : $this->slow_threshold;
}
}