File tree Expand file tree Collapse file tree
dd-trace-core/src/jmh/java/datadog/trace/core Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments