From 1987e762b1f727227e4ae47cb69478103e1ff36d Mon Sep 17 00:00:00 2001 From: easy Date: Wed, 29 Jan 2020 16:33:05 +1100 Subject: [PATCH] Add SpanId benchmark. (#33) --- CMakeLists.txt | 1 + WORKSPACE | 9 +++++ api/test/trace/BUILD | 11 ++++++ api/test/trace/CMakeLists.txt | 6 ++-- api/test/trace/span_id_benchmark.cc | 52 +++++++++++++++++++++++++++++ ci/setup_cmake.sh | 1 + ci/setup_windows_ci_environment.ps1 | 1 + 7 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 api/test/trace/span_id_benchmark.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f78df0be1e..65904f9e816 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ find_package(Threads) if(BUILD_TESTING) find_package(GTest REQUIRED) + find_package(benchmark REQUIRED) include_directories(SYSTEM ${GTEST_INCLUDE_DIRS}) endif() diff --git a/WORKSPACE b/WORKSPACE index 10b3c204577..13176f04b4c 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -24,3 +24,12 @@ http_archive( strip_prefix = "googletest-release-1.10.0", urls = ["https://github.com/google/googletest/archive/release-1.10.0.tar.gz"], ) + +# Google Benchmark library. +# Only needed for benchmarks, not to build the OpenTelemetry library. +http_archive( + name = "com_github_google_benchmark", + sha256 = "3c6a165b6ecc948967a1ead710d4a181d7b0fbcaa183ef7ea84604994966221a", + strip_prefix = "benchmark-1.5.0", + urls = ["https://github.com/google/benchmark/archive/v1.5.0.tar.gz"], +) diff --git a/api/test/trace/BUILD b/api/test/trace/BUILD index 5c6299dff35..bb835c33be3 100644 --- a/api/test/trace/BUILD +++ b/api/test/trace/BUILD @@ -9,6 +9,17 @@ cc_test( ], ) +cc_binary( + name = "span_id_benchmark", + srcs = [ + "span_id_benchmark.cc", + ], + deps = [ + "//api", + "@com_github_google_benchmark//:benchmark", + ], +) + cc_test( name = "span_id_test", srcs = [ diff --git a/api/test/trace/CMakeLists.txt b/api/test/trace/CMakeLists.txt index cee4cf9e1e3..1f83795886c 100644 --- a/api/test/trace/CMakeLists.txt +++ b/api/test/trace/CMakeLists.txt @@ -1,10 +1,12 @@ -include(GoogleTest) - add_executable(noop_test noop_test.cc) target_link_libraries(noop_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) gtest_add_tests(TARGET noop_test TEST_PREFIX trace. TEST_LIST noop_test) +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_id_test span_id_test.cc) target_link_libraries(span_id_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) diff --git a/api/test/trace/span_id_benchmark.cc b/api/test/trace/span_id_benchmark.cc new file mode 100644 index 00000000000..65f0c775e9c --- /dev/null +++ b/api/test/trace/span_id_benchmark.cc @@ -0,0 +1,52 @@ +#include "opentelemetry/trace/span_id.h" + +#include +#include + +namespace +{ +using opentelemetry::trace::SpanId; +constexpr uint8_t bytes[] = {1, 2, 3, 4, 5, 6, 7, 8}; + +void BM_SpanIdDefaultConstructor(benchmark::State &state) +{ + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(SpanId()); + } +} +BENCHMARK(BM_SpanIdDefaultConstructor); + +void BM_SpanIdConstructor(benchmark::State &state) +{ + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(SpanId(bytes)); + } +} +BENCHMARK(BM_SpanIdConstructor); + +void BM_SpanIdToLowerBase16(benchmark::State &state) +{ + SpanId id(bytes); + char buf[SpanId::kSize * 2]; + while (state.KeepRunning()) + { + id.ToLowerBase16(buf); + benchmark::DoNotOptimize(buf); + } +} +BENCHMARK(BM_SpanIdToLowerBase16); + +void BM_SpanIdIsValid(benchmark::State &state) +{ + SpanId id(bytes); + while (state.KeepRunning()) + { + benchmark::DoNotOptimize(id.IsValid()); + } +} +BENCHMARK(BM_SpanIdIsValid); + +} // namespace +BENCHMARK_MAIN(); diff --git a/ci/setup_cmake.sh b/ci/setup_cmake.sh index ec79b0492d8..1e22f39f818 100755 --- a/ci/setup_cmake.sh +++ b/ci/setup_cmake.sh @@ -4,6 +4,7 @@ set -e apt-get install --no-install-recommends --no-install-suggests -y \ cmake \ + libbenchmark-dev \ libgtest-dev # Follows these instructions for setting up gtest diff --git a/ci/setup_windows_ci_environment.ps1 b/ci/setup_windows_ci_environment.ps1 index e9c2c51b09a..a1b1b89d9c5 100755 --- a/ci/setup_windows_ci_environment.ps1 +++ b/ci/setup_windows_ci_environment.ps1 @@ -6,4 +6,5 @@ cd vcpkg $VCPKG_DIR=(Get-Item -Path ".\").FullName ./bootstrap-vcpkg.bat ./vcpkg integrate install +./vcpkg install benchmark:x64-windows ./vcpkg install gtest:x64-windows