Skip to content

Commit

Permalink
Fix for html reports (#113)
Browse files Browse the repository at this point in the history
* Replacing proceed call with skip call

* Fix for report generation with clashing test name/group

* Adding trace logging

---------

Co-authored-by: noconnor <[email protected]>
  • Loading branch information
noconnor and noconnor authored Jun 30, 2023
1 parent 3c7e556 commit 1ff2514
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Maps.newTreeMap;
import static java.lang.System.nanoTime;
import static java.util.Collections.emptyMap;
import static java.util.Objects.nonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
Expand All @@ -17,6 +18,7 @@
import com.google.common.primitives.Floats;
import com.google.common.primitives.Ints;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -109,12 +111,23 @@ public class EvaluationContext {
@Getter
@Setter
private String groupName;
@Getter
private final String uniqueId;

public EvaluationContext(String testName, long startTimeNs) {
this(testName, startTimeNs, false);
this(String.valueOf(nanoTime()), testName, startTimeNs);
}

public EvaluationContext(String testName, long startTimeNs, boolean isAsyncEvaluation) {
this(String.valueOf(nanoTime()), testName, startTimeNs, isAsyncEvaluation);
}

public EvaluationContext(String uniqueId, String testName, long startTimeNs) {
this(uniqueId, testName, startTimeNs, false);
}

public EvaluationContext(String uniqueId, String testName, long startTimeNs, boolean isAsyncEvaluation) {
this.uniqueId = testName + "_" + uniqueId;
this.testName = testName;
this.startTimeNs = startTimeNs;
this.startTime = DatetimeUtils.now();
Expand Down Expand Up @@ -180,6 +193,19 @@ public void runValidation() {
noLatencyPercentileFailures();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EvaluationContext context = (EvaluationContext) o;
return Objects.equals(testName, context.testName) && Objects.equals(groupName, context.groupName) && Objects.equals(uniqueId, context.uniqueId);
}

@Override
public int hashCode() {
return Objects.hash(testName, groupName, uniqueId);
}

private boolean validateLatency(float actualMs, float requiredMs) {
return requiredMs < 0 || actualMs <= requiredMs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static final class RequiredPercentilesData {
}

private final String testName;
private final String uniqueId;
private final String testNameColour;
private final String chartData;
private final String csvData;
Expand Down Expand Up @@ -62,6 +63,7 @@ public static final class RequiredPercentilesData {

public ViewData(EvaluationContext context) {
this.testName = buildTestName(context);
this.uniqueId = context.getUniqueId();
this.testNameColour = context.isAborted() ? SKIPPED_COLOUR : context.isSuccessful() ? SUCCESS_COLOUR : FAILED_COLOUR;
this.chartData = buildChartData(context);
this.csvData = buildCsvData(context);
Expand Down
16 changes: 8 additions & 8 deletions junitperf-core/src/main/resources/templates/report.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
<table border='1' cellspacing='0' cellpadding='3px' style='border-color:#eee'>
<tr>
<th style='background-color:#ffffdd; color:#044e9b'>&nbsp;&nbsp;&nbsp;</th>
<th style='background-color:#ffffdd; color:#044e9b'>Tests</th>
<th style='background-color:#ffffdd; color:#044e9b'>Test</th>
<tr>


{% OVERVIEW_BLOCK %}
<tr>
<!-- Success: #2b67a4 failure: #d9534f -->
<td style='background-color:{{ context.testNameColour }};'>&nbsp;</td>
<td><a href='#{{ context.testName }}'>{{ context.testName }}</td>
<td><a href='#{{ context.uniqueId }}'>{{ context.testName }}</td>
<tr>

{% END %}
Expand All @@ -33,7 +33,7 @@


{% DETAILED_BLOCK %}
<a name='{{ context.testName }}'><h2 style='color:#2b67a4'>{{ context.testName }}</h2></a>
<a name='{{ context.uniqueId }}'><h2 style='color:#2b67a4'>{{ context.testName }}</h2></a>
<table width='970'>
<tr>
<td>
Expand Down Expand Up @@ -68,7 +68,7 @@
dataOpacity: 0.5
};

var chart = new google.visualization.ScatterChart(document.getElementById('{{ context.testName }}'));
var chart = new google.visualization.ScatterChart(document.getElementById('{{ context.uniqueId }}'));

chart.draw(data, options);
}
Expand All @@ -80,15 +80,15 @@
]);
var csvFormattedDataTable = google.visualization.dataTableToCsv(csvData);
var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvFormattedDataTable);
var link = document.getElementById('Export-{{ context.testName }}')
var link = document.getElementById('Export-{{ context.uniqueId }}')
link.href=encodedUri;
link.download = '{{ context.testName }}.csv';
link.download = '{{ context.uniqueId }}.csv';
link.target = '_blank';
}

</script>
<div id="{{ context.testName }}" style="width: 600px; height: 400px;"></div>
<a id="Export-{{ context.testName }}" href="#" style="margin-left: 230px;">Download as csv</a>
<div id="{{ context.uniqueId }}" style="width: 600px; height: 400px;"></div>
<a id="Export-{{ context.uniqueId }}" href="#" style="margin-left: 230px;">Download as csv</a>
</td>
<td>
<table style='font-family:sans-serif;'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public void tearDown(){
System.clearProperty(JUNITPERF_TOTAL_EXECUTIONS);
}

@Test
public void whenANewContextIsCreated_thenAUniqueIdShouldBeAssignedToTheContext() {
int expected = String.valueOf(nanoTime()).length();
assertTrue(context.getUniqueId().matches("UNITTEST_[\\d]{"+expected+"}"));
}

@Test
public void whenLoadingJUnitPerfTestSettings_thenAppropriateContextSettingsShouldBeUpdated() {
context.loadConfiguration(perfTestAnnotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.LinkedHashSet;

Expand Down Expand Up @@ -52,7 +53,7 @@ protected File getResourceFile(String fileName) {
}

protected String readFileContents(final File file) throws IOException {
return new String(Files.readAllBytes(file.toPath()), Charset.forName("utf-8")).replaceAll("\\s+", "");
return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8).replaceAll("\\s+", "");
}

protected void verifyAllValidationFailed(EvaluationContext context) {
Expand All @@ -70,7 +71,7 @@ protected void verifyAllValidationPassed(EvaluationContext context) {
}

protected EvaluationContext createdFailedEvaluationContext(String name) {
EvaluationContext context = new EvaluationContext(name, nanoTime());
EvaluationContext context = new EvaluationContext("unique_id", name, nanoTime());
context.setFinishTimeNs(nanoTime() + SECONDS.toNanos(10));
context.loadConfiguration(perfTestAnnotationMock);
context.loadRequirements(perfTestRequirementAnnotationMock);
Expand All @@ -80,7 +81,7 @@ protected EvaluationContext createdFailedEvaluationContext(String name) {
}

protected EvaluationContext createdSuccessfulEvaluationContext(String name) {
EvaluationContext context = new EvaluationContext(name, nanoTime());
EvaluationContext context = new EvaluationContext("unique_id", name, nanoTime());
context.setFinishTimeNs(nanoTime() + SECONDS.toNanos(10));
context.loadConfiguration(perfTestAnnotationMock);
context.loadRequirements(perfTestRequirementAnnotationMock);
Expand All @@ -90,7 +91,7 @@ protected EvaluationContext createdSuccessfulEvaluationContext(String name) {
}

protected EvaluationContext createdAbortedEvaluationContext(String name) {
EvaluationContext context = new EvaluationContext(name, nanoTime());
EvaluationContext context = new EvaluationContext("unique_id", name, nanoTime());
context.loadConfiguration(perfTestAnnotationMock);
context.loadRequirements(perfTestRequirementAnnotationMock);
context.setStatistics(createAbortedMock());
Expand All @@ -99,7 +100,7 @@ protected EvaluationContext createdAbortedEvaluationContext(String name) {
}

protected EvaluationContext createdSomeFailuresEvaluationContext(String name) {
EvaluationContext context = new EvaluationContext(name, nanoTime());
EvaluationContext context = new EvaluationContext("unique_id", name, nanoTime());
context.setFinishTimeNs(nanoTime() + SECONDS.toNanos(10));
context.loadConfiguration(perfTestAnnotationMock);
context.loadRequirements(perfTestRequirementAnnotationMock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public void whenGeneratingAReport_andTestsContainsSomeAbortsAndFailures_thenAppr
assertEquals(readFileContents(expectedContents), readFileContents(reportFile));
}

@Test
public void whenGeneratingAReport_andTestsContainsSomeAbortsAndFailures_andGenerateReportIsCalledMultipleTimes_thenAppropriateReportShouldBeGenerated() throws IOException {
reportGenerator.generateReport(generateAbortedFailedAndSuccessContexts());
reportGenerator.generateReport(generateAbortedFailedAndSuccessContexts());
File expectedContents = getResourceFile("csv/fail_abort_succeed.csv");
assertEquals(readFileContents(expectedContents), readFileContents(reportFile));
}

@Test
public void whenCallingGetReportPath_andCustomPathHasBeenSpecified_thenCorrectPathShouldBeReturned() {
assertThat(reportGenerator.getReportPath(), is(reportFile.getPath()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public void whenGeneratingAReport_andTestsContainsSomeAbortsAndFailures_thenAppr
assertEquals(readFileContents(expectedContents), readFileContents(reportFile));
}

@Test
public void whenGeneratingAReport_andTestsContainsSomeAbortsAndFailures_andGenerateIsCalledMultipleTimes_thenAppropriateReportShouldBeGenerated() throws IOException {
reportGenerator.generateReport(generateAbortedFailedAndSuccessContexts());
reportGenerator.generateReport(generateAbortedFailedAndSuccessContexts());
File expectedContents = getResourceFile("html/example_aborted_failed_success.html");
assertEquals(readFileContents(expectedContents), readFileContents(reportFile));
}

@Test
public void whenCallingGetReportPath_andCustomPathHasBeenSpecified_thenCorrectPathShouldBeReturned() {
assertThat(reportGenerator.getReportPath(), is(reportFile.getPath()));
Expand Down Expand Up @@ -101,7 +109,7 @@ public void whenHtmlProcessorProcessBlocksIsCalled_thenTheCorrectBlocksShouldBeP
assertTrue(blocks.containsKey("{% DETAILED_BLOCK %}"));
assertTrue(blocks.containsKey("{% PERCENTILES_BLOCK %}"));

assertEquals(919, blocks.get("root").length());
assertEquals(918, blocks.get("root").length());
assertEquals(296, blocks.get("{% OVERVIEW_BLOCK %}").length());
assertEquals(7883, blocks.get("{% DETAILED_BLOCK %}").length());
assertEquals(704, blocks.get("{% PERCENTILES_BLOCK %}").length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
<table border='1' cellspacing='0' cellpadding='3px' style='border-color:#eee'>
<tr>
<th style='background-color:#ffffdd; color:#044e9b'>&nbsp;&nbsp;&nbsp;</th>
<th style='background-color:#ffffdd; color:#044e9b'>Tests</th>
<th style='background-color:#ffffdd; color:#044e9b'>Test</th>
<tr>


<tr>
<!-- Success: #2b67a4 failure: #d9534f -->
<td style='background-color:#d9534f;'>&nbsp;</td>
<td><a href='#unittest1'>unittest1</td>
<td><a href='#unittest1_unique_id'>unittest1</td>
<tr>


<tr>
<!-- Success: #2b67a4 failure: #d9534f -->
<td style='background-color:#dcdcdc;'>&nbsp;</td>
<td><a nolink='#unittest2 (skipped)'>unittest2 (skipped)</td>
<td><a nolink='#unittest2_unique_id'>unittest2 (skipped)</td>
<tr>


<tr>
<!-- Success: #2b67a4 failure: #d9534f -->
<td style='background-color:#2b67a4;'>&nbsp;</td>
<td><a href='#unittest3'>unittest3</td>
<td><a href='#unittest3_unique_id'>unittest3</td>
<tr>


Expand All @@ -46,7 +46,7 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>



<a name='unittest1'><h2 style='color:#2b67a4'>unittest1</h2></a>
<a name='unittest1_unique_id'><h2 style='color:#2b67a4'>unittest1</h2></a>
<table width='970'>
<tr>
<td>
Expand Down Expand Up @@ -179,7 +179,7 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
dataOpacity: 0.5
};

var chart = new google.visualization.ScatterChart(document.getElementById('unittest1'));
var chart = new google.visualization.ScatterChart(document.getElementById('unittest1_unique_id'));

chart.draw(data, options);
}
Expand Down Expand Up @@ -290,15 +290,15 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
]);
var csvFormattedDataTable = google.visualization.dataTableToCsv(csvData);
var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvFormattedDataTable);
var link = document.getElementById('Export-unittest1')
var link = document.getElementById('Export-unittest1_unique_id')
link.href=encodedUri;
link.download = 'unittest1.csv';
link.download = 'unittest1_unique_id.csv';
link.target = '_blank';
}

</script>
<div id="unittest1" style="width: 600px; height: 400px;"></div>
<a id="Export-unittest1" href="#" style="margin-left: 230px;">Download as csv</a>
<div id="unittest1_unique_id" style="width: 600px; height: 400px;"></div>
<a id="Export-unittest1_unique_id" href="#" style="margin-left: 230px;">Download as csv</a>
</td>
<td>
<table style='font-family:sans-serif;'>
Expand Down Expand Up @@ -408,7 +408,7 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
<br/><br/><br/>


<a name='unittest3'><h2 style='color:#2b67a4'>unittest3</h2></a>
<a name='unittest3_unique_id'><h2 style='color:#2b67a4'>unittest3</h2></a>
<table width='970'>
<tr>
<td>
Expand Down Expand Up @@ -541,7 +541,7 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
dataOpacity: 0.5
};

var chart = new google.visualization.ScatterChart(document.getElementById('unittest3'));
var chart = new google.visualization.ScatterChart(document.getElementById('unittest3_unique_id'));

chart.draw(data, options);
}
Expand Down Expand Up @@ -652,15 +652,15 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
]);
var csvFormattedDataTable = google.visualization.dataTableToCsv(csvData);
var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvFormattedDataTable);
var link = document.getElementById('Export-unittest3')
var link = document.getElementById('Export-unittest3_unique_id')
link.href=encodedUri;
link.download = 'unittest3.csv';
link.download = 'unittest3_unique_id.csv';
link.target = '_blank';
}

</script>
<div id="unittest3" style="width: 600px; height: 400px;"></div>
<a id="Export-unittest3" href="#" style="margin-left: 230px;">Download as csv</a>
<div id="unittest3_unique_id" style="width: 600px; height: 400px;"></div>
<a id="Export-unittest3_unique_id" href="#" style="margin-left: 230px;">Download as csv</a>
</td>
<td>
<table style='font-family:sans-serif;'>
Expand Down
Loading

0 comments on commit 1ff2514

Please sign in to comment.