bench.cpp 4.29 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
    int threads = 10;
    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]);
48 49
        if (argc > 3)
            queue_size = atoi(argv[3]);
gabime's avatar
gabime committed
50 51 52


        cout << "*******************************************************************************\n";
gabime's avatar
gabime committed
53
        cout << "Single thread, " << format(howmany)  << " iterations" << endl;
gabime's avatar
gabime committed
54 55
        cout << "*******************************************************************************\n";

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

        cout << "\n*******************************************************************************\n";
gabime's avatar
gabime committed
63
        cout << threads << " threads sharing same logger, " << format(howmany)  << " iterations" << endl;
gabime's avatar
gabime committed
64 65
        cout << "*******************************************************************************\n";

gabime's avatar
gabime committed
66
        auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt", file_size, rotating_files);
gabime's avatar
gabime committed
67 68 69
        bench_mt(howmany, rotating_mt, threads);


gabime's avatar
gabime committed
70
        auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt");
gabime's avatar
gabime committed
71 72 73 74
        bench_mt(howmany, daily_mt, threads);
        bench(howmany, spdlog::create<null_sink_st>("null_mt"));

        cout << "\n*******************************************************************************\n";
gabime's avatar
gabime committed
75
        cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations " << endl;
gabime's avatar
gabime committed
76 77 78
        cout << "*******************************************************************************\n";


79
        spdlog::set_async_mode(queue_size);
gabime's avatar
gabime committed
80 81 82

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

gabi's avatar
gabi committed
97 98 99

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


gabime's avatar
gabime committed
108 109 110
    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
111 112 113 114 115 116
}


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

gabime's avatar
gabime committed
117 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
    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
144
}