From 470c23ad92e13c9998dac45e0c0ec089632aca13 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 27 Jan 2014 19:35:18 +0200 Subject: [PATCH] fixes and imprvts --- build/gcc/makefile | 4 +++- src/formatters.cpp | 16 ++++++++++------ src/test.cpp | 41 ++++++++++++++++++++--------------------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/build/gcc/makefile b/build/gcc/makefile index 1cd01236..a58236d2 100644 --- a/build/gcc/makefile +++ b/build/gcc/makefile @@ -17,7 +17,7 @@ OUTLIB_DEBUG = libc11log-debug.a TEST_RELEASE = testme TEST_DEBUG = testme-debug -.PHONY: all mkdirs release debug build clean +.PHONY: all mkdirs release debug build clean rebuild all: release @@ -49,4 +49,6 @@ debug/%.o: $(SRC_DIR)/%.cpp clean: rm -rf release debug daily.* $(TEST_RELEASE) $(TEST_DEBUG) $(OUTLIB_RELEASE) $(OUTLIB_DEBUG) +rebuild: clean all + diff --git a/src/formatters.cpp b/src/formatters.cpp index 81900673..2a27d908 100644 --- a/src/formatters.cpp +++ b/src/formatters.cpp @@ -4,16 +4,20 @@ void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, std::ostream &dest) { + std::tm tm = details::os::localtime(std::chrono::system_clock::to_time_t(tp)); //get ms - auto duration = tp.time_since_epoch(); - int millis = static_cast(std::chrono::duration_cast(duration).count() % 1000); - //std::put_time(&tm, "[ %Y-%m-%d %H:%M:%S ]") - seems too slow + //auto duration = tp.time_since_epoch(); + //int millis = static_cast(std::chrono::duration_cast(duration).count() % 1000); + + char buf[64]; - auto size = sprintf(buf, "[%d-%02d-%02d %02d:%02d:%02d.%03d]", + auto size = sprintf(buf, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec, millis); - dest.write(buf, size); + tm.tm_hour, tm.tm_min, tm.tm_sec); + + dest.write(buf, size); + } void c11log::formatters::format_time(std::ostream& dest) diff --git a/src/test.cpp b/src/test.cpp index 2e640c7c..4a16f557 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -13,49 +13,48 @@ 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; + auto null_sink = std::make_shared(); auto stdout_sink = std::make_shared(); - auto async = std::make_shared(1000); + auto async = std::make_shared(100); //auto fsink = std::make_shared("newlog", "txt", 1024*1024*10 , 2); auto fsink = std::make_shared("daily", "txt"); async->add_sink(fsink); + //async->add_sink(null_sink); c11log::logger logger("test"); logger.add_sink(async); - - std::atomic counter { 0 }; - auto counter_ptr = &counter; - std::atomic active{true}; - auto active_ptr = &active; - + std::vector threads; - std::cout << "Starting " << nthreads << " threads for 3 seconds.." << std::endl; + 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, counter_ptr, active_ptr]() { - while (*active_ptr) + 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: " << counter_ptr->load(); - (*counter_ptr)++; + logger.info() << "Hello from thread " << std::this_thread::get_id() << "\tcounter: " << i ; } }); threads.push_back(t); } - int seconds = 0; - while (seconds++ < 3) - { - counter = 0; - std::this_thread::sleep_for(std::chrono::seconds(1)); - std::cout << "Counter = " << utils::format(counter.load()) << std::endl; - } - active = false; + + auto stime = steady_clock::now(); for(auto t:threads) t->join(); - async->shutdown(std::chrono::seconds(1)); + auto delta = steady_clock::now() - stime; + auto delta_seconds = duration_cast(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)); return 0; }