bench.cpp 4.45 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
//
9 10
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
gabime's avatar
gabime committed
11 12 13 14
#include <iostream>
#include <memory>
#include <string>
#include <thread>
15
#include "spdlog/spdlog.h"
gabi's avatar
gabi committed
16
#include "spdlog/async_logger.h"
17 18
#include "spdlog/sinks/file_sinks.h"
#include "spdlog/sinks/null_sink.h"
gabi's avatar
gabi committed
19 20
#include "utils.h"

21 22

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


gabi's avatar
gabi committed
29 30
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
31

gabi's avatar
gabi committed
32
int main(int argc, char* argv[])
gabi's avatar
gabi committed
33
{
gabi's avatar
gabi committed
34

35 36
    int queue_size = 1048576;
    int howmany = 1000000;
gabime's avatar
gabime committed
37 38 39 40 41 42 43 44 45 46 47 48
    int threads = 10;
    bool auto_flush = false;
    int file_size = 30 * 1024 * 1024;
    int rotating_files = 5;

    try
    {

        if(argc > 1)
            howmany = atoi(argv[1]);
        if (argc > 2)
            threads =   atoi(argv[2]);
49 50
        if (argc > 3)
            queue_size = atoi(argv[3]);
gabime's avatar
gabime committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79


        cout << "*******************************************************************************\n";
        cout << "Single thread, " << format(howmany)  << " iterations, auto flush=" << auto_flush << endl;
        cout << "*******************************************************************************\n";

        auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st", file_size, rotating_files, auto_flush);
        bench(howmany, rotating_st);
        auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st", auto_flush);
        bench(howmany, daily_st);
        bench(howmany, spdlog::create<null_sink_st>("null_st"));

        cout << "\n*******************************************************************************\n";
        cout << threads << " threads sharing same logger, " << format(howmany)  << " iterations, auto_flush=" << auto_flush << endl;
        cout << "*******************************************************************************\n";

        auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt", file_size, rotating_files, auto_flush);
        bench_mt(howmany, rotating_mt, threads);


        auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt", auto_flush);
        bench_mt(howmany, daily_mt, threads);
        bench(howmany, spdlog::create<null_sink_st>("null_mt"));

        cout << "\n*******************************************************************************\n";
        cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations, auto_flush=" << auto_flush << endl;
        cout << "*******************************************************************************\n";


80
        spdlog::set_async_mode(queue_size);
gabime's avatar
gabime committed
81 82 83 84 85 86 87 88 89 90 91 92

        for(int i = 0; i < 3; ++i)
        {
            auto as = spdlog::daily_logger_st("as", "logs/daily_async", auto_flush);
            bench_mt(howmany, as, threads);
            spdlog::drop("as");
        }
    }
    catch (std::exception &ex)
    {
        std::cerr << "Error: " << ex.what() << std::endl;
        perror("Last error");
93
        return EXIT_FAILURE;
gabime's avatar
gabime committed
94
    }
95
    return EXIT_SUCCESS;
gabi's avatar
gabi committed
96 97
}

gabi's avatar
gabi committed
98 99 100

void bench(int howmany, std::shared_ptr<spdlog::logger> log)
{
gabime's avatar
gabime committed
101 102 103 104 105 106
    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
107 108


gabime's avatar
gabime committed
109 110 111
    auto delta = system_clock::now() - start;
    auto delta_d = duration_cast<duration<double>> (delta).count();
    cout << format(int(howmany / delta_d)) << "/sec" << endl;
gabi's avatar
gabi committed
112 113 114 115 116 117
}


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

gabime's avatar
gabime committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    cout << log->name() << "...\t\t" << flush;
    std::atomic<int > msg_counter {0};
    vector<thread> threads;
    auto start = system_clock::now();
    for (int t = 0; t < thread_count; ++t)
    {
        threads.push_back(std::thread([&]()
        {
            for(;;)
            {
                int counter = ++msg_counter;
                if (counter > howmany) break;
                log->info("Hello logger: msg number {}", counter);
            }
        }));
    }


    for(auto &t:threads)
    {
        t.join();
    };


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