From b93493d2a61ab6aa90fc0bc33b6b9d3664e9502a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:18:34 +0100 Subject: [PATCH] Benchmark stats WIP --- nano/benchmarks/CMakeLists.txt | 2 +- nano/benchmarks/entry.cpp | 20 ++++++++++ nano/benchmarks/stats.cpp | 68 ++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 nano/benchmarks/stats.cpp diff --git a/nano/benchmarks/CMakeLists.txt b/nano/benchmarks/CMakeLists.txt index 97aa9665bb..dc3829303d 100644 --- a/nano/benchmarks/CMakeLists.txt +++ b/nano/benchmarks/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(benchmarks entry.cpp) +add_executable(benchmarks entry.cpp stats.cpp) target_link_libraries(benchmarks test_common benchmark::benchmark) diff --git a/nano/benchmarks/entry.cpp b/nano/benchmarks/entry.cpp index e69de29bb2..ed325334e8 100644 --- a/nano/benchmarks/entry.cpp +++ b/nano/benchmarks/entry.cpp @@ -0,0 +1,20 @@ +#include + +static void BM_StringCreation (benchmark::State & state) +{ + for (auto _ : state) + std::string empty_string; +} +// Register the function as a benchmark +BENCHMARK (BM_StringCreation); + +// Define another benchmark +static void BM_StringCopy (benchmark::State & state) +{ + std::string x = "hello"; + for (auto _ : state) + std::string copy (x); +} +BENCHMARK (BM_StringCopy); + +BENCHMARK_MAIN (); \ No newline at end of file diff --git a/nano/benchmarks/stats.cpp b/nano/benchmarks/stats.cpp new file mode 100644 index 0000000000..62e49ac526 --- /dev/null +++ b/nano/benchmarks/stats.cpp @@ -0,0 +1,68 @@ +#include + +#include + +#include + +static void benchmark_stats_inc_single (benchmark::State & state) +{ + nano::stats stats; + + for (auto _ : state) + { + stats.inc (nano::stat::type::ledger, nano::stat::detail::open); + } +} + +BENCHMARK (benchmark_stats_inc_single); +BENCHMARK (benchmark_stats_inc_single)->Threads (16); + +static void benchmark_stats_inc_multiple (benchmark::State & state) +{ + nano::stats stats; + + for (auto _ : state) + { + stats.inc (nano::stat::type::ledger, nano::stat::detail::open); + stats.inc (nano::stat::type::active, nano::stat::detail::activate); + stats.inc (nano::stat::type::active, nano::stat::detail::block); + stats.inc (nano::stat::type::aggregator, nano::stat::detail::block); + stats.inc (nano::stat::type::aggregator, nano::stat::detail::cleanup); + stats.inc (nano::stat::type::aggregator, nano::stat::detail::already_confirmed); + stats.inc (nano::stat::type::backlog, nano::stat::detail::insert); + } +} + +BENCHMARK (benchmark_stats_inc_multiple); +BENCHMARK (benchmark_stats_inc_multiple)->Threads (16); + +static void benchmark_stats_inc_random (benchmark::State & state) +{ + nano::stats stats; + + auto random_subset = [] (auto elements, size_t count) -> std::vector { + std::shuffle (elements.begin (), elements.end (), std::mt19937 (std::random_device () ())); + return { elements.begin (), elements.begin () + std::min (count, elements.size ()) }; + }; + + auto stat_types = random_subset (nano::stat::all_types (), state.range (0)); + auto stat_details = random_subset (nano::stat::all_details (), state.range (1)); + + size_t type_index = 0; + size_t detail_index = 0; + + for (auto _ : state) + { + stats.inc (stat_types[type_index], stat_details[detail_index]); + + type_index = (type_index + 1) % stat_types.size (); + detail_index = (detail_index + 1) % stat_details.size (); + } +} + +BENCHMARK (benchmark_stats_inc_random)->Args ({ 8, 8 }); +BENCHMARK (benchmark_stats_inc_random)->Args ({ 16, 16 }); +BENCHMARK (benchmark_stats_inc_random)->Args ({ 64, 64 }); +BENCHMARK (benchmark_stats_inc_random)->Args ({ 8, 8 })->Threads (16); +BENCHMARK (benchmark_stats_inc_random)->Args ({ 16, 16 })->Threads (16); +BENCHMARK (benchmark_stats_inc_random)->Args ({ 64, 64 })->Threads (16); \ No newline at end of file