| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace MediaWiki\Composer\PhpUnitSplitter; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class TestSuiteBuilder { |
| 11 |
|
| 12 |
private static function sortByTimeDescending( TestDescriptor $a, TestDescriptor $b ): int { |
| 13 |
if ( $a->getDuration() === $b->getDuration() ) { |
| 14 |
return 0; |
| 15 |
} |
| 16 |
return ( $a->getDuration() > $b->getDuration() ? -1 : 1 ); |
| 17 |
} |
| 18 |
|
| 19 |
private static function smallestGroup( array $suites ): int { |
| 20 |
$min = 10000; |
| 21 |
$minIndex = 0; |
| 22 |
$groups = count( $suites ); |
| 23 |
for ( $i = 0; $i < $groups; $i++ ) { |
| 24 |
if ( $suites[$i]["time"] < $min ) { |
| 25 |
$min = $suites[$i]["time"]; |
| 26 |
$minIndex = $i; |
| 27 |
} |
| 28 |
} |
| 29 |
return $minIndex; |
| 30 |
} |
| 31 |
|
| 32 |
public function buildSuites( array $testDescriptors, int $groups ): array { |
| 33 |
$suites = array_fill( 0, $groups, [ "list" => [], "time" => 0 ] ); |
| 34 |
$roundRobin = 0; |
| 35 |
usort( $testDescriptors, [ self::class, "sortByTimeDescending" ] ); |
| 36 |
foreach ( $testDescriptors as $testDescriptor ) { |
| 37 |
if ( !$testDescriptor->getFilename() ) { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
continue; |
| 44 |
} |
| 45 |
if ( $testDescriptor->getDuration() === 0 ) { |
| 46 |
|
| 47 |
|
| 48 |
$nextSuite = $roundRobin; |
| 49 |
$roundRobin = ( $roundRobin + 1 ) % $groups; |
| 50 |
} else { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
$nextSuite = self::smallestGroup( $suites ); |
| 55 |
} |
| 56 |
$suites[$nextSuite]["list"][] = $testDescriptor->getFilename(); |
| 57 |
$suites[$nextSuite]["time"] += $testDescriptor->getDuration(); |
| 58 |
} |
| 59 |
return $suites; |
| 60 |
} |
| 61 |
} |