spdlog-null-async.cpp 2.79 KB
Newer Older
1 2 3 4 5 6 7 8
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//

//
// bench.cpp : spdlog benchmarks
//
gabime's avatar
gabime committed
9 10 11 12
#include "spdlog/async_logger.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <atomic>
#include <cstdlib> // EXIT_FAILURE
#include <iostream>
#include <memory>
#include <string>
#include <thread>

using namespace std;
using namespace std::chrono;
using namespace spdlog;
using namespace spdlog::sinks;
using namespace utils;

size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);

gabime's avatar
gabime committed
28
int main(int argc, char *argv[])
29 30 31 32 33 34 35 36 37 38
{

    int queue_size = 1048576;
    int howmany = 1000000;
    int threads = 10;
    int iters = 10;

    try
    {

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

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

        spdlog::set_async_mode(queue_size);

        size_t total_rate = 0;

gabime's avatar
gabime committed
54
        for (int i = 0; i < iters; ++i)
55
        {
gabime's avatar
gabime committed
56
            // auto as = spdlog::daily_logger_st("as", "logs/daily_async");
57
            auto as = spdlog::create<null_sink_st>("async(null-sink)");
gabime's avatar
gabime committed
58
            total_rate += bench_as(howmany, as, threads);
59 60 61
            spdlog::drop("async(null-sink)");
        }
        std::cout << endl;
gabime's avatar
gabime committed
62
        std::cout << "Avg rate: " << format(total_rate / iters) << "/sec" << std::endl;
63 64 65 66 67 68 69 70 71 72
    }
    catch (std::exception &ex)
    {
        std::cerr << "Error: " << ex.what() << std::endl;
        perror("Last error");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

gabime's avatar
gabime committed
73
// return rate/sec
74 75 76
size_t bench_as(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{
    cout << log->name() << "...\t\t" << flush;
gabime's avatar
gabime committed
77
    std::atomic<int> msg_counter{0};
78 79 80 81
    vector<thread> threads;
    auto start = system_clock::now();
    for (int t = 0; t < thread_count; ++t)
    {
gabime's avatar
gabime committed
82 83
        threads.push_back(std::thread([&]() {
            for (;;)
84 85
            {
                int counter = ++msg_counter;
gabime's avatar
gabime committed
86 87
                if (counter > howmany)
                    break;
88 89 90 91 92
                log->info("Hello logger: msg number {}", counter);
            }
        }));
    }

gabime's avatar
gabime committed
93
    for (auto &t : threads)
94 95
    {
        t.join();
96
    }
97 98

    auto delta = system_clock::now() - start;
gabime's avatar
gabime committed
99
    auto delta_d = duration_cast<duration<double>>(delta).count();
100 101 102 103
    auto per_sec = size_t(howmany / delta_d);
    cout << format(per_sec) << "/sec" << endl;
    return per_sec;
}