spdlog/bench/formatter-bench.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.3 KiB
C++
Raw Permalink Normal View History

2018-11-11 06:52:21 +08:00
//
// Copyright(c) 2018 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#include "benchmark/benchmark.h"
#include "spdlog/spdlog.h"
2020-03-22 05:42:22 +08:00
#include "spdlog/pattern_formatter.h"
2018-11-11 06:52:21 +08:00
2018-11-12 22:13:52 +08:00
void bench_formatter(benchmark::State &state, std::string pattern) {
auto formatter = spdlog::details::make_unique<spdlog::pattern_formatter>(pattern);
spdlog::memory_buf_t dest;
2018-11-12 22:13:52 +08:00
std::string logger_name = "logger-name";
2018-11-21 22:02:02 +08:00
const char *text =
"Hello. This is some message with length of 80 ";
2018-11-12 22:13:52 +08:00
spdlog::source_loc source_loc{"a/b/c/d/myfile.cpp", 123, "some_func()"};
spdlog::details::log_msg msg(source_loc, logger_name, spdlog::level::info, text);
2018-11-12 22:13:52 +08:00
for (auto _ : state) {
2018-11-11 06:52:21 +08:00
dest.clear();
2018-11-12 22:13:52 +08:00
formatter->format(msg, dest);
benchmark::DoNotOptimize(dest);
2018-11-11 06:52:21 +08:00
}
}
2018-11-12 22:13:52 +08:00
void bench_formatters() {
// basic patterns(single flag)
2019-07-10 06:41:55 +08:00
std::string all_flags = "+vtPnlLaAbBcCYDmdHIMSefFprRTXzEisg@luioO%";
2018-11-12 22:13:52 +08:00
std::vector<std::string> basic_patterns;
2018-11-21 22:02:02 +08:00
for (auto &flag : all_flags) {
2018-11-12 22:13:52 +08:00
auto pattern = std::string("%") + flag;
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
2018-11-16 20:13:29 +08:00
2019-01-10 22:31:06 +08:00
// pattern = std::string("%16") + flag;
// benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
2019-01-10 22:31:06 +08:00
//
// // bench center padding
// pattern = std::string("%=16") + flag;
// benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
2018-11-12 22:13:52 +08:00
}
// complex patterns
std::vector<std::string> patterns = {
2018-11-21 22:02:02 +08:00
"[%D %X] [%l] [%n] %v",
"[%Y-%m-%d %H:%M:%S.%e] [%l] [%n] %v",
"[%Y-%m-%d %H:%M:%S.%e] [%l] [%n] [%t] %v",
2018-11-12 22:13:52 +08:00
};
2018-11-21 22:02:02 +08:00
for (auto &pattern : patterns) {
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern)
->Iterations(2500000);
2018-11-12 22:13:52 +08:00
}
}
2018-11-16 20:13:29 +08:00
int main(int argc, char *argv[]) {
2018-11-25 06:44:27 +08:00
spdlog::set_pattern("[%^%l%$] %v");
2019-01-10 22:31:06 +08:00
if (argc != 2) {
2018-11-25 06:46:24 +08:00
spdlog::error("Usage: {} <pattern> (or \"all\" to bench all)", argv[0]);
2018-11-25 06:44:27 +08:00
exit(1);
2018-11-16 20:13:29 +08:00
}
2018-11-25 06:44:27 +08:00
std::string pattern = argv[1];
2019-01-10 22:31:06 +08:00
if (pattern == "all") {
2018-11-16 20:13:29 +08:00
bench_formatters();
2018-11-25 06:44:27 +08:00
} else {
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
2018-11-25 06:44:27 +08:00
}
2018-11-11 06:52:21 +08:00
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
}