1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-04-29 12:03:53 +00:00

added spdlog::get(..) benchmarks

This commit is contained in:
gabime 2024-03-16 19:28:08 +02:00
parent 50d7f59951
commit 232aace328

View File

@ -38,6 +38,7 @@ void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logge
logger->info("Hello logger: msg number {}...............", ++i);
}
}
void bench_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
spdlog::set_default_logger(std::move(logger));
int i = 0;
@ -66,6 +67,7 @@ void bench_disabled_macro_global_logger(benchmark::State &state, std::shared_ptr
}
#ifdef __linux__
void bench_dev_null() {
auto dev_null_st = spdlog::basic_logger_st("/dev/null_st", "/dev/null");
benchmark::RegisterBenchmark("/dev/null_st", bench_logger, std::move(dev_null_st))->UseRealTime();
@ -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<spdlog::sinks::null_sink_mt>(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<spdlog::async_logger>("async_logger", std::make_shared<null_sink_mt>(), 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();
}