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 | |
|
22 | |
|
23 | |
import org.apache.maven.surefire.report.ReportEntry; |
24 | |
|
25 | |
import java.io.BufferedReader; |
26 | |
import java.io.File; |
27 | |
import java.io.FileNotFoundException; |
28 | |
import java.io.FileOutputStream; |
29 | |
import java.io.FileReader; |
30 | |
import java.io.IOException; |
31 | |
import java.io.PrintWriter; |
32 | |
import java.io.Reader; |
33 | |
import java.util.ArrayList; |
34 | |
import java.util.Collections; |
35 | |
import java.util.Comparator; |
36 | |
import java.util.HashMap; |
37 | |
import java.util.List; |
38 | |
import java.util.Map; |
39 | |
import java.util.regex.Matcher; |
40 | |
import java.util.regex.Pattern; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class RunEntryStatisticsMap |
46 | |
{ |
47 | |
private final Map<String, RunEntryStatistics> runEntryStatistics; |
48 | |
|
49 | |
public RunEntryStatisticsMap( Map<String, RunEntryStatistics> runEntryStatistics ) |
50 | 0 | { |
51 | 0 | this.runEntryStatistics = Collections.synchronizedMap( runEntryStatistics ); |
52 | 0 | } |
53 | |
|
54 | |
public RunEntryStatisticsMap() |
55 | |
{ |
56 | 0 | this( new HashMap<String, RunEntryStatistics>() ); |
57 | 0 | } |
58 | |
|
59 | |
public static RunEntryStatisticsMap fromFile( File file ) |
60 | |
{ |
61 | 0 | if ( file.exists() ) |
62 | |
{ |
63 | |
try |
64 | |
{ |
65 | 0 | FileReader fileReader = new FileReader( file ); |
66 | 0 | return fromReader( fileReader ); |
67 | |
} |
68 | 0 | catch ( FileNotFoundException e ) |
69 | |
{ |
70 | 0 | throw new RuntimeException( e ); |
71 | |
} |
72 | 0 | catch ( IOException e1 ) |
73 | |
{ |
74 | 0 | throw new RuntimeException( e1 ); |
75 | |
} |
76 | |
|
77 | |
} |
78 | 0 | return new RunEntryStatisticsMap(); |
79 | |
} |
80 | |
|
81 | |
static RunEntryStatisticsMap fromReader( Reader fileReader ) |
82 | |
throws IOException |
83 | |
{ |
84 | 0 | Map<String, RunEntryStatistics> result = new HashMap<String, RunEntryStatistics>(); |
85 | 0 | BufferedReader bufferedReader = new BufferedReader( fileReader ); |
86 | 0 | String line = bufferedReader.readLine(); |
87 | 0 | while ( line != null ) |
88 | |
{ |
89 | 0 | if ( !line.startsWith( "#" ) ) |
90 | |
{ |
91 | 0 | final RunEntryStatistics stats = RunEntryStatistics.fromString( line ); |
92 | 0 | result.put( stats.getTestName(), stats ); |
93 | |
} |
94 | 0 | line = bufferedReader.readLine(); |
95 | |
} |
96 | 0 | return new RunEntryStatisticsMap( result ); |
97 | |
} |
98 | |
|
99 | |
public void serialize( File file ) |
100 | |
throws FileNotFoundException |
101 | |
{ |
102 | 0 | FileOutputStream fos = new FileOutputStream( file ); |
103 | 0 | PrintWriter printWriter = new PrintWriter( fos ); |
104 | 0 | List<RunEntryStatistics> items = new ArrayList<RunEntryStatistics>( runEntryStatistics.values() ); |
105 | 0 | Collections.sort( items, new RunCountComparator() ); |
106 | |
RunEntryStatistics item; |
107 | 0 | for ( RunEntryStatistics item1 : items ) |
108 | |
{ |
109 | 0 | item = item1; |
110 | 0 | printWriter.println( item.getAsString() ); |
111 | 0 | } |
112 | 0 | printWriter.close(); |
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
public RunEntryStatistics findOrCreate( ReportEntry reportEntry ) |
117 | |
{ |
118 | 0 | final RunEntryStatistics item = runEntryStatistics.get( reportEntry.getName() ); |
119 | 0 | return item != null ? item : RunEntryStatistics.fromReportEntry( reportEntry ); |
120 | |
} |
121 | |
|
122 | |
public RunEntryStatistics createNextGeneration( ReportEntry reportEntry ) |
123 | |
{ |
124 | 0 | final RunEntryStatistics newItem = findOrCreate( reportEntry ); |
125 | 0 | final Integer elapsed = reportEntry.getElapsed(); |
126 | 0 | return newItem.nextGeneration( elapsed != null ? elapsed : 0 ); |
127 | |
} |
128 | |
|
129 | |
public RunEntryStatistics createNextGenerationFailure( ReportEntry reportEntry ) |
130 | |
{ |
131 | 0 | final RunEntryStatistics newItem = findOrCreate( reportEntry ); |
132 | 0 | final Integer elapsed = reportEntry.getElapsed(); |
133 | 0 | return newItem.nextGenerationFailure( elapsed != null ? elapsed : 0 ); |
134 | |
} |
135 | |
|
136 | |
public void add( RunEntryStatistics item ) |
137 | |
{ |
138 | 0 | runEntryStatistics.put( item.getTestName(), item ); |
139 | 0 | } |
140 | |
|
141 | 0 | class RunCountComparator |
142 | |
implements Comparator<RunEntryStatistics> |
143 | |
{ |
144 | |
public int compare( RunEntryStatistics o, RunEntryStatistics o1 ) |
145 | |
{ |
146 | 0 | int runtime = o.getSuccessfulBuilds() - o1.getSuccessfulBuilds(); |
147 | 0 | if ( runtime == 0 ) |
148 | |
{ |
149 | 0 | return o.getRunTime() - o1.getRunTime(); |
150 | |
} |
151 | 0 | return runtime; |
152 | |
} |
153 | |
} |
154 | |
|
155 | |
public List<Class> getPrioritizedTestsClassRunTime( List testsToRun, int threadCount ) |
156 | |
{ |
157 | 0 | final List<PrioritizedTest> prioritizedTests = getPrioritizedTests( testsToRun, new TestRuntimeComparator() ); |
158 | 0 | ThreadedExecutionScheduler threadedExecutionScheduler = new ThreadedExecutionScheduler( threadCount ); |
159 | 0 | for ( Object prioritizedTest1 : prioritizedTests ) |
160 | |
{ |
161 | 0 | threadedExecutionScheduler.addTest( (PrioritizedTest) prioritizedTest1 ); |
162 | 0 | } |
163 | |
|
164 | 0 | return threadedExecutionScheduler.getResult(); |
165 | |
|
166 | |
} |
167 | |
|
168 | |
public List<Class> getPrioritizedTestsByFailureFirst( List testsToRun ) |
169 | |
{ |
170 | 0 | final List prioritizedTests = getPrioritizedTests( testsToRun, new LeastFailureComparator() ); |
171 | 0 | return transformToClasses( prioritizedTests ); |
172 | |
} |
173 | |
|
174 | |
|
175 | |
private List<PrioritizedTest> getPrioritizedTests( List testsToRun, Comparator<Priority> priorityComparator ) |
176 | |
{ |
177 | 0 | Map classPriorities = getPriorities( priorityComparator ); |
178 | |
|
179 | 0 | List<PrioritizedTest> tests = new ArrayList<PrioritizedTest>(); |
180 | 0 | for ( Object aTestsToRun : testsToRun ) |
181 | |
{ |
182 | 0 | Class clazz = (Class) aTestsToRun; |
183 | 0 | Priority pri = (Priority) classPriorities.get( clazz.getName() ); |
184 | 0 | if ( pri == null ) |
185 | |
{ |
186 | 0 | pri = Priority.newTestClassPriority( clazz.getName() ); |
187 | |
} |
188 | 0 | PrioritizedTest prioritizedTest = new PrioritizedTest( clazz, pri ); |
189 | 0 | tests.add( prioritizedTest ); |
190 | 0 | } |
191 | 0 | Collections.sort( tests, new PrioritizedTestComparator() ); |
192 | 0 | return tests; |
193 | |
|
194 | |
} |
195 | |
|
196 | |
private List<Class> transformToClasses( List tests ) |
197 | |
{ |
198 | 0 | List<Class> result = new ArrayList<Class>(); |
199 | 0 | for ( Object test : tests ) |
200 | |
{ |
201 | 0 | result.add( ( (PrioritizedTest) test ).getClazz() ); |
202 | 0 | } |
203 | 0 | return result; |
204 | |
} |
205 | |
|
206 | |
public Map getPriorities( Comparator<Priority> priorityComparator ) |
207 | |
{ |
208 | 0 | Map<String, Priority> priorities = new HashMap<String, Priority>(); |
209 | 0 | for ( Object o : runEntryStatistics.keySet() ) |
210 | |
{ |
211 | 0 | String testNames = (String) o; |
212 | 0 | String clazzName = extractClassName( testNames ); |
213 | 0 | Priority priority = priorities.get( clazzName ); |
214 | 0 | if ( priority == null ) |
215 | |
{ |
216 | 0 | priority = new Priority( clazzName ); |
217 | 0 | priorities.put( clazzName, priority ); |
218 | |
} |
219 | |
|
220 | 0 | RunEntryStatistics itemStat = runEntryStatistics.get( testNames ); |
221 | 0 | priority.addItem( itemStat ); |
222 | 0 | } |
223 | |
|
224 | 0 | List<Priority> items = new ArrayList<Priority>( priorities.values() ); |
225 | 0 | Collections.sort( items, priorityComparator ); |
226 | 0 | Map<String, Priority> result = new HashMap<String, Priority>(); |
227 | 0 | int i = 0; |
228 | 0 | for ( Priority pri : items ) |
229 | |
{ |
230 | 0 | pri.setPriority( i++ ); |
231 | 0 | result.put( pri.getClassName(), pri ); |
232 | 0 | } |
233 | 0 | return result; |
234 | |
} |
235 | |
|
236 | 0 | class PrioritizedTestComparator |
237 | |
implements Comparator<PrioritizedTest> |
238 | |
{ |
239 | |
public int compare( PrioritizedTest o, PrioritizedTest o1 ) |
240 | |
{ |
241 | 0 | return o.getPriority() - o1.getPriority(); |
242 | |
} |
243 | |
} |
244 | |
|
245 | 0 | class TestRuntimeComparator |
246 | |
implements Comparator<Priority> |
247 | |
{ |
248 | |
public int compare( Priority o, Priority o1 ) |
249 | |
{ |
250 | 0 | return o1.getTotalRuntime() - o.getTotalRuntime(); |
251 | |
} |
252 | |
} |
253 | |
|
254 | 0 | class LeastFailureComparator |
255 | |
implements Comparator<Priority> |
256 | |
{ |
257 | |
public int compare( Priority o, Priority o1 ) |
258 | |
{ |
259 | 0 | return o.getMinSuccessRate() - o1.getMinSuccessRate(); |
260 | |
} |
261 | |
} |
262 | |
|
263 | |
|
264 | 0 | private static final Pattern PARENS = Pattern.compile( "^" + "[^\\(\\)]+" |
265 | |
+ "\\((" |
266 | |
+ "[^\\\\(\\\\)]+" |
267 | |
+ ")\\)" + "$" ); |
268 | |
|
269 | |
String extractClassName( String displayName ) |
270 | |
{ |
271 | 0 | Matcher m = PARENS.matcher( displayName ); |
272 | 0 | if ( !m.find() ) |
273 | |
{ |
274 | 0 | return displayName; |
275 | |
} |
276 | 0 | return m.group( 1 ); |
277 | |
} |
278 | |
|
279 | |
|
280 | |
} |