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..05725cb60f --- /dev/null +++ b/nano/benchmarks/stats.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +#include + +#include + +static void BM_stats_inc_single (benchmark::State & state) +{ + nano::logger::initialize_dummy (); + nano::stats stats{ nano::default_logger () }; + + for (auto _ : state) + { + stats.inc (nano::stat::type::ledger, nano::stat::detail::open); + } +} + +BENCHMARK (BM_stats_inc_single); +BENCHMARK (BM_stats_inc_single)->Threads (10); + +static void BM_stats_inc_random (benchmark::State & state) +{ + nano::logger::initialize_dummy (); + nano::stats stats{ nano::default_logger () }; + + 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::enum_util::values (), state.range (0)); + auto stat_details = random_subset (nano::enum_util::values (), 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 (BM_stats_inc_random)->Args ({ 32, 32 }); +BENCHMARK (BM_stats_inc_random)->Args ({ 32, 32 })->Threads (10); \ No newline at end of file diff --git a/nano/lib/logging.cpp b/nano/lib/logging.cpp index 0953007fa0..c6d1a67738 100644 --- a/nano/lib/logging.cpp +++ b/nano/lib/logging.cpp @@ -117,6 +117,18 @@ void nano::logger::initialize_for_tests (nano::log_config fallback) global_initialized = true; } +void nano::logger::initialize_dummy () +{ + nano::log_config config{ + .default_level = nano::log::level::off, + .flush_level = nano::log::level::off, + .console.enable = false, + .file.enable = false, + }; + initialize_common (config, std::nullopt); + global_initialized = true; +} + // Using std::cerr here, since logging may not be initialized yet void nano::logger::initialize_common (nano::log_config const & config, std::optional data_path) { @@ -307,7 +319,7 @@ nano::log_config nano::log_config::cli_default () { log_config config{}; config.default_level = nano::log::level::critical; - config.console.colors = false; // to avoid printing warning about cerr and colors + config.console.colors = false; config.console.to_cerr = true; // Use cerr to avoid interference with CLI output that goes to stdout config.file.enable = false; return config; diff --git a/nano/lib/logging.hpp b/nano/lib/logging.hpp index ae9563300a..3d059ee9e9 100644 --- a/nano/lib/logging.hpp +++ b/nano/lib/logging.hpp @@ -157,6 +157,7 @@ class logger final public: static void initialize (nano::log_config fallback, std::optional data_path = std::nullopt, std::vector const & config_overrides = {}); static void initialize_for_tests (nano::log_config fallback); + static void initialize_dummy (); // TODO: This is less than ideal, provide `nano::dummy_logger ()` instead static void flush (); private: