-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add automatic analysis from JMC #85
Draft
johnaohara
wants to merge
2
commits into
moditect:main
Choose a base branch
from
johnaohara:auto-analysis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
src/main/java/org/moditect/jfrunit/JmcAutomaticAnalysisAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020 - 2021 The JfrUnit authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.moditect.jfrunit; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.BiFunction; | ||
import java.util.stream.Collectors; | ||
|
||
import org.assertj.core.api.AbstractAssert; | ||
import org.openjdk.jmc.common.unit.IQuantity; | ||
import org.openjdk.jmc.flightrecorder.rules.IResult; | ||
import org.openjdk.jmc.flightrecorder.rules.IRule; | ||
import org.openjdk.jmc.flightrecorder.rules.Severity; | ||
import org.openjdk.jmc.flightrecorder.rules.TypedResult; | ||
|
||
public class JmcAutomaticAnalysisAssert extends AbstractAssert<JmcAutomaticAnalysisAssert, List<IResult>> { | ||
|
||
private IResult foundResult; | ||
private static final String LS = System.getProperty("line.separator"); | ||
|
||
public JmcAutomaticAnalysisAssert(List<IResult> results) { | ||
super(results, JmcAutomaticAnalysisAssert.class); | ||
} | ||
|
||
public static JmcAutomaticAnalysisAssert assertThat(List<IResult> results) { | ||
return new JmcAutomaticAnalysisAssert(results); | ||
} | ||
|
||
public JmcAutomaticAnalysisAssert doesNotContain(Class<? extends IRule> expectedRule) { | ||
return findRule(expectedRule, true, "JMC Analysis result contains rule of type <%s>"); | ||
} | ||
|
||
public JmcAutomaticAnalysisAssert contains(Class<? extends IRule> expectedRule) { | ||
return findRule(expectedRule); | ||
} | ||
|
||
private JmcAutomaticAnalysisAssert findRule(Class<? extends IRule> expectedRule) { | ||
return findRule(expectedRule, false, "No JMC Analysis result rule of type <%s>"); | ||
} | ||
|
||
private JmcAutomaticAnalysisAssert findRule(Class<? extends IRule> expectedRule, boolean negate, String failureMsg) { | ||
isNotNull(); | ||
|
||
Optional<IResult> optionalIResult = actual.stream() | ||
.filter(re -> re.getRule().getClass().equals(expectedRule)) | ||
.findAny(); | ||
|
||
boolean found = optionalIResult | ||
.isPresent(); | ||
|
||
if (negate ? found : !found) { | ||
failWithMessage(failureMsg, expectedRule.getName()); | ||
} | ||
else { | ||
if (!negate) { | ||
this.foundResult = optionalIResult.get(); | ||
} | ||
} | ||
|
||
return this; | ||
|
||
} | ||
|
||
public JmcAutomaticAnalysisAssert hasSeverity(Class<? extends IRule> expectedRule, Severity expectedSeverity) { | ||
Optional<IResult> resultOptional = findResult(expectedRule); | ||
|
||
if (!resultOptional.isPresent()) { | ||
failWithMessage("No analysis type for <%s>", expectedRule.getName()); | ||
} | ||
else { | ||
IResult result = resultOptional.get(); | ||
|
||
if (result.getSeverity().getLimit() < expectedSeverity.getLimit()) { | ||
failWithMessage("Analysis result not required severity <%s>", expectedSeverity); | ||
|
||
} | ||
} | ||
return this; | ||
} | ||
|
||
public JmcAutomaticAnalysisAssert scoresLessThan(Class<? extends IRule> expectedRule, double expectedScore) { | ||
|
||
findRule(expectedRule); | ||
|
||
return scoresLessThan(expectedScore); | ||
} | ||
|
||
public JmcAutomaticAnalysisAssert scoresLessThan(double expectedScore) { | ||
IQuantity resultScore = this.foundResult.getResult(TypedResult.SCORE); | ||
double score = 0; | ||
if (resultScore != null) { | ||
score = resultScore.doubleValue(); | ||
} | ||
else if (this.foundResult.getSeverity().getLimit() != 0.0d) { | ||
score = this.foundResult.getSeverity().getLimit(); | ||
} | ||
|
||
if (score > expectedScore) { | ||
failWithMessage("Analysis result score exceeds threshold: actual <%.1f>, threshold <%.1f>", score, expectedScore); | ||
} | ||
return this; | ||
|
||
} | ||
|
||
public JmcAutomaticAnalysisAssert removeRuleFromResults(Class<? extends IRule> rule) { | ||
actual | ||
.stream() | ||
.filter(result -> result.getRule().equals(rule)).forEach(filtered -> actual.remove(filtered)); | ||
return this; | ||
} | ||
|
||
public JmcAutomaticAnalysisAssert haveSeverityGreaterThan(Severity expectedSeverity) { | ||
List<IResult> filterResults = filterBySeverity(expectedSeverity, (expected, actualSeverity) -> actualSeverity.compareTo(expectedSeverity) < 0); | ||
if (filterResults.size() == 0) { | ||
failWithMessage("Expected to contain severity greater than: " + expectedSeverity.getLocalizedName()); | ||
} | ||
return this; | ||
} | ||
|
||
public JmcAutomaticAnalysisAssert haveSeverityLessThan(Severity expectedSeverity) { | ||
List<IResult> filterResults = filterBySeverity(expectedSeverity, (expected, actualSeverity) -> actualSeverity.compareTo(expectedSeverity) >= 0); | ||
|
||
if (filterResults.size() > 0) { | ||
StringBuilder reportBuilder = new StringBuilder(); | ||
reportBuilder.append("Analysis result score equals or exceeds threshold: ") | ||
.append(expectedSeverity.getLocalizedName()) | ||
.append(LS).append(LS); | ||
|
||
filterResults.forEach(result -> { | ||
reportBuilder.append(result.getSummary()) | ||
.append(LS) | ||
.append(result.getExplanation()) | ||
.append(LS).append(LS); | ||
}); | ||
failWithMessage(reportBuilder.toString()); | ||
} | ||
return this; | ||
|
||
} | ||
|
||
private List<IResult> filterBySeverity(Severity expectedSeverity, BiFunction<Severity, Severity, Boolean> severityComparator) { | ||
return actual | ||
.stream() | ||
.filter(result -> severityComparator.apply(expectedSeverity, result.getSeverity())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Optional<IResult> findResult(Class<? extends IRule> expectedRule) { | ||
return actual.stream() | ||
.filter(re -> re.getRule().getClass().equals(expectedRule)) | ||
.findFirst(); | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does that score mean?
Taking a step back, I'm still trying to wrap my head around the capabilities of the JMC rules. Are there other real-world examples coming to mind which would show the power of that feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each rule can generate a score between 0-100. It roughly translates to the risk rating of a problem that has been detected. 0 being least likely to cause an issue to 100 being most likely to be problem. The score implementation is specific to the Rule that is triggered.
e.g. for the
ExceptionRule
it is a score based on number of exceptions per second : https://github.com/openjdk/jmc/blob/master/core/org.openjdk.jmc.flightrecorder.rules.jdk/src/main/java/org/openjdk/jmc/flightrecorder/rules/jdk/exceptions/ExceptionRule.java#L109For the SockertWriteRule, it is score calculated from the maximum duration of all socket writes: https://github.com/openjdk/jmc/blob/master/core/org.openjdk.jmc.flightrecorder.rules.jdk/src/main/java/org/openjdk/jmc/flightrecorder/rules/jdk/io/SocketWriteRule.java#L135
This assertion allows users who are familiar with the Rules to assert that risk scores did not cross a certain threshold
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I see. Aren't we back to relying on characteristics of the environment then though? E.g. Exceptions per second might differ on the general performance and load of the machine running the test. So I'm wondering how stable/portable such test would be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe they were poor examples, I think some of the rules will be dependent on environment and others not. At present all the rules available from JMC are loaded. Maybe I could curate a list of Rules that do not rely on environment and only load them?