Skip to content

Improve timestamps #20

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
</parent>
<groupId>de.rub.nds</groupId>
<artifactId>anvil-core</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.1.4-SNAPSHOT</version>
<name>Anvil-Core</name>

<scm>
<connection>scm:git:https://github.com/tls-attacker/Anvil-Core.git</connection>
<developerConnection>scm:git:ssh://git@github.com/tls-attacker/Anvil-Core.git</developerConnection>
<tag>v1.1.6</tag>
<tag>HEAD</tag>
<url>https://github.com/tls-attacker/Anvil-Core</url>
</scm>

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/de/rub/nds/anvilcore/context/AnvilContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class AnvilContext {
private final Map<String, AnvilTestRun> activeTestRuns = new HashMap<>();
private final Map<TestResult, List<String>> resultTestMap = new HashMap<>();
private final Map<String, Boolean> finishedTests = new HashMap<>();
private final Map<String, Date> startInputGenerationTimes = new HashMap<>();
private final Map<String, Date> endInputGenerationTimes = new HashMap<>();
private boolean aborted = false;

public static synchronized AnvilContext getInstance() {
Expand Down Expand Up @@ -187,4 +189,24 @@ public MetadataFetcher getMetadataFetcher() {
public String getConfigString() {
return configString;
}

public synchronized Map<String, Date> getStartInputGenerationTimes() {
return startInputGenerationTimes;
}

public synchronized Date getStartInputGenerationTime(String k) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use descirptive parameter names

return startInputGenerationTimes.get(k);
}

public synchronized void addStartInputGenerationTime(String methodeName, Date time) {
startInputGenerationTimes.put(methodeName, time);
}

public synchronized Map<String, Date> getEndInputGenerationTimes() {
return endInputGenerationTimes;
}

public synchronized void addEndInputGenerationTime(String methodeName, Date time) {
endInputGenerationTimes.put(methodeName, time);
}
}
14 changes: 11 additions & 3 deletions src/main/java/de/rub/nds/anvilcore/execution/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;

import de.rub.nds.anvilcore.annotation.AnvilTest;
import de.rub.nds.anvilcore.annotation.NonCombinatorialAnvilTest;
import de.rub.nds.anvilcore.context.AnvilContext;
import de.rub.nds.anvilcore.context.AnvilTestConfig;
import de.rub.nds.anvilcore.context.ProfileResolver;
Expand Down Expand Up @@ -99,11 +100,18 @@ public void runTests() {
(MethodBasedTestDescriptor) descriptor;
Method method = md.getTestMethod();
String anvilTestId = null;
try {
if (method.getAnnotation(AnvilTest.class) != null) {
anvilTestId = method.getAnnotation(AnvilTest.class).id();
} catch (NullPointerException e) {
LOGGER.debug("Method " + method + " has no ID");
} else if (method.getAnnotation(NonCombinatorialAnvilTest.class)
!= null) {
anvilTestId =
method.getAnnotation(
NonCombinatorialAnvilTest.class)
.id();
} else {
LOGGER.warn("Method " + method + " has no ID");
}

if (anvilTestId != null) {
if (ids.contains(anvilTestId)) {
return FilterResult.included("Profile includes ID");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
Expand Down Expand Up @@ -119,6 +120,17 @@ private void processNonCombinatorial(
testRun.finish();
}

public AnvilTestCase getTestCase(ExtensionContext extensionContext, AnvilTestRun anvilTestRun) {
return anvilTestRun.getTestCases().stream()
.filter(
testCase ->
testCase.getExtensionContext()
.getUniqueId()
.equals(extensionContext.getUniqueId()))
.findFirst()
.orElse(null);
}

/**
* TestCase or non-combinatorial test failed / did not pass.
*
Expand Down Expand Up @@ -208,7 +220,12 @@ private AnvilTestRun createAnvilTestRunForExtensionContext(ExtensionContext exte
@Override
public void testInputGroupGenerated(
TestInputGroupContext context, List<Combination> testInputs) {
// AnvilTestRun testRun = new AnvilTestRun(extensionContext);
// AnvilContext.getInstance().addActiveTestRun(testRun);
AnvilTestRun testRun = createAnvilTestRunForExtensionContext(extensionContext);
AnvilContext.getInstance()
.addEndInputGenerationTime(
extensionContext.getRequiredTestMethod().toString(), new Date());
LOGGER.trace(
"Test Inputs generated for " + extensionContext.getRequiredTestMethod().getName());
}
Expand Down Expand Up @@ -293,6 +310,8 @@ public void executionSkipped(TestIdentifier testIdentifier, String reason) {
*/
@Override
public void executionStarted(TestIdentifier testIdentifier) {
long time = System.currentTimeMillis();
Date date = new Date(time);
if (!testIdentifier.isContainer()
&& testIdentifier.getSource().isPresent()
&& testIdentifier.getSource().get() instanceof MethodSource) {
Expand All @@ -307,7 +326,28 @@ public void executionStarted(TestIdentifier testIdentifier) {
LOGGER.trace(testIdentifier.getDisplayName() + " started");
}
if (testIdentifier.isContainer()) {
elapsedTimes.put(testIdentifier.getUniqueId(), System.currentTimeMillis());
elapsedTimes.put(testIdentifier.getUniqueId(), time);
}
TestSource source = testIdentifier.getSource().orElse(null);
if (testIdentifier.isContainer() && source instanceof MethodSource) {
MethodSource methodSource = (MethodSource) source;
AnvilContext anvilContext = AnvilContext.getInstance();
anvilContext.addStartInputGenerationTime(methodSource.getJavaMethod().toString(), date);
}
}

public void executionStarted2(TestIdentifier testIdentifier) {
long time = System.currentTimeMillis();
Date date = new Date(time);
LOGGER.trace(testIdentifier.getDisplayName() + " started");
if (testIdentifier.isContainer()) {
elapsedTimes.put(testIdentifier.getUniqueId(), time);
}
TestSource source = testIdentifier.getSource().orElse(null);
if (testIdentifier.isContainer() && source instanceof MethodSource) {
MethodSource methodSource = (MethodSource) source;
AnvilContext anvilContext = AnvilContext.getInstance();
anvilContext.addStartInputGenerationTime(methodSource.getJavaMethod().toString(), date);
}
}

Expand Down
58 changes: 57 additions & 1 deletion src/main/java/de/rub/nds/anvilcore/teststate/AnvilTestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import de.rub.nds.anvilcore.annotation.AnvilTest;
import de.rub.nds.anvilcore.context.AnvilContext;
import de.rub.nds.anvilcore.junit.Utils;
import de.rub.nds.anvilcore.model.ParameterCombination;
Expand Down Expand Up @@ -89,6 +90,13 @@ private boolean hasVaryingAdditionalResultInformation() {
@JsonProperty("DisabledReason")
private String disabledReason;

private Date startInputGenerationTime;

private Date endInputGenerationTime;

@JsonProperty("ElapsedGenerationTimeSeconds")
private long elapsedGenerationTimeSeconds;

@JsonProperty("FailedReason")
private String failedReason;

Expand Down Expand Up @@ -202,10 +210,28 @@ public void finish() {
LOGGER.info("{} is disable because {}", getTestMethodName(), getDisabledReason());
}
scoreContainer.updateForResult(result);
// AnvilContext.getInstance().testFinished(uniqueId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete uncommented code

if (result != TestResult.DISABLED && testMethod.getAnnotation(AnvilTest.class) != null) {
try {
Date startInputGenerationTimes =
AnvilContext.getInstance()
.getStartInputGenerationTimes()
.get(this.testMethod.toString());
Date endInputGenerationTimes =
AnvilContext.getInstance()
.getEndInputGenerationTimes()
.get(this.testMethod.toString());
this.setStartInputGenerationTime(startInputGenerationTimes);
this.setEndInputGenerationTime(endInputGenerationTimes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change it from date to seconds elapsed? Attrbutes could be named like "generationTimeSeconds" and "executionTimeSeconds"

} catch (NullPointerException e) {
LOGGER.warn("Cannot read GenerationTimes");
}
}
scoreContainer.updateForResult(result);
AnvilContext.getInstance()
.addTestResult(result, testClass.getName() + "." + testMethod.getName());
AnvilContext.getInstance().getMapper().saveTestRunToPath(this);
AnvilContext.getInstance().testFinished(uniqueId);
AnvilContext.getInstance().getMapper().saveTestRunToPath(this);
}

public TestResult resolveFinalResult() {
Expand Down Expand Up @@ -333,4 +359,34 @@ public boolean isReadyForCompletion() {
public void setReadyForCompletion(boolean readyForCompletion) {
this.readyForCompletion = readyForCompletion;
}

public Date getStartInputGenerationTime() {
return startInputGenerationTime;
}

public void setStartInputGenerationTime(Date startInputGenerationTime) {
this.startInputGenerationTime = startInputGenerationTime;
}

public Date getEndInputGenerationTime() {
return endInputGenerationTime;
}

public void setEndInputGenerationTime(Date endInputGenerationTime) {
this.endInputGenerationTime = endInputGenerationTime;
this.updateElapsedGenerationTimeSeconds();
}

private void updateElapsedGenerationTimeSeconds() {
this.elapsedGenerationTimeSeconds =
(this.endInputGenerationTime.getTime() - startInputGenerationTime.getTime()) / 1000;
}

public long getElapsedGenerationTimeSeconds() {
return elapsedGenerationTimeSeconds;
}

public void setElapsedGenerationTimeSeconds(long elapsedGenerationTimeSeconds) {
this.elapsedGenerationTimeSeconds = elapsedGenerationTimeSeconds;
}
}