-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMeasurementCountGateTest.java
More file actions
115 lines (104 loc) · 5.03 KB
/
Copy pathMeasurementCountGateTest.java
File metadata and controls
115 lines (104 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.demcha.compose;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Deterministic regression gate for how much measurement work the layout pass
* does, driving {@link MeasurementCountBenchmark}'s real compile through a
* {@link CountingTextMeasurementSystem}.
*
* <p>These counts are exact integers, not timings. A wall-clock benchmark can
* only say "slower on this machine today"; it cannot separate a slower laptop
* from a slower engine, and an absolute millisecond ceiling generous enough to
* survive CI variance is generous enough to miss a real regression. Asking the
* layout pass how many width measurements it requested has neither problem: the
* answer is identical on every machine and moves only when the engine's
* behaviour actually changes.</p>
*
* <p>Complementary to the layout snapshots rather than a duplicate of them. Two
* implementations that place fragments identically but measure a different
* number of times both satisfy every snapshot; only this gate separates
* them.</p>
*
* <p><b>When this fails</b>, the engine changed how it measures text. Decide
* whether that was intended: fewer requests for the same document is an
* improvement worth recording, more is a regression worth explaining. Either
* way the fix is to run {@code MeasurementCountBenchmark} and update the
* expectation below in the same commit as the behaviour change, so the number
* moves deliberately.</p>
*
* <p>Allocation is deliberately not gated. It is stable to about 0.1% across
* runs on one JVM, but it is a property of the JDK as much as of the engine, so
* an expectation recorded on one machine would be a guess about another. The
* probe still reports it.</p>
*/
class MeasurementCountGateTest {
/**
* Exact measurement work per scenario, recorded from
* {@link MeasurementCountBenchmark} on the 2.1.1 development line.
*/
private static final Map<String, Work> EXPECTED = expected();
private static Map<String, Work> expected() {
Map<String, Work> work = new LinkedHashMap<>();
work.put("long-text", new Work(4472, 32, 32457, 124, 1, 2));
work.put("long-token", new Work(99, 55, 7739, 600, 1, 1));
work.put("accented-latin", new Work(2499, 37, 14393, 123, 1, 1));
work.put("large-table", new Work(1206, 211, 5916, 13, 0, 6));
return work;
}
/**
* The measurement work one scenario costs the layout pass.
*
* @param widthRequests total text-width measurements requested
* @param distinctWidthRequests distinct (style, text) pairs among them
* @param summedRequestChars characters across all requests
* @param maxRequestChars longest single measured string
* @param lineMetricsCalls line-metric lookups
* @param pages pages the document compiled to
*/
private record Work(long widthRequests,
long distinctWidthRequests,
long summedRequestChars,
long maxRequestChars,
long lineMetricsCalls,
int pages) {
static Work of(MeasurementCountBenchmark.Result result) {
CountingTextMeasurementSystem.Counts counts = result.counts();
return new Work(counts.widthRequests(),
counts.distinctWidthRequests(),
counts.summedRequestChars(),
counts.maxRequestChars(),
counts.lineMetricsCalls(),
result.pages());
}
}
@Test
void everyScenarioMeasuresExactlyAsMuchTextAsRecorded() throws Exception {
// Soft, so a deliberate engine change reports all four scenarios' new
// numbers in one run instead of surfacing them one failure at a time.
SoftAssertions soft = new SoftAssertions();
for (var scenario : MeasurementCountBenchmark.scenarios().entrySet()) {
Work actual = Work.of(
MeasurementCountBenchmark.measureScenario(scenario.getKey(), scenario.getValue()));
soft.assertThat(actual)
.describedAs("measurement work for '%s' changed - re-run "
+ "MeasurementCountBenchmark and update the expectation "
+ "in the same commit if the change was intended",
scenario.getKey())
.isEqualTo(EXPECTED.get(scenario.getKey()));
}
soft.assertAll();
}
/**
* A scenario added to the probe without an expectation would be measured and
* silently ignored, which is the failure mode that lets coverage rot.
*/
@Test
void everyProbeScenarioIsGated() {
assertThat(MeasurementCountBenchmark.scenarios().keySet())
.describedAs("probe scenarios missing a recorded expectation")
.containsExactlyInAnyOrderElementsOf(EXPECTED.keySet());
}
}