Skip to content

Commit

Permalink
Add SpanId benchmark. (open-telemetry#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-easy authored Jan 29, 2020
1 parent 2787e95 commit 1987e76
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 9 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
11 changes: 11 additions & 0 deletions api/test/trace/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 4 additions & 2 deletions api/test/trace/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
52 changes: 52 additions & 0 deletions api/test/trace/span_id_benchmark.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "opentelemetry/trace/span_id.h"

#include <benchmark/benchmark.h>
#include <cstdint>

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();
1 change: 1 addition & 0 deletions ci/setup_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ci/setup_windows_ci_environment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1987e76

Please sign in to comment.