32
32
import java .util .Queue ;
33
33
import java .util .concurrent .ConcurrentLinkedQueue ;
34
34
import java .util .concurrent .CountDownLatch ;
35
+ import java .util .concurrent .atomic .AtomicInteger ;
35
36
import java .util .concurrent .atomic .AtomicLong ;
36
37
import java .util .function .Predicate ;
37
38
import java .util .regex .Pattern ;
38
39
import java .util .stream .Stream ;
39
40
40
41
import org .moditect .jfrunit .EnableEvent .StacktracePolicy ;
42
+ import org .moditect .jfrunit .internal .JmcAutomaticAnalysis ;
41
43
import org .moditect .jfrunit .internal .SyncEvent ;
44
+ import org .openjdk .jmc .flightrecorder .rules .IResult ;
42
45
import org .openjdk .jmc .flightrecorder .rules .Severity ;
43
46
44
47
import jdk .jfr .Configuration ;
@@ -64,7 +67,7 @@ public class JfrEvents {
64
67
private Recording recording ;
65
68
private boolean capturing ;
66
69
67
- private JfrAnalysisResults analysis = null ;
70
+ private AtomicInteger analysisCounter = new AtomicInteger ( 0 ) ;
68
71
69
72
public JfrEvents () {
70
73
}
@@ -138,6 +141,10 @@ void stopRecordingEvents() {
138
141
}
139
142
140
143
private Path getRecordingFilePath () throws URISyntaxException , IOException {
144
+ return getRecordingFilePath (null );
145
+ }
146
+
147
+ private Path getRecordingFilePath (String suffix ) throws URISyntaxException , IOException {
141
148
URI testSourceUri = testMethod .getDeclaringClass ().getProtectionDomain ().getCodeSource ().getLocation ().toURI ();
142
149
Path dumpDir ;
143
150
try {
@@ -148,7 +155,7 @@ private Path getRecordingFilePath() throws URISyntaxException, IOException {
148
155
dumpDir = Files .createTempDirectory (null );
149
156
LOGGER .log (Level .WARNING , "'" + testSourceUri .getScheme () + "' is not a valid file system, dumping recording to a temporary location." );
150
157
}
151
- String fileName = getDumpFileName ();
158
+ String fileName = getDumpFileName (suffix );
152
159
return dumpDir .resolve (fileName );
153
160
}
154
161
@@ -314,31 +321,39 @@ private List<EventConfiguration> matchEventTypes(List<EventConfiguration> enable
314
321
return allEvents ;
315
322
}
316
323
317
- private String getDumpFileName () {
324
+ private String getDumpFileName (String suffix ) {
318
325
if (dumpFileName == null ) {
319
- return getDefaultDumpFileName ();
326
+ return getDefaultDumpFileName (suffix );
320
327
}
321
328
else {
322
329
return dumpFileName .endsWith (".jfr" ) ? dumpFileName : dumpFileName + ".jfr" ;
323
330
}
324
331
}
325
332
326
333
private String getDefaultDumpFileName () {
327
- return testMethod . getDeclaringClass (). getName () + "-" + testMethod . getName () + ".jfr" ;
334
+ return getDefaultDumpFileName ( null ) ;
328
335
}
329
336
330
- public JfrAnalysisResults automaticAnalysis () {
331
- if (analysis == null ) {
332
- try {
333
- Path recordingPath = getRecordingFilePath ();
334
- dumpRecording (recordingPath );
337
+ private String getDefaultDumpFileName (String suffix ) {
338
+ return testMethod .getDeclaringClass ().getName () + "-" + testMethod .getName () + (suffix != null ? "-" + suffix : "" ) + ".jfr" ;
339
+ }
335
340
336
- analysis = new JfrAnalysisResults (JfrAnalysis .analysisRecording (recordingPath .toAbsolutePath ().toString (), Severity .INFO ));
337
- }
338
- catch (IOException | URISyntaxException e ) {
339
- LOGGER .log (Level .WARNING , "Unable to analyse jfr recording: " + e .getLocalizedMessage ());
340
- }
341
+ // TODO: Do we move out of JfrEvents?
342
+ public List <IResult > automaticAnalysis () {
343
+ try {
344
+ awaitEvents ();
345
+
346
+ int counter = analysisCounter .getAndIncrement ();
347
+ Path recordingPath = getRecordingFilePath ("analysis" + (counter != 0 ? "-" + counter : "" ));
348
+
349
+ LOGGER .log (Level .INFO , "Analysis recording: " + recordingPath .toAbsolutePath ());
350
+ dumpRecording (recordingPath );
351
+
352
+ return JmcAutomaticAnalysis .analysisRecording (recordingPath .toAbsolutePath ().toString (), Severity .INFO );
353
+
354
+ }
355
+ catch (IOException | URISyntaxException e ) {
356
+ throw new RuntimeException ("Unable to analyse jfr recording" , e );
341
357
}
342
- return analysis ;
343
358
}
344
359
}
0 commit comments