feat add benchmark for system_time_bench
All checks were successful
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 33s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 37s

This commit is contained in:
tqcq 2024-03-11 11:23:53 +08:00
parent d85287d60a
commit e1cb39690b
3 changed files with 43 additions and 3 deletions

View File

@ -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)

View File

@ -1,12 +1,42 @@
#include <benchmark/benchmark.h>
#include <sled/random.h>
#include <sled/strings/base64.h>
#include <sstream>
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);

10
src/system_time_bench.cc Normal file
View File

@ -0,0 +1,10 @@
#include <benchmark/benchmark.h>
#include <sled/system_time.h>
static void
SystemTimeNanos(benchmark::State &state)
{
for (auto _ : state) { (void) sled::SystemTimeNanos(); }
}
BENCHMARK(SystemTimeNanos);