spdlog/bench/latency.cpp

164 lines
7.1 KiB
C++
Raw Normal View History

2018-07-10 02:06:15 +08:00
//
// Copyright(c) 2018 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
//
// latency.cpp : spdlog latency benchmarks
//
2018-10-12 08:04:55 +08:00
2018-10-25 03:31:50 +08:00
#include "benchmark/benchmark.h"
2024-12-08 01:18:04 +08:00
#include "spdlog/sinks/async_sink.h"
2018-07-10 02:06:15 +08:00
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
2023-09-29 05:20:26 +08:00
#include "spdlog/spdlog.h"
2018-10-26 21:34:14 +08:00
2024-12-08 01:18:04 +08:00
using namespace spdlog::sinks;
void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
const char *msg =
2024-05-03 23:30:46 +08:00
"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) {
2018-10-25 03:31:50 +08:00
logger->info(msg);
2018-07-10 02:06:15 +08:00
}
}
void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
2018-10-25 03:31:50 +08:00
int i = 0;
2024-05-03 23:30:46 +08:00
for (auto _ : state) {
2018-10-26 21:34:14 +08:00
logger->info("Hello logger: msg number {}...............", ++i);
2018-07-10 02:06:15 +08:00
}
}
2024-03-17 01:28:08 +08:00
void bench_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
2024-12-07 01:21:42 +08:00
spdlog::set_global_logger(std::move(logger));
2023-05-21 16:11:29 +08:00
int i = 0;
2024-05-03 23:30:46 +08:00
for (auto _ : state) {
2023-05-21 16:11:29 +08:00
spdlog::info("Hello logger: msg number {}...............", ++i);
}
}
2018-07-10 02:06:15 +08:00
void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
2018-11-12 17:07:51 +08:00
int i = 0;
2023-09-25 21:40:05 +08:00
benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); // prevent unused warnings
2024-05-03 23:30:46 +08:00
for (auto _ : state) {
2018-11-12 17:07:51 +08:00
SPDLOG_LOGGER_DEBUG(logger, "Hello logger: msg number {}...............", i++);
2023-05-21 16:11:29 +08:00
}
}
2024-01-13 15:37:32 +08:00
void bench_disabled_macro_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
2024-12-07 01:21:42 +08:00
spdlog::set_global_logger(std::move(logger));
2023-05-21 16:11:29 +08:00
int i = 0;
2023-09-25 21:40:05 +08:00
benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); // prevent unused warnings
2024-05-03 23:30:46 +08:00
for (auto _ : state) {
2018-11-12 17:07:51 +08:00
SPDLOG_DEBUG("Hello logger: msg number {}...............", i++);
}
}
#ifdef __linux__
2024-03-17 01:28:08 +08:00
void bench_dev_null() {
2024-12-08 01:18:04 +08:00
auto dev_null_st = spdlog::create<basic_file_sink_st>("/dev/null_st", "/dev/null");
2024-01-13 15:37:32 +08:00
benchmark::RegisterBenchmark("/dev/null_st", bench_logger, std::move(dev_null_st))->UseRealTime();
2024-12-08 01:18:04 +08:00
auto dev_null_mt = spdlog::create<basic_file_sink_mt>("/dev/null_mt", "/dev/null");
2024-01-13 15:37:32 +08:00
benchmark::RegisterBenchmark("/dev/null_mt", bench_logger, std::move(dev_null_mt))->UseRealTime();
}
2024-03-17 01:28:08 +08:00
2023-09-25 21:40:05 +08:00
#endif // __linux__
2019-01-10 22:31:06 +08:00
2024-03-17 01:28:08 +08:00
// 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() {
const std::string some_logger_name = "Some logger name";
2024-03-17 06:44:10 +08:00
const int null_logger_count = 9;
2024-03-17 01:28:08 +08:00
for (int i = 0; i < null_logger_count; i++) {
2024-12-08 01:18:04 +08:00
spdlog::create<null_sink_mt>(some_logger_name + std::to_string(i));
2024-03-17 01:28:08 +08:00
}
return some_logger_name + std::to_string(null_logger_count / 2);
}
int main(int argc, char *argv[]) {
2018-10-30 06:08:49 +08:00
using spdlog::sinks::null_sink_mt;
using spdlog::sinks::null_sink_st;
2018-10-25 03:31:50 +08:00
2018-10-26 21:34:14 +08:00
size_t file_size = 30 * 1024 * 1024;
size_t rotating_files = 5;
2018-10-30 06:08:49 +08:00
int n_threads = benchmark::CPUInfo::Get().num_cpus;
2018-10-26 21:34:14 +08:00
auto full_bench = argc > 1 && std::string(argv[1]) == "full";
2018-11-12 17:07:51 +08:00
// disabled loggers
2024-01-13 15:37:32 +08:00
auto disabled_logger = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
disabled_logger->set_level(spdlog::level::off);
2018-11-12 17:07:51 +08:00
benchmark::RegisterBenchmark("disabled-at-compile-time", bench_disabled_macro, disabled_logger);
2024-01-14 00:20:08 +08:00
benchmark::RegisterBenchmark("disabled-at-compile-time (global logger)", bench_disabled_macro_global_logger, disabled_logger);
2024-01-13 15:37:32 +08:00
benchmark::RegisterBenchmark("disabled-at-runtime", bench_logger, disabled_logger);
benchmark::RegisterBenchmark("disabled-at-runtime (global logger)", bench_global_logger, disabled_logger);
2018-10-30 06:08:49 +08:00
2024-01-13 15:37:32 +08:00
auto null_logger_st = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
benchmark::RegisterBenchmark("null_sink_st (500_bytes c_str)", bench_c_string, std::move(null_logger_st));
2018-10-30 06:08:49 +08:00
benchmark::RegisterBenchmark("null_sink_st", bench_logger, null_logger_st);
2024-01-13 15:37:32 +08:00
benchmark::RegisterBenchmark("null_sink_st (global logger)", bench_global_logger, null_logger_st);
2018-10-26 21:34:14 +08:00
2024-03-16 23:33:03 +08:00
#ifdef __linux__
bench_dev_null();
2023-09-25 21:40:05 +08:00
#endif // __linux__
if (full_bench) {
// basic_st
2024-12-08 01:18:04 +08:00
auto basic_st = spdlog::create<basic_file_sink_st>("basic_st", "latency_logs/basic_st.log", true);
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
// rotating st
2024-12-08 01:18:04 +08:00
auto rotating_st = spdlog::create<rotating_file_sink_st>("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
2024-01-13 15:37:32 +08:00
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
// daily st
2024-12-08 01:18:04 +08:00
auto daily_st = spdlog::create<daily_file_sink_st>("daily_st", "latency_logs/daily_st.log", 0, 1);
benchmark::RegisterBenchmark("daily_st", bench_logger, std::move(daily_st))->UseRealTime();
//
// Multi threaded bench, 10 loggers using same logger concurrently
//
2024-01-13 15:37:32 +08:00
auto null_logger_mt = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
benchmark::RegisterBenchmark("null_sink_mt", bench_logger, null_logger_mt)->Threads(n_threads)->UseRealTime();
// basic_mt
2024-12-08 01:18:04 +08:00
auto basic_mt = spdlog::create<basic_file_sink_mt>("basic_mt", "latency_logs/basic_mt.log", true);
2024-01-13 15:37:32 +08:00
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();
// rotating mt
2024-12-08 01:18:04 +08:00
auto rotating_mt = spdlog::create<rotating_file_sink_mt>("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files);
2024-01-14 00:20:08 +08:00
benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime();
// daily mt
2024-12-08 01:18:04 +08:00
auto daily_mt = spdlog::create<daily_file_sink_mt>("daily_mt", "latency_logs/daily_mt.log", 0, 1);
2024-01-13 15:37:32 +08:00
benchmark::RegisterBenchmark("daily_mt", bench_logger, std::move(daily_mt))->Threads(n_threads)->UseRealTime();
}
2018-10-25 03:31:50 +08:00
2018-10-30 06:08:49 +08:00
// async
auto queue_size = 1024 * 1024 * 3;
2024-12-08 01:18:04 +08:00
auto async_sink = std::make_shared<async_sink_mt>(queue_size);
async_sink->add_sink(std::make_shared<null_sink_st>());
async_sink->set_overflow_policy(async_sink_mt::overflow_policy::overrun_oldest);
auto async_logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
2024-01-13 15:37:32 +08:00
benchmark::RegisterBenchmark("async_logger", bench_logger, async_logger)->Threads(n_threads)->UseRealTime();
2024-03-17 01:28:08 +08:00
2018-10-25 03:31:50 +08:00
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
2018-07-10 02:06:15 +08:00
}