Skip to content

Commit

Permalink
added spdlog::get(..) benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
gabime committed Mar 16, 2024
1 parent 50d7f59 commit 232aace
Showing 1 changed file with 73 additions and 15 deletions.
88 changes: 73 additions & 15 deletions bench/latency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,32 @@

void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> 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<spdlog::logger> 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<spdlog::logger> logger) {
spdlog::set_default_logger(std::move(logger));
int i = 0;
for (auto _ : state) {
for (auto _: state) {
spdlog::info("Hello logger: msg number {}...............", ++i);
}
}
Expand All @@ -50,7 +51,7 @@ void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logge
int i = 0;
benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); // prevent unused warnings
for (auto _ : state) {
for (auto _: state) {
SPDLOG_LOGGER_DEBUG(logger, "Hello logger: msg number {}...............", i++);
}
}
Expand All @@ -60,12 +61,13 @@ void bench_disabled_macro_global_logger(benchmark::State &state, std::shared_ptr
int i = 0;
benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); // prevent unused warnings
for (auto _ : state) {
for (auto _: state) {
SPDLOG_DEBUG("Hello logger: msg number {}...............", i++);
}
}

#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();
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

0 comments on commit 232aace

Please sign in to comment.