formatter-bench.cpp 2.36 KB
Newer Older
gabime's avatar
gabime committed
1 2 3 4 5 6 7 8 9 10
//
// 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"

gabime's avatar
gabime committed
11 12 13
void bench_formatter(benchmark::State &state, std::string pattern)
{
    auto formatter = spdlog::details::make_unique<spdlog::pattern_formatter>(pattern);
14
    spdlog::memory_buf_t dest;
gabime's avatar
gabime committed
15
    std::string logger_name = "logger-name";
gabime's avatar
gabime committed
16
    const char *text = "Hello. This is some message with length of 80                                   ";
gabime's avatar
gabime committed
17

18 19
    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);
gabime's avatar
gabime committed
20 21 22

    for (auto _ : state)
    {
gabime's avatar
gabime committed
23
        dest.clear();
gabime's avatar
gabime committed
24 25
        formatter->format(msg, dest);
        benchmark::DoNotOptimize(dest);
gabime's avatar
gabime committed
26 27 28
    }
}

gabime's avatar
gabime committed
29 30 31
void bench_formatters()
{
    // basic patterns(single flag)
gabime's avatar
gabime committed
32
    std::string all_flags = "+vtPnlLaAbBcCYDmdHIMSefFprRTXzEisg@luioO%";
gabime's avatar
gabime committed
33
    std::vector<std::string> basic_patterns;
gabime's avatar
gabime committed
34
    for (auto &flag : all_flags)
gabime's avatar
gabime committed
35 36
    {
        auto pattern = std::string("%") + flag;
gabime's avatar
gabime committed
37 38
        benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);

gabime's avatar
gabime committed
39 40 41 42 43 44
        //        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);
gabime's avatar
gabime committed
45 46 47 48
    }

    // complex patterns
    std::vector<std::string> patterns = {
gabime's avatar
gabime committed
49 50 51
        "[%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",
gabime's avatar
gabime committed
52
    };
gabime's avatar
gabime committed
53
    for (auto &pattern : patterns)
gabime's avatar
gabime committed
54
    {
55
        benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern)->Iterations(2500000);
gabime's avatar
gabime committed
56 57 58
    }
}

gabime's avatar
gabime committed
59
int main(int argc, char *argv[])
gabime's avatar
gabime committed
60
{
gabime's avatar
gabime committed
61

62
    spdlog::set_pattern("[%^%l%$] %v");
gabime's avatar
gabime committed
63
    if (argc != 2)
gabime's avatar
gabime committed
64
    {
65
        spdlog::error("Usage: {} <pattern> (or \"all\" to bench all)", argv[0]);
66
        exit(1);
gabime's avatar
gabime committed
67
    }
68 69

    std::string pattern = argv[1];
gabime's avatar
gabime committed
70
    if (pattern == "all")
gabime's avatar
gabime committed
71 72
    {
        bench_formatters();
gabime's avatar
gabime committed
73
    }
74 75 76 77
    else
    {
        benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
    }
gabime's avatar
gabime committed
78 79 80
    benchmark::Initialize(&argc, argv);
    benchmark::RunSpecifiedBenchmarks();
}