boost-bench-mt.cpp 1.84 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 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <thread>
#include <vector>
#include <atomic>

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

void init()
{
gabime's avatar
gabime committed
26 27 28 29 30 31 32 33 34 35 36
    logging::add_file_log
    (
        keywords::file_name = "logs/boost-sample_%N.log",                              /*< file name pattern >*/
        keywords::auto_flush = false,
        keywords::format = "[%TimeStamp%]: %Message%"
    );

    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );
gabime's avatar
gabime committed
37 38 39 40 41 42
}



using namespace std;

43
int main(int argc, char* argv[])
gabime's avatar
gabime committed
44
{
gabime's avatar
gabime committed
45 46 47
    int thread_count = 10;
    if(argc > 1)
        thread_count = atoi(argv[1]);
gabime's avatar
gabime committed
48

gabime's avatar
gabime committed
49
    int howmany = 1000000;
gabime's avatar
gabime committed
50

51

gabime's avatar
gabime committed
52 53
    init();
    logging::add_common_attributes();
gabime's avatar
gabime committed
54

gabime's avatar
gabime committed
55

gabime's avatar
gabime committed
56
    using namespace logging::trivial;
gabime's avatar
gabime committed
57

gabime's avatar
gabime committed
58
    src::severity_logger_mt< severity_level > lg;
gabime's avatar
gabime committed
59

gabime's avatar
gabime committed
60 61
    std::atomic<int > msg_counter {0};
    vector<thread> threads;
gabime's avatar
gabime committed
62

gabime's avatar
gabime committed
63 64 65 66 67 68 69 70 71 72 73 74
    for (int t = 0; t < thread_count; ++t)
    {
        threads.push_back(std::thread([&]()
        {
            while (true)
            {
                int counter = ++msg_counter;
                if (counter > howmany) break;
                BOOST_LOG_SEV(lg, info) << "boost message #" << counter << ": This is some text for your pleasure";
            }
        }));
    }
gabime's avatar
gabime committed
75 76


gabime's avatar
gabime committed
77 78 79 80
    for(auto &t:threads)
    {
        t.join();
    };
gabime's avatar
gabime committed
81 82


gabime's avatar
gabime committed
83
    return 0;
gabime's avatar
gabime committed
84
}