Skip to content

Commit 1ff2514

Browse files
noconnornoconnor
and
noconnor
authored
Fix for html reports (#113)
* Replacing proceed call with skip call * Fix for report generation with clashing test name/group * Adding trace logging --------- Co-authored-by: noconnor <[email protected]>
1 parent 3c7e556 commit 1ff2514

File tree

14 files changed

+150
-99
lines changed

14 files changed

+150
-99
lines changed

junitperf-core/src/main/java/com/github/noconnor/junitperf/data/EvaluationContext.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.google.common.base.Preconditions.checkNotNull;
44
import static com.google.common.base.Preconditions.checkState;
55
import static com.google.common.collect.Maps.newTreeMap;
6+
import static java.lang.System.nanoTime;
67
import static java.util.Collections.emptyMap;
78
import static java.util.Objects.nonNull;
89
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@@ -17,6 +18,7 @@
1718
import com.google.common.primitives.Floats;
1819
import com.google.common.primitives.Ints;
1920
import java.util.Map;
21+
import java.util.Objects;
2022
import java.util.stream.Stream;
2123
import lombok.Getter;
2224
import lombok.Setter;
@@ -109,12 +111,23 @@ public class EvaluationContext {
109111
@Getter
110112
@Setter
111113
private String groupName;
114+
@Getter
115+
private final String uniqueId;
112116

113117
public EvaluationContext(String testName, long startTimeNs) {
114-
this(testName, startTimeNs, false);
118+
this(String.valueOf(nanoTime()), testName, startTimeNs);
115119
}
116120

117121
public EvaluationContext(String testName, long startTimeNs, boolean isAsyncEvaluation) {
122+
this(String.valueOf(nanoTime()), testName, startTimeNs, isAsyncEvaluation);
123+
}
124+
125+
public EvaluationContext(String uniqueId, String testName, long startTimeNs) {
126+
this(uniqueId, testName, startTimeNs, false);
127+
}
128+
129+
public EvaluationContext(String uniqueId, String testName, long startTimeNs, boolean isAsyncEvaluation) {
130+
this.uniqueId = testName + "_" + uniqueId;
118131
this.testName = testName;
119132
this.startTimeNs = startTimeNs;
120133
this.startTime = DatetimeUtils.now();
@@ -180,6 +193,19 @@ public void runValidation() {
180193
noLatencyPercentileFailures();
181194
}
182195

196+
@Override
197+
public boolean equals(Object o) {
198+
if (this == o) return true;
199+
if (o == null || getClass() != o.getClass()) return false;
200+
EvaluationContext context = (EvaluationContext) o;
201+
return Objects.equals(testName, context.testName) && Objects.equals(groupName, context.groupName) && Objects.equals(uniqueId, context.uniqueId);
202+
}
203+
204+
@Override
205+
public int hashCode() {
206+
return Objects.hash(testName, groupName, uniqueId);
207+
}
208+
183209
private boolean validateLatency(float actualMs, float requiredMs) {
184210
return requiredMs < 0 || actualMs <= requiredMs;
185211
}

junitperf-core/src/main/java/com/github/noconnor/junitperf/reporting/providers/utils/ViewData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static final class RequiredPercentilesData {
3333
}
3434

3535
private final String testName;
36+
private final String uniqueId;
3637
private final String testNameColour;
3738
private final String chartData;
3839
private final String csvData;
@@ -62,6 +63,7 @@ public static final class RequiredPercentilesData {
6263

6364
public ViewData(EvaluationContext context) {
6465
this.testName = buildTestName(context);
66+
this.uniqueId = context.getUniqueId();
6567
this.testNameColour = context.isAborted() ? SKIPPED_COLOUR : context.isSuccessful() ? SUCCESS_COLOUR : FAILED_COLOUR;
6668
this.chartData = buildChartData(context);
6769
this.csvData = buildCsvData(context);

junitperf-core/src/main/resources/templates/report.template

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
<table border='1' cellspacing='0' cellpadding='3px' style='border-color:#eee'>
1515
<tr>
1616
<th style='background-color:#ffffdd; color:#044e9b'>&nbsp;&nbsp;&nbsp;</th>
17-
<th style='background-color:#ffffdd; color:#044e9b'>Tests</th>
17+
<th style='background-color:#ffffdd; color:#044e9b'>Test</th>
1818
<tr>
1919

2020

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

2828
{% END %}
@@ -33,7 +33,7 @@
3333

3434

3535
{% DETAILED_BLOCK %}
36-
<a name='{{ context.testName }}'><h2 style='color:#2b67a4'>{{ context.testName }}</h2></a>
36+
<a name='{{ context.uniqueId }}'><h2 style='color:#2b67a4'>{{ context.testName }}</h2></a>
3737
<table width='970'>
3838
<tr>
3939
<td>
@@ -68,7 +68,7 @@
6868
dataOpacity: 0.5
6969
};
7070

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

7373
chart.draw(data, options);
7474
}
@@ -80,15 +80,15 @@
8080
]);
8181
var csvFormattedDataTable = google.visualization.dataTableToCsv(csvData);
8282
var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvFormattedDataTable);
83-
var link = document.getElementById('Export-{{ context.testName }}')
83+
var link = document.getElementById('Export-{{ context.uniqueId }}')
8484
link.href=encodedUri;
85-
link.download = '{{ context.testName }}.csv';
85+
link.download = '{{ context.uniqueId }}.csv';
8686
link.target = '_blank';
8787
}
8888

8989
</script>
90-
<div id="{{ context.testName }}" style="width: 600px; height: 400px;"></div>
91-
<a id="Export-{{ context.testName }}" href="#" style="margin-left: 230px;">Download as csv</a>
90+
<div id="{{ context.uniqueId }}" style="width: 600px; height: 400px;"></div>
91+
<a id="Export-{{ context.uniqueId }}" href="#" style="margin-left: 230px;">Download as csv</a>
9292
</td>
9393
<td>
9494
<table style='font-family:sans-serif;'>

junitperf-core/src/test/java/com/github/noconnor/junitperf/data/EvaluationContextTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public void tearDown(){
6666
System.clearProperty(JUNITPERF_TOTAL_EXECUTIONS);
6767
}
6868

69+
@Test
70+
public void whenANewContextIsCreated_thenAUniqueIdShouldBeAssignedToTheContext() {
71+
int expected = String.valueOf(nanoTime()).length();
72+
assertTrue(context.getUniqueId().matches("UNITTEST_[\\d]{"+expected+"}"));
73+
}
74+
6975
@Test
7076
public void whenLoadingJUnitPerfTestSettings_thenAppropriateContextSettingsShouldBeUpdated() {
7177
context.loadConfiguration(perfTestAnnotation);

junitperf-core/src/test/java/com/github/noconnor/junitperf/reporting/BaseReportGeneratorTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.OutputStreamWriter;
88
import java.io.Writer;
99
import java.nio.charset.Charset;
10+
import java.nio.charset.StandardCharsets;
1011
import java.nio.file.Files;
1112
import java.util.LinkedHashSet;
1213

@@ -52,7 +53,7 @@ protected File getResourceFile(String fileName) {
5253
}
5354

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

5859
protected void verifyAllValidationFailed(EvaluationContext context) {
@@ -70,7 +71,7 @@ protected void verifyAllValidationPassed(EvaluationContext context) {
7071
}
7172

7273
protected EvaluationContext createdFailedEvaluationContext(String name) {
73-
EvaluationContext context = new EvaluationContext(name, nanoTime());
74+
EvaluationContext context = new EvaluationContext("unique_id", name, nanoTime());
7475
context.setFinishTimeNs(nanoTime() + SECONDS.toNanos(10));
7576
context.loadConfiguration(perfTestAnnotationMock);
7677
context.loadRequirements(perfTestRequirementAnnotationMock);
@@ -80,7 +81,7 @@ protected EvaluationContext createdFailedEvaluationContext(String name) {
8081
}
8182

8283
protected EvaluationContext createdSuccessfulEvaluationContext(String name) {
83-
EvaluationContext context = new EvaluationContext(name, nanoTime());
84+
EvaluationContext context = new EvaluationContext("unique_id", name, nanoTime());
8485
context.setFinishTimeNs(nanoTime() + SECONDS.toNanos(10));
8586
context.loadConfiguration(perfTestAnnotationMock);
8687
context.loadRequirements(perfTestRequirementAnnotationMock);
@@ -90,7 +91,7 @@ protected EvaluationContext createdSuccessfulEvaluationContext(String name) {
9091
}
9192

9293
protected EvaluationContext createdAbortedEvaluationContext(String name) {
93-
EvaluationContext context = new EvaluationContext(name, nanoTime());
94+
EvaluationContext context = new EvaluationContext("unique_id", name, nanoTime());
9495
context.loadConfiguration(perfTestAnnotationMock);
9596
context.loadRequirements(perfTestRequirementAnnotationMock);
9697
context.setStatistics(createAbortedMock());
@@ -99,7 +100,7 @@ protected EvaluationContext createdAbortedEvaluationContext(String name) {
99100
}
100101

101102
protected EvaluationContext createdSomeFailuresEvaluationContext(String name) {
102-
EvaluationContext context = new EvaluationContext(name, nanoTime());
103+
EvaluationContext context = new EvaluationContext("unique_id", name, nanoTime());
103104
context.setFinishTimeNs(nanoTime() + SECONDS.toNanos(10));
104105
context.loadConfiguration(perfTestAnnotationMock);
105106
context.loadRequirements(perfTestRequirementAnnotationMock);

junitperf-core/src/test/java/com/github/noconnor/junitperf/reporting/providers/CsvReportGeneratorTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public void whenGeneratingAReport_andTestsContainsSomeAbortsAndFailures_thenAppr
7171
assertEquals(readFileContents(expectedContents), readFileContents(reportFile));
7272
}
7373

74+
@Test
75+
public void whenGeneratingAReport_andTestsContainsSomeAbortsAndFailures_andGenerateReportIsCalledMultipleTimes_thenAppropriateReportShouldBeGenerated() throws IOException {
76+
reportGenerator.generateReport(generateAbortedFailedAndSuccessContexts());
77+
reportGenerator.generateReport(generateAbortedFailedAndSuccessContexts());
78+
File expectedContents = getResourceFile("csv/fail_abort_succeed.csv");
79+
assertEquals(readFileContents(expectedContents), readFileContents(reportFile));
80+
}
81+
7482
@Test
7583
public void whenCallingGetReportPath_andCustomPathHasBeenSpecified_thenCorrectPathShouldBeReturned() {
7684
assertThat(reportGenerator.getReportPath(), is(reportFile.getPath()));

junitperf-core/src/test/java/com/github/noconnor/junitperf/reporting/providers/HtmlReportGeneratorTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public void whenGeneratingAReport_andTestsContainsSomeAbortsAndFailures_thenAppr
7474
assertEquals(readFileContents(expectedContents), readFileContents(reportFile));
7575
}
7676

77+
@Test
78+
public void whenGeneratingAReport_andTestsContainsSomeAbortsAndFailures_andGenerateIsCalledMultipleTimes_thenAppropriateReportShouldBeGenerated() throws IOException {
79+
reportGenerator.generateReport(generateAbortedFailedAndSuccessContexts());
80+
reportGenerator.generateReport(generateAbortedFailedAndSuccessContexts());
81+
File expectedContents = getResourceFile("html/example_aborted_failed_success.html");
82+
assertEquals(readFileContents(expectedContents), readFileContents(reportFile));
83+
}
84+
7785
@Test
7886
public void whenCallingGetReportPath_andCustomPathHasBeenSpecified_thenCorrectPathShouldBeReturned() {
7987
assertThat(reportGenerator.getReportPath(), is(reportFile.getPath()));
@@ -101,7 +109,7 @@ public void whenHtmlProcessorProcessBlocksIsCalled_thenTheCorrectBlocksShouldBeP
101109
assertTrue(blocks.containsKey("{% DETAILED_BLOCK %}"));
102110
assertTrue(blocks.containsKey("{% PERCENTILES_BLOCK %}"));
103111

104-
assertEquals(919, blocks.get("root").length());
112+
assertEquals(918, blocks.get("root").length());
105113
assertEquals(296, blocks.get("{% OVERVIEW_BLOCK %}").length());
106114
assertEquals(7883, blocks.get("{% DETAILED_BLOCK %}").length());
107115
assertEquals(704, blocks.get("{% PERCENTILES_BLOCK %}").length());

junitperf-core/src/test/resources/html/example_aborted_failed_success.html

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
1414
<table border='1' cellspacing='0' cellpadding='3px' style='border-color:#eee'>
1515
<tr>
1616
<th style='background-color:#ffffdd; color:#044e9b'>&nbsp;&nbsp;&nbsp;</th>
17-
<th style='background-color:#ffffdd; color:#044e9b'>Tests</th>
17+
<th style='background-color:#ffffdd; color:#044e9b'>Test</th>
1818
<tr>
1919

2020

2121
<tr>
2222
<!-- Success: #2b67a4 failure: #d9534f -->
2323
<td style='background-color:#d9534f;'>&nbsp;</td>
24-
<td><a href='#unittest1'>unittest1</td>
24+
<td><a href='#unittest1_unique_id'>unittest1</td>
2525
<tr>
2626

2727

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

3434

3535
<tr>
3636
<!-- Success: #2b67a4 failure: #d9534f -->
3737
<td style='background-color:#2b67a4;'>&nbsp;</td>
38-
<td><a href='#unittest3'>unittest3</td>
38+
<td><a href='#unittest3_unique_id'>unittest3</td>
3939
<tr>
4040

4141

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

4747

4848

49-
<a name='unittest1'><h2 style='color:#2b67a4'>unittest1</h2></a>
49+
<a name='unittest1_unique_id'><h2 style='color:#2b67a4'>unittest1</h2></a>
5050
<table width='970'>
5151
<tr>
5252
<td>
@@ -179,7 +179,7 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
179179
dataOpacity: 0.5
180180
};
181181

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

184184
chart.draw(data, options);
185185
}
@@ -290,15 +290,15 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
290290
]);
291291
var csvFormattedDataTable = google.visualization.dataTableToCsv(csvData);
292292
var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvFormattedDataTable);
293-
var link = document.getElementById('Export-unittest1')
293+
var link = document.getElementById('Export-unittest1_unique_id')
294294
link.href=encodedUri;
295-
link.download = 'unittest1.csv';
295+
link.download = 'unittest1_unique_id.csv';
296296
link.target = '_blank';
297297
}
298298

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

410410

411-
<a name='unittest3'><h2 style='color:#2b67a4'>unittest3</h2></a>
411+
<a name='unittest3_unique_id'><h2 style='color:#2b67a4'>unittest3</h2></a>
412412
<table width='970'>
413413
<tr>
414414
<td>
@@ -541,7 +541,7 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
541541
dataOpacity: 0.5
542542
};
543543

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

546546
chart.draw(data, options);
547547
}
@@ -652,15 +652,15 @@ <h1 style='color:#044e9b'>JUnit Performance Report</h1>
652652
]);
653653
var csvFormattedDataTable = google.visualization.dataTableToCsv(csvData);
654654
var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvFormattedDataTable);
655-
var link = document.getElementById('Export-unittest3')
655+
var link = document.getElementById('Export-unittest3_unique_id')
656656
link.href=encodedUri;
657-
link.download = 'unittest3.csv';
657+
link.download = 'unittest3_unique_id.csv';
658658
link.target = '_blank';
659659
}
660660

661661
</script>
662-
<div id="unittest3" style="width: 600px; height: 400px;"></div>
663-
<a id="Export-unittest3" href="#" style="margin-left: 230px;">Download as csv</a>
662+
<div id="unittest3_unique_id" style="width: 600px; height: 400px;"></div>
663+
<a id="Export-unittest3_unique_id" href="#" style="margin-left: 230px;">Download as csv</a>
664664
</td>
665665
<td>
666666
<table style='font-family:sans-serif;'>

0 commit comments

Comments
 (0)