1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
4 #include "opentelemetry/nostd/shared_ptr.h"
5 #include "opentelemetry/trace/context.h"
6 #include "opentelemetry/trace/noop.h"
7 #include "opentelemetry/trace/span_id.h"
8 #include "opentelemetry/trace/trace_id.h"
12 #include <benchmark/benchmark.h>
14 using opentelemetry::trace::SpanContext
;
15 namespace trace_api
= opentelemetry::trace
;
16 namespace nostd
= opentelemetry::nostd
;
17 namespace context
= opentelemetry::context
;
22 std::shared_ptr
<trace_api::Tracer
> initTracer()
24 return std::shared_ptr
<trace_api::Tracer
>(new trace_api::NoopTracer());
27 // Test to measure performance for span creation
28 void BM_SpanCreation(benchmark::State
&state
)
30 auto tracer
= initTracer();
31 while (state
.KeepRunning())
33 auto span
= tracer
->StartSpan("span");
37 BENCHMARK(BM_SpanCreation
);
39 // Test to measure performance for single span creation with scope
40 void BM_SpanCreationWithScope(benchmark::State
&state
)
42 auto tracer
= initTracer();
43 while (state
.KeepRunning())
45 auto span
= tracer
->StartSpan("span");
46 auto scope
= tracer
->WithActiveSpan(span
);
50 BENCHMARK(BM_SpanCreationWithScope
);
52 // Test to measure performance for nested span creation with scope
53 void BM_NestedSpanCreationWithScope(benchmark::State
&state
)
55 auto tracer
= initTracer();
56 while (state
.KeepRunning())
58 auto span
= tracer
->StartSpan("outer");
59 auto scope
= tracer
->WithActiveSpan(span
);
61 auto span
= tracer
->StartSpan("inner");
62 auto scope
= tracer
->WithActiveSpan(span
);
64 auto span
= tracer
->StartSpan("innermost");
65 auto scope
= tracer
->WithActiveSpan(span
);
74 BENCHMARK(BM_NestedSpanCreationWithScope
);
76 // Test to measure performance for nested span creation with manual span context management
77 void BM_SpanCreationWithManualSpanContextPropagation(benchmark::State
&state
)
79 auto tracer
= initTracer();
80 constexpr uint8_t buf1
[] = {1, 2, 3, 4, 5, 6, 7, 8};
81 trace_api::SpanId
span_id(buf1
);
82 constexpr uint8_t buf2
[] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1};
83 trace_api::TraceId
trace_id(buf2
);
85 while (state
.KeepRunning())
87 auto outer_span
= nostd::shared_ptr
<trace_api::Span
>(
88 new trace_api::DefaultSpan(SpanContext(trace_id
, span_id
, trace_api::TraceFlags(), false)));
89 trace_api::StartSpanOptions options
;
90 options
.parent
= outer_span
->GetContext();
91 auto inner_span
= tracer
->StartSpan("inner", options
);
92 auto inner_span_context
= inner_span
->GetContext();
93 options
.parent
= inner_span_context
;
94 auto innermost_span
= tracer
->StartSpan("innermost", options
);
95 innermost_span
->End();
99 BENCHMARK(BM_SpanCreationWithManualSpanContextPropagation
);
101 // Test to measure performance for nested span creation with context propagation
102 void BM_SpanCreationWitContextPropagation(benchmark::State
&state
)
104 auto tracer
= initTracer();
105 constexpr uint8_t buf1
[] = {1, 2, 3, 4, 5, 6, 7, 8};
106 trace_api::SpanId
span_id(buf1
);
107 constexpr uint8_t buf2
[] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1};
108 trace_api::TraceId
trace_id(buf2
);
110 while (state
.KeepRunning())
112 auto current_ctx
= context::RuntimeContext::GetCurrent();
113 auto outer_span_context
= SpanContext(trace_id
, span_id
, trace_api::TraceFlags(), false);
115 nostd::shared_ptr
<trace_api::Span
>(new trace_api::DefaultSpan(outer_span_context
));
116 trace_api::SetSpan(current_ctx
, outer_span
);
117 auto inner_child
= tracer
->StartSpan("inner");
118 auto scope
= tracer
->WithActiveSpan(inner_child
);
120 auto innermost_child
= tracer
->StartSpan("innermost");
121 auto scope
= tracer
->WithActiveSpan(innermost_child
);
122 innermost_child
->End();
127 BENCHMARK(BM_SpanCreationWitContextPropagation
);