Skip to content

Commit 1cec53a

Browse files
committed
feat(test): reporting time spent to create context and perform each test
Closes: #79
1 parent f200ae6 commit 1cec53a

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

src/main/java/ru/ewc/checklogic/testing/CheckFile.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,28 @@ public List<TestResult> performChecks(final String root, final CheckSuite files)
7575
this.suite = files;
7676
return this.tests.stream()
7777
.filter(rule -> rule.getFragments().stream().anyMatch(f -> f.nonEmptyOfType("CND")))
78-
.map(rule -> this.getTestResult(rule, ServerContextFactory.create(root).context()))
79-
.toList();
78+
.map(
79+
rule -> {
80+
final long start = System.currentTimeMillis();
81+
final ComputationContext context = ServerContextFactory.create(root).context();
82+
final long elapsed = System.currentTimeMillis() - start;
83+
return this.getTestResult(rule, context, elapsed);
84+
}
85+
).toList();
8086
}
8187

8288
public void performInSameContext(final ComputationContext ctx, final CheckSuite files) {
8389
this.suite = files;
84-
this.getTestResult(this.tests.getFirst(), ctx);
90+
this.getTestResult(this.tests.getFirst(), ctx, 0);
8591
}
8692

87-
private TestResult getTestResult(final RuleFragments rule, final ComputationContext ctx) {
93+
private TestResult getTestResult(
94+
final RuleFragments rule,
95+
final ComputationContext ctx,
96+
final long time
97+
) {
8898
logCheckpoint(ctx, "%s - started".formatted(rule.header()));
99+
final long start = System.currentTimeMillis();
89100
final OutputTracker<String> tracker = ctx.startTracking();
90101
final List<CheckFailure> failures = new ArrayList<>(1);
91102
for (final RuleFragment fragment : rule.getFragments()) {
@@ -112,11 +123,14 @@ private TestResult getTestResult(final RuleFragments rule, final ComputationCont
112123
}
113124
}
114125
logCheckpoint(ctx, "%s - %s".formatted(rule.header(), CheckFile.desc(failures)));
126+
final long elapsed = System.currentTimeMillis() - start;
115127
return new TestResult(
116128
rule.header().replace("::", " - "),
117129
failures.isEmpty(),
118130
resultAsUnorderedList(failures),
119-
tracker.events()
131+
tracker.events(),
132+
time,
133+
elapsed
120134
);
121135
}
122136

src/main/java/ru/ewc/checklogic/testing/CheckSuite.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,43 @@ public final class CheckSuite {
5555
*/
5656
private final String root;
5757

58-
private CheckSuite(final Collection<CheckFile> tests, final String root) {
58+
/**
59+
* Milliseconds elapsed to read all the test files.
60+
*/
61+
private final long reading;
62+
63+
private CheckSuite(final Collection<CheckFile> tests, final String root, final long reading) {
5964
this.tests = tests;
6065
this.results = new ArrayList<>(tests.size());
6166
this.root = root;
67+
this.reading = reading;
6268
}
6369

6470
public static CheckSuite using(
6571
final ContentsReader reader,
6672
final String root,
6773
final String request
6874
) {
69-
return new CheckSuite(
70-
reader.readAll().stream()
71-
.map(sl -> new CheckFile(sl.fileName(), sl.specifiedRulesFragments(), request))
72-
.toList(),
73-
root
74-
);
75+
final long start = System.currentTimeMillis();
76+
final List<CheckFile> files = reader.readAll().stream()
77+
.map(sl -> new CheckFile(sl.fileName(), sl.specifiedRulesFragments(), request))
78+
.toList();
79+
final long elapsed = System.currentTimeMillis() - start;
80+
return new CheckSuite(files, root, elapsed);
7581
}
7682

7783
public String statsAsHtmlDiv(final double elapsed) {
78-
return "%d test(s) performed in %.3f second(s), %d passed, %d failed".formatted(
84+
return """
85+
%d test(s) performed in %.3f second(s) (read time: %.3f), %d passed, %d failed</br>
86+
%d milliseconds spent creating context, %d milliseconds spent in the tests
87+
""".formatted(
7988
this.results.size(),
8089
elapsed,
90+
this.reading / 1000.0,
8191
this.results.stream().filter(TestResult::successful).count(),
82-
this.results.stream().filter(result -> !result.successful()).count()
92+
this.results.stream().filter(result -> !result.successful()).count(),
93+
this.results.stream().reduce(0L, (acc, result) -> acc + result.context(), Long::sum),
94+
this.results.stream().reduce(0L, (acc, result) -> acc + result.elapsed(), Long::sum)
8395
);
8496
}
8597

src/main/java/ru/ewc/checklogic/testing/TestResult.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public record TestResult(
2929
String file,
3030
boolean successful,
3131
String error,
32-
List<String> log
32+
List<String> log,
33+
long context,
34+
long elapsed
3335
) implements Comparable<TestResult> {
3436

3537
public String result() {
@@ -44,14 +46,19 @@ public String result() {
4446

4547
public String asHtmlTableRow() {
4648
return String.format(
47-
"<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td></tr>",
49+
"<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
4850
this.classBasedOnSuccess(),
4951
this.file,
5052
this.result(),
53+
this.time(),
5154
this.errorMessages()
5255
);
5356
}
5457

58+
private String time() {
59+
return "C: %d ms, T: %d ms".formatted(this.context, this.elapsed);
60+
}
61+
5562
private String errorMessages() {
5663
final String result;
5764
if (this.successful) {

src/main/resources/templates/test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ <h1>Test results</h1>
2828
<tr>
2929
<th scope="col">Test</th>
3030
<th scope="col">Result</th>
31+
<th scope="col">Duration</th>
3132
<th scope="col">Message</th>
3233
</tr>
3334
</thead>

0 commit comments

Comments
 (0)