From bc2141a78364d91125fccb6b564724380968cc50 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Wed, 15 Jul 2020 11:31:05 -0400 Subject: [PATCH 01/10] Add initial benchmark tests --- sdk/test/trace/BUILD | 11 +++ sdk/test/trace/CMakeLists.txt | 4 + sdk/test/trace/sampler_benchmark.cc | 128 ++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 sdk/test/trace/sampler_benchmark.cc diff --git a/sdk/test/trace/BUILD b/sdk/test/trace/BUILD index 280acc4fe3..e9e93f3e35 100644 --- a/sdk/test/trace/BUILD +++ b/sdk/test/trace/BUILD @@ -1,3 +1,5 @@ +load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") + cc_test( name = "tracer_provider_test", srcs = [ @@ -86,3 +88,12 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +otel_cc_benchmark( + name = "sampler_benchmark", + srcs = ["sampler_benchmark.cc"], + deps = [ + "//sdk/src/trace", + ], +) + diff --git a/sdk/test/trace/CMakeLists.txt b/sdk/test/trace/CMakeLists.txt index 8326d62a7b..f3cf3b299d 100644 --- a/sdk/test/trace/CMakeLists.txt +++ b/sdk/test/trace/CMakeLists.txt @@ -6,3 +6,7 @@ foreach(testname tracer_provider_test span_data_test simple_processor_test ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common opentelemetry_trace) gtest_add_tests(TARGET ${testname} TEST_PREFIX trace. TEST_LIST ${testname}) endforeach() + +add_executable(sampler_benchmark sampler_benchmark.cc) +target_link_libraries(sampler_benchmark benchmark::benchmark + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace) \ No newline at end of file diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc new file mode 100644 index 0000000000..4650cbe8ee --- /dev/null +++ b/sdk/test/trace/sampler_benchmark.cc @@ -0,0 +1,128 @@ +#include "opentelemetry/sdk/trace/samplers/always_off.h" +#include "opentelemetry/sdk/trace/samplers/always_on.h" +#include "opentelemetry/sdk/trace/samplers/parent_or_else.h" +#include "opentelemetry/sdk/trace/samplers/probability.h" + +#include + +#include + +namespace +{ +using opentelemetry::sdk::trace::AlwaysOffSampler; +using opentelemetry::sdk::trace::AlwaysOnSampler; +using opentelemetry::sdk::trace::ParentOrElseSampler; +using opentelemetry::sdk::trace::ProbabilitySampler; +using opentelemetry::sdk::trace::Decision; +using opentelemetry::trace::SpanContext; + +void BM_AlwaysOffSamplerConstruction(benchmark::State &state) +{ + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(AlwaysOffSampler()); + } +} +BENCHMARK(BM_AlwaysOffSamplerConstruction); + +void BM_AlwaysOnSamplerConstruction(benchmark::State &state) +{ + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(AlwaysOnSampler()); + } +} +BENCHMARK(BM_AlwaysOnSamplerConstruction); + +void BM_ParentOrElseSamplerConstruction(benchmark::State &state) +{ + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(ParentOrElseSampler(std::make_shared())); + } +} +BENCHMARK(BM_ParentOrElseSamplerConstruction); + +void BM_ProbabilitySamplerConstruction(benchmark::State &state) +{ + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(ProbabilitySampler(0.01)); + } +} +BENCHMARK(BM_ProbabilitySamplerConstruction); + +void BM_AlwaysOffSamplerShouldSample(benchmark::State &state) +{ + AlwaysOffSampler sampler; + + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; + + using M = std::map; + M m1 = {{}}; + opentelemetry::trace::KeyValueIterableView view{m1}; + + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); + } +} +BENCHMARK(BM_AlwaysOffSamplerShouldSample); + +void BM_AlwaysOnSamplerShouldSample(benchmark::State &state) +{ + AlwaysOnSampler sampler; + + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; + + using M = std::map; + M m1 = {{}}; + opentelemetry::trace::KeyValueIterableView view{m1}; + + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); + } +} +BENCHMARK(BM_AlwaysOnSamplerShouldSample); + +void BM_ParentOrElseSamplerShouldSample(benchmark::State &state) +{ + ParentOrElseSampler sampler(std::make_shared()); + + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; + + using M = std::map; + M m1 = {{}}; + opentelemetry::trace::KeyValueIterableView view{m1}; + + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); + } +} +BENCHMARK(BM_ParentOrElseSamplerShouldSample); + +void BM_ProbabilitySamplerShouldSample(benchmark::State &state) +{ + ProbabilitySampler sampler(0.01); + + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; + + using M = std::map; + M m1 = {{}}; + opentelemetry::trace::KeyValueIterableView view{m1}; + + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); + } +} +BENCHMARK(BM_ProbabilitySamplerShouldSample); + +} // namespace +BENCHMARK_MAIN(); \ No newline at end of file From d5f51294c3b3223bb238af0c7eaef278773cf235 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Wed, 15 Jul 2020 11:32:57 -0400 Subject: [PATCH 02/10] Add newline to eof --- sdk/test/trace/CMakeLists.txt | 2 +- sdk/test/trace/sampler_benchmark.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/test/trace/CMakeLists.txt b/sdk/test/trace/CMakeLists.txt index f3cf3b299d..7060170c67 100644 --- a/sdk/test/trace/CMakeLists.txt +++ b/sdk/test/trace/CMakeLists.txt @@ -9,4 +9,4 @@ endforeach() add_executable(sampler_benchmark sampler_benchmark.cc) target_link_libraries(sampler_benchmark benchmark::benchmark - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace) \ No newline at end of file + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace) diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc index 4650cbe8ee..5076b22953 100644 --- a/sdk/test/trace/sampler_benchmark.cc +++ b/sdk/test/trace/sampler_benchmark.cc @@ -125,4 +125,4 @@ void BM_ProbabilitySamplerShouldSample(benchmark::State &state) BENCHMARK(BM_ProbabilitySamplerShouldSample); } // namespace -BENCHMARK_MAIN(); \ No newline at end of file +BENCHMARK_MAIN(); From 6bb791b9dac935ce1221f5dff79c59c4a60f624a Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Wed, 15 Jul 2020 11:47:20 -0400 Subject: [PATCH 03/10] Reformat BUILD file --- sdk/test/trace/BUILD | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sdk/test/trace/BUILD b/sdk/test/trace/BUILD index e9e93f3e35..62aa234d66 100644 --- a/sdk/test/trace/BUILD +++ b/sdk/test/trace/BUILD @@ -92,8 +92,5 @@ cc_test( otel_cc_benchmark( name = "sampler_benchmark", srcs = ["sampler_benchmark.cc"], - deps = [ - "//sdk/src/trace", - ], + deps = ["//sdk/src/trace"], ) - From bcf0a8ab852ae0bb860fbf899709c3e54cca89e5 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Wed, 15 Jul 2020 13:17:30 -0400 Subject: [PATCH 04/10] Add comments --- sdk/test/trace/sampler_benchmark.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc index 5076b22953..5562bf0a27 100644 --- a/sdk/test/trace/sampler_benchmark.cc +++ b/sdk/test/trace/sampler_benchmark.cc @@ -16,6 +16,7 @@ using opentelemetry::sdk::trace::ProbabilitySampler; using opentelemetry::sdk::trace::Decision; using opentelemetry::trace::SpanContext; +// NOOP sampler constructor used as a baseline to compare with other samplers void BM_AlwaysOffSamplerConstruction(benchmark::State &state) { while (state.KeepRunning()) @@ -25,6 +26,7 @@ void BM_AlwaysOffSamplerConstruction(benchmark::State &state) } BENCHMARK(BM_AlwaysOffSamplerConstruction); +// NOOP sampler constructor used as a baseline to compare with other samplers void BM_AlwaysOnSamplerConstruction(benchmark::State &state) { while (state.KeepRunning()) @@ -52,6 +54,7 @@ void BM_ProbabilitySamplerConstruction(benchmark::State &state) } BENCHMARK(BM_ProbabilitySamplerConstruction); +// NOOP sampler used as a baseline to compare with other samplers void BM_AlwaysOffSamplerShouldSample(benchmark::State &state) { AlwaysOffSampler sampler; @@ -70,6 +73,7 @@ void BM_AlwaysOffSamplerShouldSample(benchmark::State &state) } BENCHMARK(BM_AlwaysOffSamplerShouldSample); +// NOOP sampler used as a baseline to compare with other samplers void BM_AlwaysOnSamplerShouldSample(benchmark::State &state) { AlwaysOnSampler sampler; From bf1540b4fcddd924eadef572131b686487417f81 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Wed, 15 Jul 2020 15:45:25 -0400 Subject: [PATCH 05/10] Add tests to measure span creation time --- sdk/test/trace/sampler_benchmark.cc | 123 +++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 10 deletions(-) diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc index 5562bf0a27..0cb22ad58f 100644 --- a/sdk/test/trace/sampler_benchmark.cc +++ b/sdk/test/trace/sampler_benchmark.cc @@ -1,22 +1,82 @@ +#include "opentelemetry/sdk/trace/tracer.h" #include "opentelemetry/sdk/trace/samplers/always_off.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" #include "opentelemetry/sdk/trace/samplers/parent_or_else.h" #include "opentelemetry/sdk/trace/samplers/probability.h" +#include "opentelemetry/sdk/trace/simple_processor.h" +#include "opentelemetry/sdk/trace/span_data.h" #include #include +using namespace opentelemetry::sdk::trace; +namespace nostd = opentelemetry::nostd; +namespace common = opentelemetry::common; +using opentelemetry::trace::SpanContext; + +/** + * A mock sampler that returns non-empty sampling results attributes. + */ +class MockSampler final : public Sampler +{ +public: + SamplingResult ShouldSample(const SpanContext * /*parent_context*/, + trace_api::TraceId /*trace_id*/, + nostd::string_view /*name*/, + trace_api::SpanKind /*span_kind*/, + const trace_api::KeyValueIterable & /*attributes*/) noexcept override + { + // Return two pairs of attributes. These attributes should be added to the span attributes + return {Decision::RECORD_AND_SAMPLE, + nostd::unique_ptr>( + new const std::map( + {{"sampling_attr1", 123}, {"sampling_attr2", "string"}}))}; + } + + std::string GetDescription() const noexcept override { return "MockSampler"; } +}; + +/** + * A mock exporter that switches a flag once a valid recordable was received. + */ +class MockSpanExporter final : public SpanExporter +{ +public: + MockSpanExporter(std::shared_ptr>> spans_received) noexcept + : spans_received_(spans_received) + {} + + std::unique_ptr MakeRecordable() noexcept override + { + return std::unique_ptr(new SpanData); + } + + ExportResult Export(const nostd::span> &recordables) noexcept override + { + for (auto &recordable : recordables) + { + auto span = std::unique_ptr(static_cast(recordable.release())); + if (span != nullptr) + { + spans_received_->push_back(std::move(span)); + } + } + + return ExportResult::kSuccess; + } + + void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override + {} + +private: + std::shared_ptr>> spans_received_; +}; + namespace { -using opentelemetry::sdk::trace::AlwaysOffSampler; -using opentelemetry::sdk::trace::AlwaysOnSampler; -using opentelemetry::sdk::trace::ParentOrElseSampler; -using opentelemetry::sdk::trace::ProbabilitySampler; -using opentelemetry::sdk::trace::Decision; -using opentelemetry::trace::SpanContext; -// NOOP sampler constructor used as a baseline to compare with other samplers +// Sampler constructor used as a baseline to compare with other samplers void BM_AlwaysOffSamplerConstruction(benchmark::State &state) { while (state.KeepRunning()) @@ -26,7 +86,7 @@ void BM_AlwaysOffSamplerConstruction(benchmark::State &state) } BENCHMARK(BM_AlwaysOffSamplerConstruction); -// NOOP sampler constructor used as a baseline to compare with other samplers +// Sampler constructor used as a baseline to compare with other samplers void BM_AlwaysOnSamplerConstruction(benchmark::State &state) { while (state.KeepRunning()) @@ -54,7 +114,7 @@ void BM_ProbabilitySamplerConstruction(benchmark::State &state) } BENCHMARK(BM_ProbabilitySamplerConstruction); -// NOOP sampler used as a baseline to compare with other samplers +// Sampler used as a baseline to compare with other samplers void BM_AlwaysOffSamplerShouldSample(benchmark::State &state) { AlwaysOffSampler sampler; @@ -73,7 +133,7 @@ void BM_AlwaysOffSamplerShouldSample(benchmark::State &state) } BENCHMARK(BM_AlwaysOffSamplerShouldSample); -// NOOP sampler used as a baseline to compare with other samplers +// Sampler used as a baseline to compare with other samplers void BM_AlwaysOnSamplerShouldSample(benchmark::State &state) { AlwaysOnSampler sampler; @@ -128,5 +188,48 @@ void BM_ProbabilitySamplerShouldSample(benchmark::State &state) } BENCHMARK(BM_ProbabilitySamplerShouldSample); +// Test to measure performance for span creation +void BM_SpanCreation(benchmark::State &state) +{ + std::shared_ptr>> spans_received_off( + new std::vector>); + + std::unique_ptr exporter(new MockSpanExporter(spans_received_off)); + auto processor = std::make_shared(std::move(exporter)); + auto tracer_off = std::shared_ptr(new Tracer(processor, std::make_shared())); + + while (state.KeepRunning()) + { + auto span_off_1 = tracer_off->StartSpan("span with AlwaysOn sampler"); + + span_off_1->SetAttribute("attr1", 3.1); // Not recorded. + + span_off_1->End(); + } +} +BENCHMARK(BM_SpanCreation); + +// Test to measure performance overhead for no-op span creation +void BM_NoopSpanCreation(benchmark::State &state) +{ + std::shared_ptr>> spans_received_off( + new std::vector>); + + std::unique_ptr exporter(new MockSpanExporter(spans_received_off)); + auto processor = std::make_shared(std::move(exporter)); + auto tracer_off = std::shared_ptr(new Tracer(processor, std::make_shared())); + + while (state.KeepRunning()) + { + auto span_off_1 = tracer_off->StartSpan("span with AlwaysOff sampler"); + + span_off_1->SetAttribute("attr1", 3.1); // Not recorded. + + span_off_1->End(); + } +} +BENCHMARK(BM_NoopSpanCreation); + + } // namespace BENCHMARK_MAIN(); From de090b31058fa594b1397358ecd830282e08ee42 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Fri, 17 Jul 2020 09:10:26 -0400 Subject: [PATCH 06/10] Update comments --- sdk/test/trace/sampler_benchmark.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc index 0cb22ad58f..ab32e353c4 100644 --- a/sdk/test/trace/sampler_benchmark.cc +++ b/sdk/test/trace/sampler_benchmark.cc @@ -202,7 +202,7 @@ void BM_SpanCreation(benchmark::State &state) { auto span_off_1 = tracer_off->StartSpan("span with AlwaysOn sampler"); - span_off_1->SetAttribute("attr1", 3.1); // Not recorded. + span_off_1->SetAttribute("attr1", 3.1); span_off_1->End(); } From bb4a46855be67725f9aaf9b243691f72882ed372 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Fri, 17 Jul 2020 09:37:30 -0400 Subject: [PATCH 07/10] Move duplicated tests to helper function --- sdk/test/trace/sampler_benchmark.cc | 64 +++++++++-------------------- 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc index ab32e353c4..75df7be250 100644 --- a/sdk/test/trace/sampler_benchmark.cc +++ b/sdk/test/trace/sampler_benchmark.cc @@ -1,4 +1,5 @@ #include "opentelemetry/sdk/trace/tracer.h" +#include "opentelemetry/sdk/trace/sampler.h" #include "opentelemetry/sdk/trace/samplers/always_off.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" #include "opentelemetry/sdk/trace/samplers/parent_or_else.h" @@ -73,6 +74,21 @@ class MockSpanExporter final : public SpanExporter std::shared_ptr>> spans_received_; }; +void BenchmarkShouldSampler(Sampler &sampler, benchmark::State &state) +{ + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; + + using M = std::map; + M m1 = {{}}; + opentelemetry::trace::KeyValueIterableView view{m1}; + + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); + } +} + namespace { @@ -119,17 +135,7 @@ void BM_AlwaysOffSamplerShouldSample(benchmark::State &state) { AlwaysOffSampler sampler; - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - - using M = std::map; - M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; - - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); - } + BenchmarkShouldSampler(sampler, state); } BENCHMARK(BM_AlwaysOffSamplerShouldSample); @@ -138,17 +144,7 @@ void BM_AlwaysOnSamplerShouldSample(benchmark::State &state) { AlwaysOnSampler sampler; - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - - using M = std::map; - M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; - - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); - } + BenchmarkShouldSampler(sampler, state); } BENCHMARK(BM_AlwaysOnSamplerShouldSample); @@ -156,17 +152,7 @@ void BM_ParentOrElseSamplerShouldSample(benchmark::State &state) { ParentOrElseSampler sampler(std::make_shared()); - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - - using M = std::map; - M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; - - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); - } + BenchmarkShouldSampler(sampler, state); } BENCHMARK(BM_ParentOrElseSamplerShouldSample); @@ -174,17 +160,7 @@ void BM_ProbabilitySamplerShouldSample(benchmark::State &state) { ProbabilitySampler sampler(0.01); - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - - using M = std::map; - M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; - - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); - } + BenchmarkShouldSampler(sampler, state); } BENCHMARK(BM_ProbabilitySamplerShouldSample); From d3461664f9eacbef252192f4a94dbb0c6f43ec6e Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Mon, 20 Jul 2020 06:35:52 -0400 Subject: [PATCH 08/10] Update helper functions --- sdk/test/trace/sampler_benchmark.cc | 93 ++++++++++------------------- 1 file changed, 31 insertions(+), 62 deletions(-) diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc index 75df7be250..fdc02fdede 100644 --- a/sdk/test/trace/sampler_benchmark.cc +++ b/sdk/test/trace/sampler_benchmark.cc @@ -16,28 +16,6 @@ namespace nostd = opentelemetry::nostd; namespace common = opentelemetry::common; using opentelemetry::trace::SpanContext; -/** - * A mock sampler that returns non-empty sampling results attributes. - */ -class MockSampler final : public Sampler -{ -public: - SamplingResult ShouldSample(const SpanContext * /*parent_context*/, - trace_api::TraceId /*trace_id*/, - nostd::string_view /*name*/, - trace_api::SpanKind /*span_kind*/, - const trace_api::KeyValueIterable & /*attributes*/) noexcept override - { - // Return two pairs of attributes. These attributes should be added to the span attributes - return {Decision::RECORD_AND_SAMPLE, - nostd::unique_ptr>( - new const std::map( - {{"sampling_attr1", 123}, {"sampling_attr2", "string"}}))}; - } - - std::string GetDescription() const noexcept override { return "MockSampler"; } -}; - /** * A mock exporter that switches a flag once a valid recordable was received. */ @@ -74,24 +52,8 @@ class MockSpanExporter final : public SpanExporter std::shared_ptr>> spans_received_; }; -void BenchmarkShouldSampler(Sampler &sampler, benchmark::State &state) -{ - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - - using M = std::map; - M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; - - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); - } -} - namespace { - // Sampler constructor used as a baseline to compare with other samplers void BM_AlwaysOffSamplerConstruction(benchmark::State &state) { @@ -130,6 +92,22 @@ void BM_ProbabilitySamplerConstruction(benchmark::State &state) } BENCHMARK(BM_ProbabilitySamplerConstruction); +// Sampler Helper Function +void BenchmarkShouldSampler(Sampler &sampler, benchmark::State &state) +{ + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; + + using M = std::map; + M m1 = {{}}; + opentelemetry::trace::KeyValueIterableView view{m1}; + + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); + } +} + // Sampler used as a baseline to compare with other samplers void BM_AlwaysOffSamplerShouldSample(benchmark::State &state) { @@ -164,48 +142,39 @@ void BM_ProbabilitySamplerShouldSample(benchmark::State &state) } BENCHMARK(BM_ProbabilitySamplerShouldSample); -// Test to measure performance for span creation -void BM_SpanCreation(benchmark::State &state) +// Sampler Helper Function +void BenchmarkSpanCreation(std::shared_ptr sampler, benchmark::State &state) { - std::shared_ptr>> spans_received_off( + std::shared_ptr>> spans_received( new std::vector>); - std::unique_ptr exporter(new MockSpanExporter(spans_received_off)); + std::unique_ptr exporter(new MockSpanExporter(spans_received)); auto processor = std::make_shared(std::move(exporter)); - auto tracer_off = std::shared_ptr(new Tracer(processor, std::make_shared())); + auto tracer = std::shared_ptr(new Tracer(processor, sampler)); while (state.KeepRunning()) { - auto span_off_1 = tracer_off->StartSpan("span with AlwaysOn sampler"); + auto span = tracer->StartSpan("span"); - span_off_1->SetAttribute("attr1", 3.1); + span->SetAttribute("attr1", 3.1); - span_off_1->End(); + span->End(); } } + +// Test to measure performance for span creation +void BM_SpanCreation(benchmark::State &state) +{ + BenchmarkSpanCreation(std::move(std::make_shared()), state); +} BENCHMARK(BM_SpanCreation); // Test to measure performance overhead for no-op span creation void BM_NoopSpanCreation(benchmark::State &state) { - std::shared_ptr>> spans_received_off( - new std::vector>); - - std::unique_ptr exporter(new MockSpanExporter(spans_received_off)); - auto processor = std::make_shared(std::move(exporter)); - auto tracer_off = std::shared_ptr(new Tracer(processor, std::make_shared())); - - while (state.KeepRunning()) - { - auto span_off_1 = tracer_off->StartSpan("span with AlwaysOff sampler"); - - span_off_1->SetAttribute("attr1", 3.1); // Not recorded. - - span_off_1->End(); - } + BenchmarkSpanCreation(std::move(std::make_shared()), state); } BENCHMARK(BM_NoopSpanCreation); - } // namespace BENCHMARK_MAIN(); From a974812d17be12104ea28d03c8f2b2ff11cd7880 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Mon, 20 Jul 2020 17:00:28 -0400 Subject: [PATCH 09/10] Format benchmark --- sdk/test/trace/sampler_benchmark.cc | 96 ++++++++++++++--------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc index fdc02fdede..2dad8c8128 100644 --- a/sdk/test/trace/sampler_benchmark.cc +++ b/sdk/test/trace/sampler_benchmark.cc @@ -1,4 +1,3 @@ -#include "opentelemetry/sdk/trace/tracer.h" #include "opentelemetry/sdk/trace/sampler.h" #include "opentelemetry/sdk/trace/samplers/always_off.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" @@ -6,6 +5,7 @@ #include "opentelemetry/sdk/trace/samplers/probability.h" #include "opentelemetry/sdk/trace/simple_processor.h" #include "opentelemetry/sdk/trace/span_data.h" +#include "opentelemetry/sdk/trace/tracer.h" #include @@ -57,124 +57,124 @@ namespace // Sampler constructor used as a baseline to compare with other samplers void BM_AlwaysOffSamplerConstruction(benchmark::State &state) { - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(AlwaysOffSampler()); - } + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(AlwaysOffSampler()); + } } BENCHMARK(BM_AlwaysOffSamplerConstruction); // Sampler constructor used as a baseline to compare with other samplers void BM_AlwaysOnSamplerConstruction(benchmark::State &state) { - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(AlwaysOnSampler()); - } + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(AlwaysOnSampler()); + } } BENCHMARK(BM_AlwaysOnSamplerConstruction); void BM_ParentOrElseSamplerConstruction(benchmark::State &state) { - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(ParentOrElseSampler(std::make_shared())); - } + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(ParentOrElseSampler(std::make_shared())); + } } BENCHMARK(BM_ParentOrElseSamplerConstruction); void BM_ProbabilitySamplerConstruction(benchmark::State &state) { - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(ProbabilitySampler(0.01)); - } + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(ProbabilitySampler(0.01)); + } } BENCHMARK(BM_ProbabilitySamplerConstruction); // Sampler Helper Function void BenchmarkShouldSampler(Sampler &sampler, benchmark::State &state) { - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; - using M = std::map; - M m1 = {{}}; - opentelemetry::trace::KeyValueIterableView view{m1}; + using M = std::map; + M m1 = {{}}; + opentelemetry::trace::KeyValueIterableView view{m1}; - while (state.KeepRunning()) - { - benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); - } + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(sampler.ShouldSample(nullptr, trace_id, "", span_kind, view)); + } } // Sampler used as a baseline to compare with other samplers void BM_AlwaysOffSamplerShouldSample(benchmark::State &state) { - AlwaysOffSampler sampler; + AlwaysOffSampler sampler; - BenchmarkShouldSampler(sampler, state); + BenchmarkShouldSampler(sampler, state); } BENCHMARK(BM_AlwaysOffSamplerShouldSample); // Sampler used as a baseline to compare with other samplers void BM_AlwaysOnSamplerShouldSample(benchmark::State &state) { - AlwaysOnSampler sampler; + AlwaysOnSampler sampler; - BenchmarkShouldSampler(sampler, state); + BenchmarkShouldSampler(sampler, state); } BENCHMARK(BM_AlwaysOnSamplerShouldSample); void BM_ParentOrElseSamplerShouldSample(benchmark::State &state) { - ParentOrElseSampler sampler(std::make_shared()); + ParentOrElseSampler sampler(std::make_shared()); - BenchmarkShouldSampler(sampler, state); + BenchmarkShouldSampler(sampler, state); } BENCHMARK(BM_ParentOrElseSamplerShouldSample); void BM_ProbabilitySamplerShouldSample(benchmark::State &state) { - ProbabilitySampler sampler(0.01); + ProbabilitySampler sampler(0.01); - BenchmarkShouldSampler(sampler, state); + BenchmarkShouldSampler(sampler, state); } BENCHMARK(BM_ProbabilitySamplerShouldSample); // Sampler Helper Function void BenchmarkSpanCreation(std::shared_ptr sampler, benchmark::State &state) { - std::shared_ptr>> spans_received( - new std::vector>); + std::shared_ptr>> spans_received( + new std::vector>); - std::unique_ptr exporter(new MockSpanExporter(spans_received)); - auto processor = std::make_shared(std::move(exporter)); - auto tracer = std::shared_ptr(new Tracer(processor, sampler)); + std::unique_ptr exporter(new MockSpanExporter(spans_received)); + auto processor = std::make_shared(std::move(exporter)); + auto tracer = std::shared_ptr(new Tracer(processor, sampler)); - while (state.KeepRunning()) - { - auto span = tracer->StartSpan("span"); + while (state.KeepRunning()) + { + auto span = tracer->StartSpan("span"); - span->SetAttribute("attr1", 3.1); + span->SetAttribute("attr1", 3.1); - span->End(); - } + span->End(); + } } // Test to measure performance for span creation void BM_SpanCreation(benchmark::State &state) { - BenchmarkSpanCreation(std::move(std::make_shared()), state); + BenchmarkSpanCreation(std::move(std::make_shared()), state); } BENCHMARK(BM_SpanCreation); // Test to measure performance overhead for no-op span creation void BM_NoopSpanCreation(benchmark::State &state) { - BenchmarkSpanCreation(std::move(std::make_shared()), state); + BenchmarkSpanCreation(std::move(std::make_shared()), state); } BENCHMARK(BM_NoopSpanCreation); -} // namespace +} // namespace BENCHMARK_MAIN(); From b92ec791316b60487f01867eb64f9bf030b98065 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Tue, 21 Jul 2020 10:37:56 -0400 Subject: [PATCH 10/10] Format additional files --- sdk/test/trace/BUILD | 2 +- sdk/test/trace/CMakeLists.txt | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/sdk/test/trace/BUILD b/sdk/test/trace/BUILD index 62aa234d66..b9253befcb 100644 --- a/sdk/test/trace/BUILD +++ b/sdk/test/trace/BUILD @@ -83,8 +83,8 @@ cc_test( "probability_sampler_test.cc", ], deps = [ - "//sdk/src/trace", "//sdk/src/common:random", + "//sdk/src/trace", "@com_google_googletest//:gtest_main", ], ) diff --git a/sdk/test/trace/CMakeLists.txt b/sdk/test/trace/CMakeLists.txt index 7060170c67..6f7ce66fca 100644 --- a/sdk/test/trace/CMakeLists.txt +++ b/sdk/test/trace/CMakeLists.txt @@ -1,9 +1,17 @@ -foreach(testname tracer_provider_test span_data_test simple_processor_test - tracer_test always_off_sampler_test always_on_sampler_test - parent_or_else_sampler_test probability_sampler_test) +foreach( + testname + tracer_provider_test + span_data_test + simple_processor_test + tracer_test + always_off_sampler_test + always_on_sampler_test + parent_or_else_sampler_test + probability_sampler_test) add_executable(${testname} "${testname}.cc") - target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common opentelemetry_trace) + target_link_libraries( + ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + opentelemetry_common opentelemetry_trace) gtest_add_tests(TARGET ${testname} TEST_PREFIX trace. TEST_LIST ${testname}) endforeach()