3232import java .util .Queue ;
3333import java .util .concurrent .ConcurrentLinkedQueue ;
3434import java .util .concurrent .CountDownLatch ;
35+ import java .util .concurrent .atomic .AtomicInteger ;
3536import java .util .concurrent .atomic .AtomicLong ;
3637import java .util .function .Predicate ;
3738import java .util .regex .Pattern ;
3839import java .util .stream .Stream ;
3940
4041import org .moditect .jfrunit .EnableEvent .StacktracePolicy ;
42+ import org .moditect .jfrunit .internal .JmcAutomaticAnalysis ;
4143import org .moditect .jfrunit .internal .SyncEvent ;
44+ import org .openjdk .jmc .flightrecorder .rules .IResult ;
4245import org .openjdk .jmc .flightrecorder .rules .Severity ;
4346
4447import jdk .jfr .Configuration ;
@@ -64,7 +67,7 @@ public class JfrEvents {
6467 private Recording recording ;
6568 private boolean capturing ;
6669
67- private JfrAnalysisResults analysis = null ;
70+ private AtomicInteger analysisCounter = new AtomicInteger ( 0 ) ;
6871
6972 public JfrEvents () {
7073 }
@@ -138,6 +141,10 @@ void stopRecordingEvents() {
138141 }
139142
140143 private Path getRecordingFilePath () throws URISyntaxException , IOException {
144+ return getRecordingFilePath (null );
145+ }
146+
147+ private Path getRecordingFilePath (String suffix ) throws URISyntaxException , IOException {
141148 URI testSourceUri = testMethod .getDeclaringClass ().getProtectionDomain ().getCodeSource ().getLocation ().toURI ();
142149 Path dumpDir ;
143150 try {
@@ -148,7 +155,7 @@ private Path getRecordingFilePath() throws URISyntaxException, IOException {
148155 dumpDir = Files .createTempDirectory (null );
149156 LOGGER .log (Level .WARNING , "'" + testSourceUri .getScheme () + "' is not a valid file system, dumping recording to a temporary location." );
150157 }
151- String fileName = getDumpFileName ();
158+ String fileName = getDumpFileName (suffix );
152159 return dumpDir .resolve (fileName );
153160 }
154161
@@ -314,31 +321,39 @@ private List<EventConfiguration> matchEventTypes(List<EventConfiguration> enable
314321 return allEvents ;
315322 }
316323
317- private String getDumpFileName () {
324+ private String getDumpFileName (String suffix ) {
318325 if (dumpFileName == null ) {
319- return getDefaultDumpFileName ();
326+ return getDefaultDumpFileName (suffix );
320327 }
321328 else {
322329 return dumpFileName .endsWith (".jfr" ) ? dumpFileName : dumpFileName + ".jfr" ;
323330 }
324331 }
325332
326333 private String getDefaultDumpFileName () {
327- return testMethod . getDeclaringClass (). getName () + "-" + testMethod . getName () + ".jfr" ;
334+ return getDefaultDumpFileName ( null ) ;
328335 }
329336
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+ }
335340
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 );
341357 }
342- return analysis ;
343358 }
344359}
0 commit comments