From f5b3deff209ced50d1e19638131e873a27a6f828 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Wed, 16 Jun 2021 12:48:08 +0530 Subject: [PATCH] baggage benchmark --- api/test/baggage/BUILD | 6 ++ api/test/baggage/CMakeLists.txt | 3 + api/test/baggage/baggage_benchmark.cc | 117 ++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 api/test/baggage/baggage_benchmark.cc diff --git a/api/test/baggage/BUILD b/api/test/baggage/BUILD index 205bed68fe..e5fc1ca5be 100644 --- a/api/test/baggage/BUILD +++ b/api/test/baggage/BUILD @@ -10,3 +10,9 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +otel_cc_benchmark( + name = "baggage_benchmark", + srcs = ["baggage_benchmark.cc"], + deps = ["//api"], +) diff --git a/api/test/baggage/CMakeLists.txt b/api/test/baggage/CMakeLists.txt index a2489cf930..0d6cba5a0d 100644 --- a/api/test/baggage/CMakeLists.txt +++ b/api/test/baggage/CMakeLists.txt @@ -9,4 +9,7 @@ foreach(testname baggage_test) TEST_PREFIX baggage. TEST_LIST ${testname}) endforeach() +add_executable(baggage_benchmark baggage_benchmark.cc) +target_link_libraries(baggage_benchmark benchmark::benchmark + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) add_subdirectory(propagation) diff --git a/api/test/baggage/baggage_benchmark.cc b/api/test/baggage/baggage_benchmark.cc new file mode 100644 index 0000000000..9b75297957 --- /dev/null +++ b/api/test/baggage/baggage_benchmark.cc @@ -0,0 +1,117 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/nostd/string_view.h" + +#include +#include + +using namespace opentelemetry::baggage; +namespace nostd = opentelemetry::nostd; + +namespace +{ + +const size_t kNumEntries = 10; + +std::string header_with_custom_entries(size_t num_entries) +{ + std::string header; + for (size_t i = 0; i < num_entries; i++) + { + std::string key = "ADecentlyLargekey" + std::to_string(i); + std::string value = "ADecentlyLargeValue" + std::to_string(i); + header += key + "=" + value; + if (i != num_entries - 1) + { + header += ","; + } + } + return header; +} + +void BM_CreateBaggageFromTenEntries(benchmark::State &state) +{ + std::string header = header_with_custom_entries(kNumEntries); + while (state.KeepRunning()) + { + auto baggage = Baggage::FromHeader(header); + } +} +BENCHMARK(BM_CreateBaggageFromTenEntries); + +void BM_ExtractBaggageHavingTenEntries(benchmark::State &state) +{ + auto baggage = Baggage::FromHeader(header_with_custom_entries(kNumEntries)); + while (state.KeepRunning()) + { + baggage->GetAllEntries([](nostd::string_view key, nostd::string_view value) { return true; }); + } +} +BENCHMARK(BM_ExtractBaggageHavingTenEntries); + +void BM_CreateBaggageFrom180Entries(benchmark::State &state) +{ + std::string header = header_with_custom_entries(Baggage::kMaxKeyValuePairs); + while (state.KeepRunning()) + { + auto baggage = Baggage::FromHeader(header); + } +} +BENCHMARK(BM_CreateBaggageFrom180Entries); + +void BM_ExtractBaggageWith180Entries(benchmark::State &state) +{ + auto baggage = Baggage::FromHeader(header_with_custom_entries(Baggage::kMaxKeyValuePairs)); + while (state.KeepRunning()) + { + baggage->GetAllEntries([](nostd::string_view key, nostd::string_view value) { return true; }); + } +} +BENCHMARK(BM_ExtractBaggageWith180Entries); + +void BM_SetValueBaggageWithTenEntries(benchmark::State &state) +{ + auto baggage = Baggage::FromHeader( + header_with_custom_entries(kNumEntries - 1)); // 9 entries, and add one new + while (state.KeepRunning()) + { + auto new_baggage = baggage->Set("new_key", "new_value"); + } +} +BENCHMARK(BM_SetValueBaggageWithTenEntries); + +void BM_SetValueBaggageWith180Entries(benchmark::State &state) +{ + auto baggage = Baggage::FromHeader(header_with_custom_entries( + Baggage::kMaxKeyValuePairs - 1)); // keep 179 entries, and add one new + while (state.KeepRunning()) + { + auto new_baggage = baggage->Set("new_key", "new_value"); + } +} +BENCHMARK(BM_SetValueBaggageWith180Entries); + +void BM_BaggageToHeaderTenEntries(benchmark::State &state) +{ + auto baggage = Baggage::FromHeader(header_with_custom_entries(kNumEntries)); + while (state.KeepRunning()) + { + auto new_baggage = baggage->ToHeader(); + } +} +BENCHMARK(BM_BaggageToHeaderTenEntries); + +void BM_BaggageToHeader180Entries(benchmark::State &state) +{ + auto baggage = Baggage::FromHeader(header_with_custom_entries(Baggage::kMaxKeyValuePairs)); + while (state.KeepRunning()) + { + auto new_baggage = baggage->ToHeader(); + } +} +BENCHMARK(BM_BaggageToHeader180Entries); +} // namespace + +BENCHMARK_MAIN();