diff --git a/bench/latency.cpp b/bench/latency.cpp index 1471d0373..4a31a6f18 100644 --- a/bench/latency.cpp +++ b/bench/latency.cpp @@ -17,31 +17,32 @@ void bench_c_string(benchmark::State &state, std::shared_ptr logger) { const char *msg = - "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus " - "lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, " - "eu consequat sem " - "libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec " - "fringilla dui sed " - "augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, " - "nisi turpis ornare " - "nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue " - "nibh turpis duis."; - - for (auto _ : state) { + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus " + "lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, " + "eu consequat sem " + "libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec " + "fringilla dui sed " + "augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, " + "nisi turpis ornare " + "nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue " + "nibh turpis duis."; + + for (auto _: state) { logger->info(msg); } } void bench_logger(benchmark::State &state, std::shared_ptr logger) { int i = 0; - for (auto _ : state) { + for (auto _: state) { logger->info("Hello logger: msg number {}...............", ++i); } } + void bench_global_logger(benchmark::State &state, std::shared_ptr logger) { spdlog::set_default_logger(std::move(logger)); int i = 0; - for (auto _ : state) { + for (auto _: state) { spdlog::info("Hello logger: msg number {}...............", ++i); } } @@ -50,7 +51,7 @@ void bench_disabled_macro(benchmark::State &state, std::shared_ptrUseRealTime(); @@ -75,8 +77,58 @@ void bench_dev_null() { benchmark::RegisterBenchmark("/dev/null_mt", bench_logger, std::move(dev_null_mt))->UseRealTime(); spdlog::drop("/dev/null_mt"); } + #endif // __linux__ + +// test spdlog::get() performance +// for this test we create multiple null loggers and then call spdlog::get() on one of them multiple times +// create multiple null loggers and return name of the one to test +static std::string prepare_null_loggers() { + spdlog::drop_all(); + const std::string some_logger_name = "Some logger name"; + const int null_logger_count = 10; + for (int i = 0; i < null_logger_count; i++) { + spdlog::create(some_logger_name + std::to_string(i)); + } + return some_logger_name + std::to_string(null_logger_count / 2); +} + +// benchmark spdlog::get() with const char* +void bench_get_logger_const_char(benchmark::State &state) { + std::string str_name = prepare_null_loggers(); + const char* name = str_name.c_str(); + for (auto _: state) { + auto rv = spdlog::get(name); + if (rv == nullptr) { + state.SkipWithError("get() returned nullptr"); + } + } +} + +// benchmark spdlog::get() with std::string_view +void bench_get_logger_sv(benchmark::State &state) { + auto str_name = prepare_null_loggers(); + auto sv_name = std::string_view{str_name}; + for (auto _: state) { + auto rv = spdlog::get(sv_name); + if (rv == nullptr) { + state.SkipWithError("get() returned nullptr"); + } + } +} + +// benchmark spdlog::get() with std::string +void bench_get_logger_string(benchmark::State &state) { + auto str_name = prepare_null_loggers(); + for (auto _: state) { + auto rv = spdlog::get(str_name); + if (rv == nullptr) { + state.SkipWithError("get() returned nullptr"); + } + } +} + int main(int argc, char *argv[]) { using spdlog::sinks::null_sink_mt; using spdlog::sinks::null_sink_st; @@ -148,6 +200,12 @@ int main(int argc, char *argv[]) { auto async_logger = std::make_shared("async_logger", std::make_shared(), std::move(tp), spdlog::async_overflow_policy::overrun_oldest); benchmark::RegisterBenchmark("async_logger", bench_logger, async_logger)->Threads(n_threads)->UseRealTime(); + + + benchmark::RegisterBenchmark("spdlog::get(const char* name)", bench_get_logger_const_char); + benchmark::RegisterBenchmark("spdlog::get(std::string_view name)", bench_get_logger_sv); + benchmark::RegisterBenchmark("spdlog::get(const std::string &name)", bench_get_logger_string); + benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); }