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

gabime's avatar
gabime committed
6
#include <atomic>
7 8
#include <chrono>
#include <iostream>
gabime's avatar
gabime committed
9 10 11 12 13 14 15
#include <thread>
#include <vector>

#include "glog/logging.h"

using namespace std;

gabime's avatar
gabime committed
16
int main(int argc, char *argv[])
gabime's avatar
gabime committed
17
{
18 19
    using namespace std::chrono;
    using clock = steady_clock;
gabime's avatar
gabime committed
20

gabime's avatar
gabime committed
21
    int thread_count = 10;
gabime's avatar
gabime committed
22
    if (argc > 1)
gabime's avatar
gabime committed
23 24 25 26 27 28 29 30
        thread_count = atoi(argv[1]);

    int howmany = 1000000;

    FLAGS_logtostderr = 0;
    FLAGS_log_dir = "logs";
    google::InitGoogleLogging(argv[0]);

gabime's avatar
gabime committed
31
    std::atomic<int> msg_counter{0};
gabime's avatar
gabime committed
32 33
    vector<thread> threads;

34
    auto start = clock::now();
gabime's avatar
gabime committed
35 36
    for (int t = 0; t < thread_count; ++t)
    {
gabime's avatar
gabime committed
37
        threads.push_back(std::thread([&]() {
gabime's avatar
gabime committed
38 39 40
            while (true)
            {
                int counter = ++msg_counter;
gabime's avatar
gabime committed
41 42
                if (counter > howmany)
                    break;
gabime's avatar
gabime committed
43 44 45 46 47
                LOG(INFO) << "glog message #" << counter << ": This is some text for your pleasure";
            }
        }));
    }

gabime's avatar
gabime committed
48
    for (auto &t : threads)
gabime's avatar
gabime committed
49 50
    {
        t.join();
51 52 53 54 55 56 57 58 59 60
    }

    duration<float> delta = clock::now() - start;
    float deltaf = delta.count();
    auto rate = howmany / deltaf;

    std::cout << "Total: " << howmany << std::endl;
    std::cout << "Threads: " << thread_count << std::endl;
    std::cout << "Delta = " << deltaf << " seconds" << std::endl;
    std::cout << "Rate = " << rate << "/sec" << std::endl;
gabime's avatar
gabime committed
61 62

    return 0;
gabime's avatar
gabime committed
63
}