This commit is contained in:
gabime
2014-02-21 22:51:54 +02:00
parent 2f6a5eabe0
commit c5a8eb5cdb
18 changed files with 496 additions and 600 deletions

View File

@@ -1,6 +1,6 @@
// test.cpp : Defines the entry point for the console application.
// example.cpp : Simple logger example
//
#include "stdafx.h"
#include <string>
#include <functional>
#include "c11log/logger.h"
@@ -10,84 +10,63 @@
#include "utils.h"
std::atomic<uint64_t> push_count, pop_count;
std::atomic<uint64_t> log_count;
std::atomic<bool> active;
using std::string;
using std::chrono::seconds;
using Q = c11log::details::blocking_queue<string>;
void pusher(Q* )
void logging_thread()
{
auto &logger = c11log::get_logger("async");
while(active)
{
logger.info()<<"Hello logger!";
++push_count;
}
auto &logger = c11log::get_logger("async");
while(active) {
logger.info()<<"Hello logger!";
++log_count;
}
}
void testq(int size, int pushers /*int poppers*/)
void testlog(int threads)
{
active = true;
Q q{static_cast<Q::size_type>(size)};
active = true;
/*
for(int i = 0; i < poppers; i++)
testq(qsize, pushers, poppers);
*/
for(int i = 0; i < pushers; i++)
new std::thread(std::bind(pusher, &q));
for(int i = 0; i < threads; i++)
new std::thread(std::bind(logging_thread));
while(active)
{
using std::endl;
using std::cout;
using utils::format;
while(active) {
using std::endl;
using std::cout;
using utils::format;
push_count = 0;
pop_count = 0;
std::this_thread::sleep_for(seconds(1));
cout << "Pushes/sec =\t" << format(push_count.load()) << endl;
//cout << "Pops/sec =\t" << format(pop_count.load()) << endl << endl;
//cout << "Total/sec =\t" << format(push_count+pop_count) << endl;
cout << "Queue size =\t" << format(q.size()) << endl;
cout << "---------------------------------------------------------------------" << endl;
}
log_count = 0;
std::this_thread::sleep_for(seconds(1));
cout << "Logs/sec =\t" << format(log_count.load()) << endl;
}
}
int main(int argc, char* argv[])
{
using namespace std::chrono;
if(argc !=4)
{
std::cerr << "Usage: " << argv[0] << " qsize, pushers, poppers" << std::endl;
return 0;
}
int qsize = atoi(argv[1]);
int pushers = atoi(argv[2]);
if(argc !=3) {
std::cerr << "Usage: " << argv[0] << " qsize, threads" << std::endl;
return 0;
}
int qsize = atoi(argv[1]);
int threads = atoi(argv[2]);
using namespace std::chrono;
using namespace c11log::sinks;
auto null_sink = std::make_shared<null_sink>();
auto stdout_sink = std::make_shared<stdout_sink>();
auto async = std::make_shared<async_sink>(qsize);
auto fsink = std::make_shared<rotating_file_sink>("example_log", "txt", 1024*1024*50 , 5);
async->add_sink(fsink);
auto null_sink = std::make_shared<c11log::sinks::null_sink>();
auto stdout_sink = std::make_shared<c11log::sinks::stdout_sink>();
auto async = std::make_shared<c11log::sinks::async_sink>(1000);
auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5);
auto &logger = c11log::get_logger("async");
logger.add_sink(async);
async->add_sink(fsink);
auto &logger = c11log::get_logger("async");
logger.add_sink(async);
testq(qsize, pushers);
testlog(threads);
}

54
example/utils.h Normal file
View File

@@ -0,0 +1,54 @@
#pragma once
#include <functional>
#include <chrono>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <locale>
namespace utils {
template<typename T>
std::string format(const T& value)
{
static std::locale loc("");
std::stringstream ss;
ss.imbue(loc);
ss << value;
return ss.str();
}
inline void bench(const std::string& fn_name, const std::chrono::milliseconds &duration, const std::function<void() >& fn)
{
using namespace std::chrono;
typedef steady_clock the_clock;
size_t counter = 0;
seconds print_interval(1);
auto start_time = the_clock::now();
auto lastPrintTime = start_time;
while (true) {
fn();
++counter;
auto now = the_clock::now();
if (now - start_time >= duration)
break;
if (now - lastPrintTime >= print_interval) {
std::cout << fn_name << ": " << format(counter) << " per sec" << std::endl;
counter = 0;
lastPrintTime = the_clock::now();
}
}
}
inline void bench(const std::string& fn_name, const std::function<void() >& fn)
{
using namespace std::chrono;
auto start = steady_clock::now();
fn();
auto delta = steady_clock::now() - start;
std::cout << fn_name << ": " << duration_cast<milliseconds>(delta).count() << " ms" << std::endl;
}
}