feat add benchmark for system_time_bench
This commit is contained in:
parent
d85287d60a
commit
e1cb39690b
@ -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)
|
||||
|
@ -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
10
src/system_time_bench.cc
Normal 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);
|
Loading…
Reference in New Issue
Block a user