Skip to content

Commit 4d26a77

Browse files
jbachorikclaude
andcommitted
fix(bench): stabilize reapplyByIdAndBytes with Level.Iteration reset
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent a38d084 commit 4d26a77

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/throughput/ThreadContextBenchmark.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,27 @@ public void setup(ProfilerState ps) {
9898

9999
// Prime the normal path to obtain constant IDs, then snapshot for reapply.
100100
ctx.put(localRootSpanId, spanId, 0, spanId);
101-
for (int i = 0; i < ROUTES.length && i < 10; i++) {
102-
ctx.setContextAttribute(i % ROUTES.length, ROUTES[i % ROUTES.length]);
101+
for (int i = 0; i < ROUTES.length; i++) {
102+
ctx.setContextAttribute(i, ROUTES[i]);
103103
}
104104
constantIds = new int[10];
105105
ctx.copyCustoms(constantIds);
106106
utf8 = new byte[10][];
107-
for (int i = 0; i < ROUTES.length && i < 10; i++) {
108-
utf8[i] = ROUTES[i % ROUTES.length].getBytes(StandardCharsets.UTF_8);
107+
for (int i = 0; i < ROUTES.length; i++) {
108+
utf8[i] = ROUTES[i].getBytes(StandardCharsets.UTF_8);
109109
}
110110
}
111+
112+
@Setup(Level.Iteration)
113+
public void resetToSteadyState() {
114+
// Re-establish a live span (valid=1) and pre-populate attrs_data with all slots
115+
// before each measurement window. Without this, reapplyByIdAndBytes sees a
116+
// different attrs_data state on the first invocation of each iteration (empty
117+
// after put() in the previous iteration or after the trial setup), causing a
118+
// bimodal distribution across forks due to JIT profile divergence.
119+
ctx.put(localRootSpanId, spanId, 0, spanId);
120+
ctx.setContextAttributesByIdAndBytes(constantIds, utf8);
121+
}
111122
}
112123

113124
@Benchmark

0 commit comments

Comments
 (0)