Skip to content

Commit 03a9c07

Browse files
committed
- Corrected README
- Renamed JfrAnalysis -> JmcAutomaticAnalysis - Moved JmcAutomaticAnalysis to internal package - Separated tests for Automated Analysis - Add to JmcAutomaticAnalysisAssert api to assert based on IReport severity - Generate new JFR dump for each analysis - Removed JfrAnalysisResults, replaced with List<IResult>
1 parent 2b4a3f9 commit 03a9c07

File tree

8 files changed

+330
-247
lines changed

8 files changed

+330
-247
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ Java Mission Control provides an Automatic Analysis tool that performs pattern a
143143
It is possible to write assertions against the Automatic Analysis results to verify that unit tests against common performance issues:
144144

145145
```java
146-
import dev.morling.jfrunit.*;
146+
import org.moditect.jfrunit.*;
147147

148-
import static dev.morling.jfrunit.JfrEventsAssert.*;
149-
import static dev.morling.jfrunit.ExpectedEvent.*;
148+
import static org.moditect.jfrunit.JfrEventsAssert.*;
149+
import static org.moditect.jfrunit.ExpectedEvent.*;
150150

151151
@Test
152152
@EnableConfiguration("profile")

src/main/java/org/moditect/jfrunit/JfrAnalysisAssert.java

Lines changed: 0 additions & 124 deletions
This file was deleted.

src/main/java/org/moditect/jfrunit/JfrAnalysisResults.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/java/org/moditect/jfrunit/JfrEvents.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@
3232
import java.util.Queue;
3333
import java.util.concurrent.ConcurrentLinkedQueue;
3434
import java.util.concurrent.CountDownLatch;
35+
import java.util.concurrent.atomic.AtomicInteger;
3536
import java.util.concurrent.atomic.AtomicLong;
3637
import java.util.function.Predicate;
3738
import java.util.regex.Pattern;
3839
import java.util.stream.Stream;
3940

4041
import org.moditect.jfrunit.EnableEvent.StacktracePolicy;
42+
import org.moditect.jfrunit.internal.JmcAutomaticAnalysis;
4143
import org.moditect.jfrunit.internal.SyncEvent;
44+
import org.openjdk.jmc.flightrecorder.rules.IResult;
4245
import org.openjdk.jmc.flightrecorder.rules.Severity;
4346

4447
import 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

Comments
 (0)