1 | |
package org.apache.maven.plugin.surefire.runorder; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.List; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class ThreadedExecutionScheduler |
29 | |
{ |
30 | |
private final int numThreads; |
31 | |
|
32 | |
private final int runTime[]; |
33 | |
|
34 | |
private final List[] lists; |
35 | |
|
36 | |
public ThreadedExecutionScheduler( int numThreads ) |
37 | 2 | { |
38 | 2 | this.numThreads = numThreads; |
39 | 2 | runTime = new int[numThreads]; |
40 | 2 | lists = new List[numThreads]; |
41 | 8 | for ( int i = 0; i < numThreads; i++ ) |
42 | |
{ |
43 | 6 | lists[i] = new ArrayList(); |
44 | |
} |
45 | 2 | } |
46 | |
|
47 | |
public void addTest( PrioritizedTest prioritizedTest ) |
48 | |
{ |
49 | 10 | final int leastBusySlot = findLeastBusySlot(); |
50 | 10 | runTime[leastBusySlot] += prioritizedTest.getTotalRuntime(); |
51 | 10 | lists[leastBusySlot].add( prioritizedTest.getClazz() ); |
52 | 10 | } |
53 | |
|
54 | |
public List getResult() |
55 | |
{ |
56 | 2 | List result = new ArrayList(); |
57 | 2 | int index = 0; |
58 | 2 | boolean added = false; |
59 | |
do |
60 | |
{ |
61 | 7 | added = false; |
62 | 27 | for ( int i = 0; i < numThreads; i++ ) |
63 | |
{ |
64 | 20 | if ( lists[i].size() > index ) |
65 | |
{ |
66 | 10 | result.add( lists[i].get( index ) ); |
67 | 10 | added = true; |
68 | |
} |
69 | |
} |
70 | 7 | index++; |
71 | |
} |
72 | 7 | while ( added ); |
73 | 2 | return result; |
74 | |
} |
75 | |
|
76 | |
private int findLeastBusySlot() |
77 | |
{ |
78 | 10 | int leastBusy = 0; |
79 | 10 | int minRuntime = runTime[0]; |
80 | 30 | for ( int i = 1; i < numThreads; i++ ) |
81 | |
{ |
82 | 20 | if ( runTime[i] < minRuntime ) |
83 | |
{ |
84 | 12 | leastBusy = i; |
85 | 12 | minRuntime = runTime[i]; |
86 | |
} |
87 | |
} |
88 | 10 | return leastBusy; |
89 | |
} |
90 | |
} |