diff --git a/CMakeLists.txt b/CMakeLists.txt index 2adefa4..4b328e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,7 +77,11 @@ target_link_libraries(sled PUBLIC rpc_core fmt marl) if(SLED_BUILD_BENCHMARK) find_package(benchmark REQUIRED) - add_executable(sled_benchmark "src/system/fiber/fiber_bench.cc") + add_executable(sled_benchmark + src/random_bench.cc + src/strings/base64_bench.cc + src/system/fiber/fiber_bench.cc + ) target_link_libraries(sled_benchmark PRIVATE sled benchmark::benchmark benchmark::benchmark_main) endif(SLED_BUILD_BENCHMARK) diff --git a/src/random_bench.cc b/src/random_bench.cc new file mode 100644 index 0000000..6defd91 --- /dev/null +++ b/src/random_bench.cc @@ -0,0 +1,56 @@ +#include +#include + +class RandomFixture : public benchmark::Fixture { + void SetUp(::benchmark::State &state) { rand_ = new sled::Random(1314); } + + void TearDown(::benchmark::State &state) { delete rand_; } + +protected: + sled::Random *rand_; +}; + +BENCHMARK_F(RandomFixture, bool)(benchmark::State &state) +{ + for (auto _ : state) { bool b = rand_->Rand(); } +} + +BENCHMARK_F(RandomFixture, int32_t)(benchmark::State &state) +{ + for (auto _ : state) { int32_t i = rand_->Rand(); } +} + +BENCHMARK_F(RandomFixture, int32_t_range)(benchmark::State &state) +{ + for (auto _ : state) { int32_t i = rand_->Rand(-1000, 1000); } +} + +BENCHMARK_F(RandomFixture, uint32_t)(benchmark::State &state) +{ + for (auto _ : state) { uint32_t i = rand_->Rand(); } +} + +BENCHMARK_F(RandomFixture, uint32_t_range)(benchmark::State &state) +{ + for (auto _ : state) { uint32_t i = rand_->Rand(0u, 1000u); } +} + +BENCHMARK_F(RandomFixture, Gaussian)(benchmark::State &state) +{ + for (auto _ : state) { double d = rand_->Gaussian(0, 1); } +} + +BENCHMARK_F(RandomFixture, Exponential)(benchmark::State &state) +{ + for (auto _ : state) { double d = rand_->Exponential(1); } +} + +BENCHMARK_F(RandomFixture, float)(benchmark::State &state) +{ + for (auto _ : state) { float f = rand_->Rand(); } +} + +BENCHMARK_F(RandomFixture, double)(benchmark::State &state) +{ + for (auto _ : state) { double d = rand_->Rand(); } +} diff --git a/src/strings/base64.cc b/src/strings/base64.cc index 5b81f20..6d7a541 100644 --- a/src/strings/base64.cc +++ b/src/strings/base64.cc @@ -1,5 +1,5 @@ -#include "sled/strings/base64.h" #include +#include #include namespace sled { diff --git a/src/strings/base64_bench.cc b/src/strings/base64_bench.cc new file mode 100644 index 0000000..37f57c6 --- /dev/null +++ b/src/strings/base64_bench.cc @@ -0,0 +1,12 @@ +#include +#include +#include + +static void +Base64Encode(benchmark::State &state) +{ + std::string input = "hello world\n"; + for (auto _ : state) { (void) sled::Base64::Encode(input); } +} + +BENCHMARK(Base64Encode)->RangeMultiplier(10)->Range(10, 1000000); diff --git a/src/system/fiber/fiber_bench.cc b/src/system/fiber/fiber_bench.cc index cc36772..cde9f0e 100644 --- a/src/system/fiber/fiber_bench.cc +++ b/src/system/fiber/fiber_bench.cc @@ -2,24 +2,58 @@ #include #include +static void +SingleQueue(benchmark::State &state) +{ + for (auto _ : state) { + state.PauseTiming(); + sled::Scheduler scheduler( + sled::Scheduler::Config().setWorkerThreadCount(0)); + scheduler.bind(); + defer(scheduler.unbind()); + const int num_tasks = state.range(0); + sled::WaitGroup wg(num_tasks); + sled::WaitGroup start_flag(1); + + for (int i = 0; i < num_tasks; i++) { + sled::Schedule([=] { + start_flag.Wait(); + wg.Done(); + }); + } + state.ResumeTiming(); + start_flag.Done(); + wg.Wait(); + state.PauseTiming(); + } +} + static void MultiQueue(benchmark::State &state) { - sled::Scheduler scheduler(sled::Scheduler::Config::allCores()); - scheduler.bind(); - defer(scheduler.unbind()); - const int num_tasks = state.range(0); - sled::WaitGroup wg(num_tasks); - sled::WaitGroup start_flag(1); - for (int i = 0; i < num_tasks; i++) { - sled::Schedule([=] { - start_flag.Wait(); - wg.Done(); - }); + for (auto _ : state) { + state.PauseTiming(); + sled::Scheduler scheduler(sled::Scheduler::Config::allCores()); + scheduler.bind(); + defer(scheduler.unbind()); + + const int num_tasks = state.range(0); + sled::WaitGroup wg(num_tasks); + sled::WaitGroup start_flag(1); + + for (int i = 0; i < num_tasks; i++) { + sled::Schedule([=] { + start_flag.Wait(); + wg.Done(); + }); + } + state.ResumeTiming(); + start_flag.Done(); + wg.Wait(); + state.PauseTiming(); } - start_flag.Done(); - wg.Wait(); } BENCHMARK(MultiQueue)->RangeMultiplier(10)->Range(10, 10000); +BENCHMARK(SingleQueue)->RangeMultiplier(10)->Range(10, 10000);