Skip to content

Commit

Permalink
Benchmark stats WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 25, 2024
1 parent 641bfa5 commit b93493d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nano/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_executable(benchmarks entry.cpp)
add_executable(benchmarks entry.cpp stats.cpp)

target_link_libraries(benchmarks test_common benchmark::benchmark)

Expand Down
20 changes: 20 additions & 0 deletions nano/benchmarks/entry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <benchmark/benchmark.h>

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 ();
68 changes: 68 additions & 0 deletions nano/benchmarks/stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <nano/lib/stats.hpp>

#include <random>

#include <benchmark/benchmark.h>

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<typename decltype (elements)::value_type> {
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);

0 comments on commit b93493d

Please sign in to comment.