Skip to content

Commit 2e6fda2

Browse files
bm1549claude
andcommitted
Add startAndFinishSpan and contended benchmarks to TimeSourceBenchmark
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f852039 commit 2e6fda2

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

dd-trace-core/src/jmh/java/datadog/trace/core/TimeSourceBenchmark.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,40 @@ public long systemCurrentTimeMillis() {
6060
public long traceGetTimeWithNanoTicks() {
6161
return TRACER.getTimeWithNanoTicks(System.nanoTime());
6262
}
63+
64+
/**
65+
* Measures a full span start + finish cycle, exercising both the {@code rootSpan} CAS guard in
66+
* {@link PendingTrace#registerSpan} and the {@code lazySet} of {@code lastReferenced} in {@link
67+
* PendingTrace#getCurrentTimeNano}.
68+
*/
69+
@Benchmark
70+
public void startAndFinishSpan() {
71+
TRACER.startSpan("benchmark", "op").finish();
72+
}
73+
74+
@State(Scope.Benchmark)
75+
public static class SharedState {
76+
PendingTrace sharedTrace;
77+
78+
@Setup(Level.Trial)
79+
public void setup() {
80+
TraceCollector collector = TRACER.createTraceCollector(DDTraceId.ONE);
81+
sharedTrace = (PendingTrace) collector;
82+
}
83+
84+
@TearDown(Level.Trial)
85+
public void teardown() {
86+
sharedTrace = null;
87+
}
88+
}
89+
90+
/**
91+
* Measures {@link PendingTrace#getCurrentTimeNano()} under cross-thread contention on a single
92+
* shared {@code PendingTrace}. All threads write to the same {@code lastReferenced} field,
93+
* demonstrating the benefit of {@code lazySet} over a volatile store under contention.
94+
*/
95+
@Benchmark
96+
public long getCurrentTimeNano_contended(SharedState shared) {
97+
return shared.sharedTrace.getCurrentTimeNano();
98+
}
6399
}

0 commit comments

Comments
 (0)