]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
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" | |
9 | ||
10 | #include <cstdint> | |
11 | ||
12 | #include <benchmark/benchmark.h> | |
13 | ||
14 | using opentelemetry::trace::SpanContext; | |
15 | namespace trace_api = opentelemetry::trace; | |
16 | namespace nostd = opentelemetry::nostd; | |
17 | namespace context = opentelemetry::context; | |
18 | ||
19 | namespace | |
20 | { | |
21 | ||
22 | std::shared_ptr<trace_api::Tracer> initTracer() | |
23 | { | |
24 | return std::shared_ptr<trace_api::Tracer>(new trace_api::NoopTracer()); | |
25 | } | |
26 | ||
27 | // Test to measure performance for span creation | |
28 | void BM_SpanCreation(benchmark::State &state) | |
29 | { | |
30 | auto tracer = initTracer(); | |
31 | while (state.KeepRunning()) | |
32 | { | |
33 | auto span = tracer->StartSpan("span"); | |
34 | span->End(); | |
35 | } | |
36 | } | |
37 | BENCHMARK(BM_SpanCreation); | |
38 | ||
39 | // Test to measure performance for single span creation with scope | |
40 | void BM_SpanCreationWithScope(benchmark::State &state) | |
41 | { | |
42 | auto tracer = initTracer(); | |
43 | while (state.KeepRunning()) | |
44 | { | |
45 | auto span = tracer->StartSpan("span"); | |
46 | auto scope = tracer->WithActiveSpan(span); | |
47 | span->End(); | |
48 | } | |
49 | } | |
50 | BENCHMARK(BM_SpanCreationWithScope); | |
51 | ||
52 | // Test to measure performance for nested span creation with scope | |
53 | void BM_NestedSpanCreationWithScope(benchmark::State &state) | |
54 | { | |
55 | auto tracer = initTracer(); | |
56 | while (state.KeepRunning()) | |
57 | { | |
58 | auto span = tracer->StartSpan("outer"); | |
59 | auto scope = tracer->WithActiveSpan(span); | |
60 | { | |
61 | auto span = tracer->StartSpan("inner"); | |
62 | auto scope = tracer->WithActiveSpan(span); | |
63 | { | |
64 | auto span = tracer->StartSpan("innermost"); | |
65 | auto scope = tracer->WithActiveSpan(span); | |
66 | span->End(); | |
67 | } | |
68 | span->End(); | |
69 | } | |
70 | span->End(); | |
71 | } | |
72 | } | |
73 | ||
74 | BENCHMARK(BM_NestedSpanCreationWithScope); | |
75 | ||
76 | // Test to measure performance for nested span creation with manual span context management | |
77 | void BM_SpanCreationWithManualSpanContextPropagation(benchmark::State &state) | |
78 | { | |
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); | |
84 | ||
85 | while (state.KeepRunning()) | |
86 | { | |
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(); | |
96 | inner_span->End(); | |
97 | } | |
98 | } | |
99 | BENCHMARK(BM_SpanCreationWithManualSpanContextPropagation); | |
100 | ||
101 | // Test to measure performance for nested span creation with context propagation | |
102 | void BM_SpanCreationWitContextPropagation(benchmark::State &state) | |
103 | { | |
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); | |
109 | ||
110 | while (state.KeepRunning()) | |
111 | { | |
112 | auto current_ctx = context::RuntimeContext::GetCurrent(); | |
113 | auto outer_span_context = SpanContext(trace_id, span_id, trace_api::TraceFlags(), false); | |
114 | auto outer_span = | |
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); | |
119 | { | |
120 | auto innermost_child = tracer->StartSpan("innermost"); | |
121 | auto scope = tracer->WithActiveSpan(innermost_child); | |
122 | innermost_child->End(); | |
123 | } | |
124 | inner_child->End(); | |
125 | } | |
126 | } | |
127 | BENCHMARK(BM_SpanCreationWitContextPropagation); | |
128 | } // namespace | |
129 | BENCHMARK_MAIN(); |