-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
TestLocationBucketSorterTest.php
273 lines (232 loc) · 8.96 KB
/
TestLocationBucketSorterTest.php
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
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
declare(strict_types=1);
namespace Infection\Tests\TestFramework\Coverage\JUnit;
use function abs;
use function array_map;
use function array_reverse;
use function array_slice;
use ArrayIterator;
use function extension_loaded;
use Infection\AbstractTestFramework\Coverage\TestLocation;
use Infection\TestFramework\Coverage\JUnit\JUnitTestCaseSorter;
use Infection\TestFramework\Coverage\JUnit\TestLocationBucketSorter;
use Infection\Tests\Fixtures\TestFramework\PhpUnit\Coverage\JUnitTimes;
use function iterator_to_array;
use function log;
use function microtime;
use const PHP_SAPI;
use PHPUnit\Framework\TestCase;
use function Safe\usort;
/**
* Tagged as integration because it can be quite slow.
*
* @group integration
*/
final class TestLocationBucketSorterTest extends TestCase
{
/**
* Used for floating point comparisons.
*
* @var float
*/
private const EPSILON = 0.001;
public function test_it_sorts(): void
{
$testLocation = new TestLocation('', '', 0.0);
$sortedTestLocations = iterator_to_array(
TestLocationBucketSorter::bucketSort([$testLocation]),
false
);
$this->assertSame([$testLocation], $sortedTestLocations);
}
public function test_it_detects_precision_boundary(): void
{
$testLocations = [
new TestLocation('', '', 0.124),
new TestLocation('', '', 0.125),
new TestLocation('', '', 0.499),
new TestLocation('', '', 0.500),
new TestLocation('', '', 1.499),
new TestLocation('', '', 1.500),
new TestLocation('', '', 3.999),
new TestLocation('', '', 4.000),
];
$sortedTestLocations = iterator_to_array(
TestLocationBucketSorter::bucketSort(array_reverse($testLocations)),
false
);
$this->assertSame($testLocations, $sortedTestLocations);
}
public function test_it_detects_second_precision_boundary(): void
{
$testLocation1 = new TestLocation('', '', 0.124);
$testLocation2 = new TestLocation('', '', 0.125);
$sortedTestLocations = iterator_to_array(
TestLocationBucketSorter::bucketSort([$testLocation2, $testLocation1]),
false
);
$this->assertSame([$testLocation1, $testLocation2], $sortedTestLocations);
}
/**
* @dataProvider locationsArrayProvider
*
* @param ArrayIterator<TestLocation> $uniqueTestLocations
*/
public function test_it_sorts_correctly(ArrayIterator $uniqueTestLocations): void
{
$uniqueTestLocations = $uniqueTestLocations->getArrayCopy();
$sortedTestLocations = iterator_to_array(
TestLocationBucketSorter::bucketSort($uniqueTestLocations),
false
);
$this->assertTrue(
self::areConstraintsOrderValid($sortedTestLocations),
'Bucket sort failed order check'
);
}
/**
* Sanity check
*
* @dataProvider locationsArrayProvider
*
* @param ArrayIterator<TestLocation> $uniqueTestLocations
*/
public function test_quicksort_sorts_correctly(ArrayIterator $uniqueTestLocations): void
{
$uniqueTestLocations = $uniqueTestLocations->getArrayCopy();
self::quicksort($uniqueTestLocations);
$this->assertTrue(
self::areConstraintsOrderValid($uniqueTestLocations),
'Quicksort failed order check'
);
}
/**
* @dataProvider locationsArrayProvider
*
* @param ArrayIterator<TestLocation> $uniqueTestLocations
*/
public function test_it_sorts_faster_than_quicksort(ArrayIterator $uniqueTestLocations): void
{
if (extension_loaded('xdebug') || PHP_SAPI === 'phpdbg') {
$this->markTestSkipped('Benchmarks under xdebug or phpdbg are brittle');
}
$uniqueTestLocations = $uniqueTestLocations->getArrayCopy();
if (self::areConstraintsOrderValid($uniqueTestLocations)) {
// Ignore silently as to not pollute to the log.
$this->addToAssertionCount(1);
return;
}
$tries = 100;
// Benchmark bucket sort
$totalBucketSort = 0;
for ($i = 0; $i < $tries; ++$i) {
$start = microtime(true);
$dummy = iterator_to_array(
TestLocationBucketSorter::bucketSort($uniqueTestLocations),
false
);
$totalBucketSort += microtime(true) - $start;
}
// Benchmark quicksort
$totalQuickSort = 0;
for ($i = 0; $i < $tries; ++$i) {
$start = microtime(true);
// Updates by reference
$locationsCopy = $uniqueTestLocations;
self::quicksort($locationsCopy);
$totalQuickSort += microtime(true) - $start;
}
$this->assertGreaterThanOrEqual(self::EPSILON, self::getRelativeError($totalQuickSort, $totalBucketSort));
}
public static function locationsArrayProvider(): iterable
{
$locations = array_map(
static function (float $executionTime): TestLocation {
return new TestLocation('', '', $executionTime);
},
JUnitTimes::JUNIT_TIMES
);
yield 'Ten times the minimal amount of locations' => [new ArrayIterator(array_slice($locations, 0, JUnitTestCaseSorter::USE_BUCKET_SORT_AFTER * 10))];
yield 'All locations' => [new ArrayIterator($locations)];
}
/**
* Finds relative error.
*
* @see https://floating-point-gui.de/errors/comparison/
* @see https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison
*/
private static function getRelativeError(float $a, float $b): float
{
// We do not expect A or B to be extremely small or large: these are edge cases,
// and they will need special handling which we avoid simplicity sake.
self::assertGreaterThan(self::EPSILON, abs($a));
self::assertGreaterThan(self::EPSILON, abs($b));
return abs($a - $b) / (abs($a) + abs($b));
}
private static function quicksort(&$uniqueTestLocations): void
{
usort(
$uniqueTestLocations,
static function (TestLocation $a, TestLocation $b) {
return $a->getExecutionTime() <=> $b->getExecutionTime();
}
);
}
/**
* We assume locations should be ordered within an order of magnitude.
*
* @param TestLocation[] $sortedTestLocations
*/
private static function areConstraintsOrderValid(array $sortedTestLocations): bool
{
// Minimal precision: there's no sort below this number
$minimalPrecisionTime = 0.125;
$lastSeenTime = null;
foreach ($sortedTestLocations as $location) {
if ($lastSeenTime === null) {
// Don't enable checks unless a to-be-sorted value is seen
if ($location->getExecutionTime() > $minimalPrecisionTime) {
$lastSeenTime = $location->getExecutionTime();
}
continue;
}
// Previously seen time must not be 10 times as large as current
if ($lastSeenTime / $location->getExecutionTime() > 10.) {
return false;
}
$lastSeenTime = $location->getExecutionTime();
}
return true;
}
}