spdlog/src/test.cpp

150 lines
3.8 KiB
C++
Raw Normal View History

2014-01-25 08:52:10 -05:00
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
2014-01-28 21:00:05 -05:00
#include <functional>
2014-01-26 14:23:26 -05: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 08:52:10 -05:00
2014-01-28 21:00:05 -05:00
std::atomic<uint64_t> push_count, pop_count;
std::atomic<bool> active;
using Q = c11log::details::blocking_queue<std::string>;
using std::chrono::seconds;
void pusher(Q* q)
{
while(active)
{
//if(q->push("Hello", seconds(10)))
q->push("hello");
++push_count;
}
}
void popper(Q* q)
{
std::string output;
while(active)
{
//if(q->pop(output, seconds(10)))
q->pop(output);
++pop_count;
}
}
void testq(int size, int pushers, int poppers)
{
active = true;
Q q{static_cast<Q::size_type>(size)};
for(int i = 0; i < poppers; i++)
new std::thread(std::bind(popper, &q));
for(int i = 0; i < pushers; i++)
new std::thread(std::bind(pusher, &q));
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;
cout << "Total/sec =\t" << format(push_count+pop_count) << endl << endl;
cout << "Queue size =\t" << format(q.size()) << endl;
cout << "---------------------------------------------------------------------" << endl;
}
2014-01-25 10:28:56 -05:00
2014-01-28 21:00:05 -05:00
}
2014-01-25 08:52:10 -05:00
int main(int argc, char* argv[])
{
2014-01-28 21:00:05 -05: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]);
testq(qsize, pushers, poppers);
/*
2014-01-27 12:35:18 -05:00
using namespace std::chrono;
2014-01-28 21:00:05 -05:00
2014-01-26 14:23:26 -05:00
int nthreads = argc > 1 ? atoi(argv[1]) : 1;
2014-01-28 21:00:05 -05:00
int nlines = argc > 2 ? atoi(argv[2]) : 100000;
2014-01-27 12:35:18 -05:00
2014-01-28 21:00:05 -05:00
2014-01-25 18:53:23 -05:00
auto null_sink = std::make_shared<c11log::sinks::null_sink>();
2014-01-26 14:23:26 -05:00
auto stdout_sink = std::make_shared<c11log::sinks::stdout_sink>();
2014-01-28 21:00:05 -05:00
auto async = std::make_shared<c11log::sinks::async_sink>(1000);
2014-01-27 06:07:57 -05:00
//auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*10 , 2);
2014-01-28 21:00:05 -05:00
//auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
2014-01-25 10:28:56 -05:00
2014-01-28 21:00:05 -05:00
//async->add_sink(fsink);
async->add_sink(null_sink);
2014-01-25 10:28:56 -05:00
2014-01-28 21:00:05 -05:00
//console logger
auto &console = c11log::get_logger("console");
console.add_sink(stdout_sink);
//c11log::details::blocking_queue<std::string> q(1000);
//auto q_ptr = &q;
2014-01-27 06:57:52 -05:00
std::vector<std::thread*> threads;
2014-01-27 12:35:18 -05:00
std::cout << "Starting " << nthreads << " threads x " << utils::format(nlines) << " lines each.." << std::endl;
2014-01-26 14:23:26 -05:00
for (int i = 0; i < nthreads; i++)
2014-01-28 21:00:05 -05:00
{
auto logger = std::make_shared<c11log::logger>("test");
logger->add_sink(async);
auto t = new std::thread([logger, nlines, i]() {
auto &console = c11log::get_logger("console");
for(int j = 0 ; j < nlines; ++j)
{
logger->info() << "Hello from thread #" << i << "\tcounter: " << j ;
if(j % 2000 == 0)
console.info() << "Hello from thread " << i << "\tcounter: " << j;
}
2014-01-25 18:53:23 -05:00
});
2014-01-27 06:57:52 -05:00
threads.push_back(t);
2014-01-28 21:00:05 -05:00
//std::this_thread::sleep_for(milliseconds(2));
2014-01-25 18:53:23 -05:00
}
2014-01-27 06:57:52 -05:00
2014-01-27 12:35:18 -05:00
auto stime = steady_clock::now();
2014-01-28 21:00:05 -05:00
int thread_joined = 0;
2014-01-27 06:57:52 -05:00
for(auto t:threads)
2014-01-28 21:00:05 -05:00
{
2014-01-27 06:57:52 -05:00
t->join();
2014-01-28 21:00:05 -05:00
std::cout << "Joined " << ++thread_joined << " threads" << std::endl;
}
2014-01-27 12:35:18 -05:00
auto delta = steady_clock::now() - stime;
auto delta_seconds = duration_cast<milliseconds>(delta).count()/1000.0;
auto total = nthreads*nlines;
std::cout << "Total: " << utils::format(total) << " = " << utils::format(total/delta_seconds) << "/sec" << std::endl;
async->shutdown(seconds(1));
2014-01-28 21:00:05 -05:00
*/
2014-01-25 08:52:10 -05:00
}