spdlog/src/test.cpp

186 lines
4.1 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;
2014-01-31 18:47:37 -05:00
class A
{
public:
A()
{
std::cout << "Regular ctor\n";
}
A(const A&)
{
std::cout << "Copy ctor\n";
}
A& operator=(const A&)
{
std::cout << "operator=\n";
return *this;
}
A& operator=(A&&)
{
std::cout << "operator=&&\n";
return *this;
}
A(A&& a)
{
std::cout << "Move ctor\n";
}
~A()
{
//std::cout << "Dtor\n";
}
};
using std::string;
2014-01-28 21:00:05 -05:00
using std::chrono::seconds;
2014-01-31 18:47:37 -05:00
using Q = c11log::details::blocking_queue<string>;
2014-01-28 21:00:05 -05:00
void pusher(Q* q)
{
2014-01-31 18:47:37 -05:00
string a = "Hello";
2014-01-28 21:00:05 -05:00
while(active)
{
2014-01-31 18:47:37 -05:00
q->push(a);
2014-01-28 21:00:05 -05:00
++push_count;
}
}
void popper(Q* q)
{
2014-01-31 18:47:37 -05:00
string output;
2014-01-28 21:00:05 -05:00
while(active)
2014-02-01 17:08:47 -05:00
{
q->pop(output);
2014-01-28 21:00:05 -05:00
++pop_count;
}
}
void testq(int size, int pushers, int poppers)
{
active = true;
Q q{static_cast<Q::size_type>(size)};
2014-01-31 18:47:37 -05:00
2014-01-28 21:00:05 -05:00
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));
2014-02-01 16:49:55 -05:00
2014-01-28 21:00:05 -05: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;
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-31 18:47:37 -05:00
2014-01-25 08:52:10 -05:00
int main(int argc, char* argv[])
{
2014-01-31 18:47:37 -05:00
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
}