From e1cb39690b5bb7aaf2961f42cb79acf7c2d049f3 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:23:53 +0800 Subject: [PATCH] feat add benchmark for system_time_bench --- CMakeLists.txt | 2 +- src/strings/base64_bench.cc | 34 ++++++++++++++++++++++++++++++++-- src/system_time_bench.cc | 10 ++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/system_time_bench.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b328e7..592bc4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ if(SLED_BUILD_BENCHMARK) src/random_bench.cc src/strings/base64_bench.cc src/system/fiber/fiber_bench.cc - ) + src/system_time_bench.cc) target_link_libraries(sled_benchmark PRIVATE sled benchmark::benchmark benchmark::benchmark_main) endif(SLED_BUILD_BENCHMARK) diff --git a/src/strings/base64_bench.cc b/src/strings/base64_bench.cc index 37f57c6..3c36cdd 100644 --- a/src/strings/base64_bench.cc +++ b/src/strings/base64_bench.cc @@ -1,12 +1,42 @@ #include #include #include +#include + +static std::string +RandomString(size_t length) +{ + static const char chars[] = "0123456789" + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + std::stringstream ss; + sled::Random rand(1314); + while (length--) { ss << chars[rand.Rand(sizeof(chars))]; } + return ss.str(); +} static void Base64Encode(benchmark::State &state) { - std::string input = "hello world\n"; - for (auto _ : state) { (void) sled::Base64::Encode(input); } + for (auto _ : state) { + state.PauseTiming(); + std::string input = RandomString(state.range(0)); + state.ResumeTiming(); + (void) sled::Base64::Encode(input); + } +} + +static void +Base64Decode(benchmark::State &state) +{ + for (auto _ : state) { + state.PauseTiming(); + std::string input = RandomString(state.range(0)); + std::string base64_input = sled::Base64::Encode(input); + state.ResumeTiming(); + (void) sled::Base64::Decode(base64_input); + } } BENCHMARK(Base64Encode)->RangeMultiplier(10)->Range(10, 1000000); +BENCHMARK(Base64Decode)->RangeMultiplier(10)->Range(10, 1000000); diff --git a/src/system_time_bench.cc b/src/system_time_bench.cc new file mode 100644 index 0000000..9af5e7f --- /dev/null +++ b/src/system_time_bench.cc @@ -0,0 +1,10 @@ +#include +#include + +static void +SystemTimeNanos(benchmark::State &state) +{ + for (auto _ : state) { (void) sled::SystemTimeNanos(); } +} + +BENCHMARK(SystemTimeNanos);