bench.cpp 4.31 KB
Newer Older
gabime's avatar
gabime committed
1 2 3 4
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
gabime's avatar
gabime committed
5

gabi's avatar
gabi committed
6
//
gabime's avatar
gabime committed
7 8
// bench.cpp : spdlog benchmarks
//
gabime's avatar
gabime committed
9 10 11 12 13
#include "spdlog/async_logger.h"
#include "spdlog/sinks/file_sinks.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
14 15
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
gabime's avatar
gabime committed
16 17 18 19
#include <iostream>
#include <memory>
#include <string>
#include <thread>
20 21

using namespace std;
gabi's avatar
gabi committed
22
using namespace std::chrono;
23
using namespace spdlog;
gabime's avatar
gabime committed
24
using namespace spdlog::sinks;
gabi's avatar
gabi committed
25 26
using namespace utils;

gabi's avatar
gabi committed
27 28
void bench(int howmany, std::shared_ptr<spdlog::logger> log);
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
gabi's avatar
gabi committed
29

gabime's avatar
gabime committed
30
int main(int argc, char *argv[])
gabi's avatar
gabi committed
31
{
gabi's avatar
gabi committed
32

33 34
    int queue_size = 1048576;
    int howmany = 1000000;
gabime's avatar
gabime committed
35 36 37 38 39 40 41
    int threads = 10;
    int file_size = 30 * 1024 * 1024;
    int rotating_files = 5;

    try
    {

gabime's avatar
gabime committed
42
        if (argc > 1)
gabime's avatar
gabime committed
43 44
            howmany = atoi(argv[1]);
        if (argc > 2)
gabime's avatar
gabime committed
45
            threads = atoi(argv[2]);
46 47
        if (argc > 3)
            queue_size = atoi(argv[3]);
gabime's avatar
gabime committed
48 49

        cout << "*******************************************************************************\n";
gabime's avatar
gabime committed
50
        cout << "Single thread, " << format(howmany) << " iterations" << endl;
gabime's avatar
gabime committed
51 52
        cout << "*******************************************************************************\n";

53
        auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st.log", file_size, rotating_files);
gabime's avatar
gabime committed
54
        bench(howmany, rotating_st);
55
        auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st.log");
gabime's avatar
gabime committed
56 57 58 59
        bench(howmany, daily_st);
        bench(howmany, spdlog::create<null_sink_st>("null_st"));

        cout << "\n*******************************************************************************\n";
gabime's avatar
gabime committed
60
        cout << threads << " threads sharing same logger, " << format(howmany) << " iterations" << endl;
gabime's avatar
gabime committed
61 62
        cout << "*******************************************************************************\n";

63
        auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt.log", file_size, rotating_files);
gabime's avatar
gabime committed
64 65
        bench_mt(howmany, rotating_mt, threads);

66
        auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt.log");
gabime's avatar
gabime committed
67 68 69 70
        bench_mt(howmany, daily_mt, threads);
        bench(howmany, spdlog::create<null_sink_st>("null_mt"));

        cout << "\n*******************************************************************************\n";
gabime's avatar
gabime committed
71
        cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations " << endl;
gabime's avatar
gabime committed
72 73
        cout << "*******************************************************************************\n";

74
        spdlog::set_async_mode(queue_size);
gabime's avatar
gabime committed
75

gabime's avatar
gabime committed
76
        for (int i = 0; i < 3; ++i)
gabime's avatar
gabime committed
77
        {
78
            auto as = spdlog::daily_logger_st("as", "logs/daily_async.log");
gabime's avatar
gabime committed
79 80 81 82 83 84 85 86
            bench_mt(howmany, as, threads);
            spdlog::drop("as");
        }
    }
    catch (std::exception &ex)
    {
        std::cerr << "Error: " << ex.what() << std::endl;
        perror("Last error");
87
        return EXIT_FAILURE;
gabime's avatar
gabime committed
88
    }
89
    return EXIT_SUCCESS;
gabi's avatar
gabi committed
90 91
}

gabi's avatar
gabi committed
92 93
void bench(int howmany, std::shared_ptr<spdlog::logger> log)
{
gabime's avatar
gabime committed
94 95 96 97 98 99
    cout << log->name() << "...\t\t" << flush;
    auto start = system_clock::now();
    for (auto i = 0; i < howmany; ++i)
    {
        log->info("Hello logger: msg number {}", i);
    }
gabi's avatar
gabi committed
100

gabime's avatar
gabime committed
101
    auto delta = system_clock::now() - start;
gabime's avatar
gabime committed
102
    auto delta_d = duration_cast<duration<double>>(delta).count();
gabime's avatar
gabime committed
103
    cout << format(int(howmany / delta_d)) << "/sec" << endl;
gabi's avatar
gabi committed
104 105 106 107 108
}

void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{

gabime's avatar
gabime committed
109
    cout << log->name() << "...\t\t" << flush;
gabime's avatar
gabime committed
110
    std::atomic<int> msg_counter{0};
gabime's avatar
gabime committed
111 112 113 114
    vector<thread> threads;
    auto start = system_clock::now();
    for (int t = 0; t < thread_count; ++t)
    {
gabime's avatar
gabime committed
115 116
        threads.push_back(std::thread([&]() {
            for (;;)
gabime's avatar
gabime committed
117 118
            {
                int counter = ++msg_counter;
gabime's avatar
gabime committed
119 120
                if (counter > howmany)
                    break;
gabime's avatar
gabime committed
121 122 123 124 125
                log->info("Hello logger: msg number {}", counter);
            }
        }));
    }

gabime's avatar
gabime committed
126
    for (auto &t : threads)
gabime's avatar
gabime committed
127 128 129 130 131
    {
        t.join();
    };

    auto delta = system_clock::now() - start;
gabime's avatar
gabime committed
132
    auto delta_d = duration_cast<duration<double>>(delta).count();
gabime's avatar
gabime committed
133
    cout << format(int(howmany / delta_d)) << "/sec" << endl;
gabi's avatar
gabi committed
134
}