mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
balagan
This commit is contained in:
@@ -9,8 +9,7 @@ void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, st
|
||||
//get ms
|
||||
//auto duration = tp.time_since_epoch();
|
||||
//int millis = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
|
||||
|
||||
|
||||
|
||||
char buf[64];
|
||||
auto size = sprintf(buf, "[%d-%02d-%02d %02d:%02d:%02d]",
|
||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
|
127
src/test.cpp
127
src/test.cpp
@@ -2,6 +2,8 @@
|
||||
//
|
||||
|
||||
#include "stdafx.h"
|
||||
#include <functional>
|
||||
|
||||
#include "c11log/logger.h"
|
||||
#include "c11log/sinks/async_sink.h"
|
||||
#include "c11log/sinks/file_sinks.h"
|
||||
@@ -10,44 +12,130 @@
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
using namespace std::chrono;
|
||||
int nthreads = argc > 1 ? atoi(argv[1]) : 1;
|
||||
int nlines = argc > 2 ? atoi(argv[2]) : 1000000;
|
||||
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);
|
||||
|
||||
/*
|
||||
using namespace std::chrono;
|
||||
|
||||
|
||||
int nthreads = argc > 1 ? atoi(argv[1]) : 1;
|
||||
int nlines = argc > 2 ? atoi(argv[2]) : 100000;
|
||||
|
||||
|
||||
|
||||
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>(100);
|
||||
auto async = std::make_shared<c11log::sinks::async_sink>(1000);
|
||||
//auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*10 , 2);
|
||||
auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
|
||||
//auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
|
||||
|
||||
async->add_sink(fsink);
|
||||
//async->add_sink(null_sink);
|
||||
//async->add_sink(fsink);
|
||||
async->add_sink(null_sink);
|
||||
|
||||
c11log::logger logger("test");
|
||||
logger.add_sink(async);
|
||||
|
||||
//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;
|
||||
std::vector<std::thread*> threads;
|
||||
std::cout << "Starting " << nthreads << " threads x " << utils::format(nlines) << " lines each.." << std::endl;
|
||||
for (int i = 0; i < nthreads; i++)
|
||||
{
|
||||
auto t = new std::thread([&logger, nlines]() {
|
||||
|
||||
for(int i = 0 ; i < nlines; ++i)
|
||||
{
|
||||
logger.info() << "Hello from thread " << std::this_thread::get_id() << "\tcounter: " << i ;
|
||||
}
|
||||
|
||||
{
|
||||
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;
|
||||
}
|
||||
});
|
||||
threads.push_back(t);
|
||||
//std::this_thread::sleep_for(milliseconds(2));
|
||||
}
|
||||
|
||||
|
||||
auto stime = steady_clock::now();
|
||||
int thread_joined = 0;
|
||||
for(auto t:threads)
|
||||
{
|
||||
t->join();
|
||||
std::cout << "Joined " << ++thread_joined << " threads" << std::endl;
|
||||
}
|
||||
|
||||
auto delta = steady_clock::now() - stime;
|
||||
auto delta_seconds = duration_cast<milliseconds>(delta).count()/1000.0;
|
||||
|
||||
@@ -55,8 +143,7 @@ int main(int argc, char* argv[])
|
||||
std::cout << "Total: " << utils::format(total) << " = " << utils::format(total/delta_seconds) << "/sec" << std::endl;
|
||||
|
||||
async->shutdown(seconds(1));
|
||||
|
||||
return 0;
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user