2014-01-25 21:52:10 +08:00
|
|
|
// test.cpp : Defines the entry point for the console application.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2014-01-29 10:00:05 +08:00
|
|
|
#include <functional>
|
|
|
|
|
2014-01-27 03:23:26 +08:00
|
|
|
#include "c11log/logger.h"
|
|
|
|
#include "c11log/sinks/async_sink.h"
|
|
|
|
#include "c11log/sinks/file_sinks.h"
|
|
|
|
#include "c11log/sinks/stdout_sinks.h"
|
|
|
|
|
|
|
|
#include "utils.h"
|
2014-01-25 21:52:10 +08:00
|
|
|
|
|
|
|
|
2014-01-29 10:00:05 +08:00
|
|
|
std::atomic<uint64_t> push_count, pop_count;
|
|
|
|
std::atomic<bool> active;
|
|
|
|
|
2014-02-01 07:47:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
using std::string;
|
2014-01-29 10:00:05 +08:00
|
|
|
using std::chrono::seconds;
|
2014-02-01 07:47:37 +08:00
|
|
|
using Q = c11log::details::blocking_queue<string>;
|
2014-01-29 10:00:05 +08:00
|
|
|
|
|
|
|
void pusher(Q* q)
|
|
|
|
{
|
2014-02-04 02:28:19 +08:00
|
|
|
auto &logger = c11log::get_logger("async");
|
|
|
|
while(active)
|
|
|
|
{
|
|
|
|
logger.info()<<"Hello logger!";
|
|
|
|
++push_count;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2014-02-01 07:47:37 +08:00
|
|
|
string a = "Hello";
|
2014-01-29 10:00:05 +08:00
|
|
|
while(active)
|
|
|
|
{
|
2014-02-01 07:47:37 +08:00
|
|
|
q->push(a);
|
2014-01-29 10:00:05 +08:00
|
|
|
++push_count;
|
|
|
|
}
|
2014-02-04 02:28:19 +08:00
|
|
|
*/
|
2014-01-29 10:00:05 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
void popper(Q* q)
|
|
|
|
{
|
2014-02-01 07:47:37 +08:00
|
|
|
string output;
|
2014-01-29 10:00:05 +08:00
|
|
|
while(active)
|
2014-02-02 06:08:47 +08:00
|
|
|
{
|
|
|
|
q->pop(output);
|
2014-01-29 10:00:05 +08:00
|
|
|
++pop_count;
|
|
|
|
}
|
2014-02-04 02:28:19 +08:00
|
|
|
|
2014-01-29 10:00:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testq(int size, int pushers, int poppers)
|
|
|
|
{
|
|
|
|
|
|
|
|
active = true;
|
|
|
|
Q q{static_cast<Q::size_type>(size)};
|
2014-02-01 07:47:37 +08:00
|
|
|
|
2014-02-04 02:28:19 +08:00
|
|
|
/*
|
2014-01-29 10:00:05 +08:00
|
|
|
for(int i = 0; i < poppers; i++)
|
2014-02-04 02:28:19 +08:00
|
|
|
testq(qsize, pushers, poppers);
|
|
|
|
*/
|
2014-01-29 10:00:05 +08:00
|
|
|
for(int i = 0; i < pushers; i++)
|
|
|
|
new std::thread(std::bind(pusher, &q));
|
2014-02-02 05:49:55 +08:00
|
|
|
|
2014-01-29 10:00:05 +08:00
|
|
|
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;
|
2014-02-04 02:28:19 +08:00
|
|
|
//cout << "Pops/sec =\t" << format(pop_count.load()) << endl << endl;
|
|
|
|
//cout << "Total/sec =\t" << format(push_count+pop_count) << endl;
|
2014-01-29 10:00:05 +08:00
|
|
|
cout << "Queue size =\t" << format(q.size()) << endl;
|
|
|
|
cout << "---------------------------------------------------------------------" << endl;
|
2014-02-04 02:28:19 +08:00
|
|
|
}
|
2014-01-29 10:00:05 +08:00
|
|
|
}
|
2014-02-01 07:47:37 +08:00
|
|
|
|
|
|
|
|
2014-01-25 21:52:10 +08:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2014-02-01 07:47:37 +08:00
|
|
|
|
2014-01-29 10:00:05 +08:00
|
|
|
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]);
|
|
|
|
int poppers = atoi(argv[3]);
|
|
|
|
|
2014-02-04 02:28:19 +08:00
|
|
|
//testq(qsize, pushers, poppers);
|
2014-01-29 10:00:05 +08:00
|
|
|
|
|
|
|
|
2014-02-04 02:28:19 +08:00
|
|
|
using namespace std::chrono;
|
2014-01-28 01:35:18 +08:00
|
|
|
|
2014-02-04 02:28:19 +08:00
|
|
|
|
2014-01-26 07:53:23 +08:00
|
|
|
auto null_sink = std::make_shared<c11log::sinks::null_sink>();
|
2014-01-27 03:23:26 +08:00
|
|
|
auto stdout_sink = std::make_shared<c11log::sinks::stdout_sink>();
|
2014-01-29 10:00:05 +08:00
|
|
|
auto async = std::make_shared<c11log::sinks::async_sink>(1000);
|
2014-02-04 02:28:19 +08:00
|
|
|
auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*10 , 2);
|
2014-01-29 10:00:05 +08:00
|
|
|
//auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
|
2014-01-25 23:28:56 +08:00
|
|
|
|
2014-02-04 02:28:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
//Async logger
|
|
|
|
async->add_sink(null_sink);
|
|
|
|
auto &logger = c11log::get_logger("async");
|
|
|
|
logger.add_sink(async);
|
2014-01-29 10:00:05 +08:00
|
|
|
|
2014-02-04 02:28:19 +08:00
|
|
|
testq(qsize, pushers, poppers);
|
2014-01-27 19:57:52 +08:00
|
|
|
|
2014-01-28 01:35:18 +08:00
|
|
|
|
2014-01-25 21:52:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|