You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/bench/boost-bench-mt.cpp

87 lines
2.2 KiB
C++

10 years ago
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
8 years ago
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sources/record_ostream.hpp>
8 years ago
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
void init()
{
logging::add_file_log(keywords::file_name = "logs/boost-bench-mt_%N.log", /*< file name pattern >*/
8 years ago
keywords::auto_flush = false, keywords::format = "[%TimeStamp%]: %Message%");
11 years ago
8 years ago
logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);
}
using namespace std;
8 years ago
int main(int argc, char *argv[])
{
using namespace std::chrono;
using clock = steady_clock;
11 years ago
int thread_count = 10;
8 years ago
if (argc > 1)
11 years ago
thread_count = atoi(argv[1]);
11 years ago
11 years ago
int howmany = 1000000;
11 years ago
11 years ago
init();
logging::add_common_attributes();
11 years ago
11 years ago
using namespace logging::trivial;
11 years ago
8 years ago
src::severity_logger_mt<severity_level> lg;
11 years ago
8 years ago
std::atomic<int> msg_counter{0};
11 years ago
vector<thread> threads;
11 years ago
auto start = clock::now();
11 years ago
for (int t = 0; t < thread_count; ++t)
{
8 years ago
threads.push_back(std::thread([&]() {
11 years ago
while (true)
{
int counter = ++msg_counter;
8 years ago
if (counter > howmany)
break;
11 years ago
BOOST_LOG_SEV(lg, info) << "boost message #" << counter << ": This is some text for your pleasure";
}
}));
}
8 years ago
for (auto &t : threads)
11 years ago
{
t.join();
}
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;
11 years ago
return 0;
}