spdlog/bench/formatter-bench.cpp

81 lines
2.4 KiB
C++
Raw Normal View History

2018-11-10 17:52:21 -05:00
//
// Copyright(c) 2018 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#include "benchmark/benchmark.h"
#include "spdlog/spdlog.h"
#include "spdlog/details/pattern_formatter.h"
2018-11-12 09:13:52 -05:00
void bench_formatter(benchmark::State &state, std::string pattern)
{
auto formatter = spdlog::details::make_unique<spdlog::pattern_formatter>(pattern);
fmt::memory_buffer dest;
std::string logger_name = "logger-name";
2018-11-21 09:02:02 -05:00
const char *text = "Hello. This is some message with length of 80 ";
2018-11-12 09:13:52 -05: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 09:13:52 -05:00
for (auto _ : state)
{
2018-11-10 17:52:21 -05:00
dest.clear();
2018-11-12 09:13:52 -05:00
formatter->format(msg, dest);
benchmark::DoNotOptimize(dest);
2018-11-10 17:52:21 -05:00
}
}
2018-11-12 09:13:52 -05:00
void bench_formatters()
{
// basic patterns(single flag)
std::string all_flags = "+vtPnlLaAbBcCYDmdHIMSefFprRTXzEisg@l%";
2018-11-12 09:13:52 -05:00
std::vector<std::string> basic_patterns;
2018-11-21 09:02:02 -05:00
for (auto &flag : all_flags)
2018-11-12 09:13:52 -05:00
{
auto pattern = std::string("%") + flag;
2018-11-16 07:13:29 -05:00
benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
2019-01-10 09:31:06 -05:00
// pattern = std::string("%16") + flag;
// benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
//
// // bench center padding
// pattern = std::string("%=16") + flag;
// benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
2018-11-12 09:13:52 -05:00
}
// complex patterns
std::vector<std::string> patterns = {
2018-11-21 09:02:02 -05: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 09:13:52 -05:00
};
2018-11-21 09:02:02 -05:00
for (auto &pattern : patterns)
2018-11-12 09:13:52 -05:00
{
2018-11-12 09:44:34 -05:00
benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern)->Iterations(2500000);
2018-11-12 09:13:52 -05:00
}
}
2018-11-16 07:13:29 -05:00
int main(int argc, char *argv[])
2018-11-10 18:26:57 -05:00
{
2018-11-10 17:52:21 -05:00
2018-11-24 17:44:27 -05:00
spdlog::set_pattern("[%^%l%$] %v");
2019-01-10 09:31:06 -05:00
if (argc != 2)
2018-11-10 17:52:21 -05:00
{
2018-11-24 17:46:24 -05:00
spdlog::error("Usage: {} <pattern> (or \"all\" to bench all)", argv[0]);
2018-11-24 17:44:27 -05:00
exit(1);
2018-11-16 07:13:29 -05:00
}
2018-11-24 17:44:27 -05:00
std::string pattern = argv[1];
2019-01-10 09:31:06 -05:00
if (pattern == "all")
2018-11-16 07:13:29 -05:00
{
bench_formatters();
2018-11-10 17:52:21 -05:00
}
2018-11-24 17:44:27 -05:00
else
{
benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
}
2018-11-10 17:52:21 -05:00
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
}