Skip to content

Commit

Permalink
Perf benchmark tests for span creation ( along with context managemen…
Browse files Browse the repository at this point in the history
…t ) (#856)
  • Loading branch information
lalitb authored Jun 15, 2021
1 parent 8a8ffe5 commit b073e8b
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/test/trace/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ otel_cc_benchmark(
deps = ["//api"],
)

otel_cc_benchmark(
name = "span_benchmark",
srcs = ["span_benchmark.cc"],
deps = ["//api"],
)

cc_test(
name = "provider_test",
srcs = [
Expand Down
3 changes: 3 additions & 0 deletions api/test/trace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ endforeach()
add_executable(span_id_benchmark span_id_benchmark.cc)
target_link_libraries(span_id_benchmark benchmark::benchmark
${CMAKE_THREAD_LIBS_INIT} opentelemetry_api)
add_executable(span_benchmark span_benchmark.cc)
target_link_libraries(span_benchmark benchmark::benchmark
${CMAKE_THREAD_LIBS_INIT} opentelemetry_api)
128 changes: 128 additions & 0 deletions api/test/trace/span_benchmark.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/trace/noop.h"
#include "opentelemetry/trace/propagation/detail/context.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/trace_id.h"

#include <cstdint>

#include <benchmark/benchmark.h>

using opentelemetry::trace::SpanContext;
namespace trace_api = opentelemetry::trace;
namespace nostd = opentelemetry::nostd;

namespace
{

std::shared_ptr<opentelemetry::trace::Tracer> initTracer()
{
return std::shared_ptr<opentelemetry::trace::Tracer>(new trace_api::NoopTracer());
}

// Test to measure performance for span creation
void BM_SpanCreation(benchmark::State &state)
{
auto tracer = initTracer();
while (state.KeepRunning())
{
auto span = tracer->StartSpan("span");
span->End();
}
}
BENCHMARK(BM_SpanCreation);

// Test to measure performance for single span creation with scope
void BM_SpanCreationWithScope(benchmark::State &state)
{
auto tracer = initTracer();
while (state.KeepRunning())
{
auto span = tracer->StartSpan("span");
auto scope = tracer->WithActiveSpan(span);
span->End();
}
}
BENCHMARK(BM_SpanCreationWithScope);

// Test to measure performance for nested span creation with scope
void BM_NestedSpanCreationWithScope(benchmark::State &state)
{
auto tracer = initTracer();
while (state.KeepRunning())
{
auto span = tracer->StartSpan("outer");
auto scope = tracer->WithActiveSpan(span);
{
auto span = tracer->StartSpan("inner");
auto scope = tracer->WithActiveSpan(span);
{
auto span = tracer->StartSpan("innermost");
auto scope = tracer->WithActiveSpan(span);
span->End();
}
span->End();
}
span->End();
}
}

BENCHMARK(BM_NestedSpanCreationWithScope);

// Test to measure performance for nested span creation with manual span context management
void BM_SpanCreationWithManualSpanContextPropagation(benchmark::State &state)
{
auto tracer = initTracer();
constexpr uint8_t buf1[] = {1, 2, 3, 4, 5, 6, 7, 8};
trace_api::SpanId span_id(buf1);
constexpr uint8_t buf2[] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1};
trace_api::TraceId trace_id(buf2);

while (state.KeepRunning())
{
auto outer_span = nostd::shared_ptr<trace_api::Span>(
new trace_api::DefaultSpan(SpanContext(trace_id, span_id, trace_api::TraceFlags(), false)));
trace_api::StartSpanOptions options;
options.parent = outer_span->GetContext();
auto inner_span = tracer->StartSpan("inner", options);
auto inner_span_context = inner_span->GetContext();
options.parent = inner_span_context;
auto innermost_span = tracer->StartSpan("innermost", options);
innermost_span->End();
inner_span->End();
}
}
BENCHMARK(BM_SpanCreationWithManualSpanContextPropagation);

// Test to measure performance for nested span creation with context propagation
void BM_SpanCreationWitContextPropagation(benchmark::State &state)
{
auto tracer = initTracer();
constexpr uint8_t buf1[] = {1, 2, 3, 4, 5, 6, 7, 8};
trace_api::SpanId span_id(buf1);
constexpr uint8_t buf2[] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1};
trace_api::TraceId trace_id(buf2);

while (state.KeepRunning())
{
auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent();
auto outer_span_context = SpanContext(trace_id, span_id, trace_api::TraceFlags(), false);
auto outer_span =
nostd::shared_ptr<trace_api::Span>(new trace_api::DefaultSpan(outer_span_context));
trace_api::propagation::SetSpan(current_ctx, outer_span);
auto inner_child = tracer->StartSpan("inner");
auto scope = tracer->WithActiveSpan(inner_child);
{
auto innermost_child = tracer->StartSpan("innermost");
auto scope = tracer->WithActiveSpan(innermost_child);
innermost_child->End();
}
inner_child->End();
}
}
BENCHMARK(BM_SpanCreationWitContextPropagation);
} // namespace
BENCHMARK_MAIN();

0 comments on commit b073e8b

Please sign in to comment.