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
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
void bench_formatter(benchmark::State &state, std::string pattern) {
|
2018-11-12 22:13:52 +08:00
|
|
|
auto formatter = spdlog::details::make_unique<spdlog::pattern_formatter>(pattern);
|
2019-08-28 23:50:11 +08:00
|
|
|
spdlog::memory_buf_t dest;
|
2018-11-12 22:13:52 +08:00
|
|
|
std::string logger_name = "logger-name";
|
2023-09-25 21:08:29 +08:00
|
|
|
const char *text =
|
|
|
|
"Hello. This is some message with length of 80 ";
|
2018-11-12 22:13:52 +08:00
|
|
|
|
2019-06-14 05:23:25 +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
|
|
|
|
2023-09-25 21:08:29 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
void bench_formatters() {
|
2018-11-12 22:13:52 +08:00
|
|
|
// 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;
|
2023-09-25 21:08:29 +08:00
|
|
|
for (auto &flag : all_flags) {
|
2018-11-12 22:13:52 +08:00
|
|
|
auto pattern = std::string("%") + flag;
|
2021-02-24 16:45:35 +08:00
|
|
|
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;
|
2021-02-24 16:45:35 +08:00
|
|
|
// benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
|
2019-01-10 22:31:06 +08:00
|
|
|
//
|
|
|
|
// // bench center padding
|
|
|
|
// pattern = std::string("%=16") + flag;
|
2021-02-24 16:45:35 +08:00
|
|
|
// 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
|
|
|
};
|
2023-09-25 21:08:29 +08:00
|
|
|
for (auto &pattern : patterns) {
|
|
|
|
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern)
|
|
|
|
->Iterations(2500000);
|
2018-11-12 22:13:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2018-11-25 06:44:27 +08:00
|
|
|
spdlog::set_pattern("[%^%l%$] %v");
|
2023-09-25 21:08:29 +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];
|
2023-09-25 21:08:29 +08:00
|
|
|
if (pattern == "all") {
|
2018-11-16 20:13:29 +08:00
|
|
|
bench_formatters();
|
2023-09-25 21:08:29 +08:00
|
|
|
} else {
|
2021-02-24 16:45:35 +08:00
|
|
|
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();
|
|
|
|
}
|