diff --git a/bench/async_bench.cpp b/bench/async_bench.cpp index db066f70..eaa3a9ee 100644 --- a/bench/async_bench.cpp +++ b/bench/async_bench.cpp @@ -26,6 +26,7 @@ using namespace utils; void bench_mt(int howmany, std::shared_ptr log, int thread_count); + #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable : 4996) //disable fopen warning under msvc @@ -45,11 +46,22 @@ int count_lines(const char *filename) return counter; } + +void verify_file(const char *filename, int expected_count) +{ + auto count = count_lines(filename); + if (count != expected_count) + { + spdlog::error("Test failed. {} has {:n} lines instead of {:n}", filename, count, expected_count); + exit(1); + } + spdlog::info("Line count OK ({:n})\n", count); +} + #ifdef _MSC_VER #pragma warning(pop) #endif - int main(int argc, char *argv[]) { @@ -60,9 +72,9 @@ int main(int argc, char *argv[]) try { + spdlog::set_pattern("[%^%l%$] %v"); if (argc == 1) { - spdlog::set_pattern("%v"); spdlog::info("Usage: {} ", argv[0]); return 0; } @@ -94,24 +106,32 @@ int main(int argc, char *argv[]) spdlog::info("-------------------------------------------------"); const char *filename = "logs/basic_async.log"; - + spdlog::info(""); + spdlog::info("*********************************"); + spdlog::info("Queue Overflow Policy: block"); + spdlog::info("*********************************"); for (int i = 0; i < iters; i++) { auto tp = std::make_shared(queue_size, 1); auto file_sink = std::make_shared(filename, true); auto logger = std::make_shared("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::block); bench_mt(howmany, std::move(logger), threads); - auto count = count_lines(filename); +#ifdef SPDLOG_ASYNC_BENCH_VERIFY + verify_file(filename, howmany); +#endif // SPDLOG_ASYNC_BENCH_VERIFY + } - if (count != howmany) - { - spdlog::error("Test failed. {} has {:n} lines instead of {:n}", filename, count, howmany); - exit(1); - } - else - { - spdlog::info("Line count OK ({:n})\n", count); - } + spdlog::info(""); + spdlog::info("*********************************"); + spdlog::info("Queue Overflow Policy: overrun"); + spdlog::info("*********************************"); + // do same test but discard oldest if queue is full instead of blocking + for (int i = 0; i < iters; i++) + { + auto tp = std::make_shared(queue_size, 1); + auto file_sink = std::make_shared(filename, true); + auto logger = std::make_shared("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::overrun_oldest); + bench_mt(howmany, std::move(logger), threads); } spdlog::shutdown(); } diff --git a/bench/bench.cpp b/bench/bench.cpp index d21dcdc1..5435e985 100644 --- a/bench/bench.cpp +++ b/bench/bench.cpp @@ -7,7 +7,6 @@ // bench.cpp : spdlog benchmarks // #include "spdlog/spdlog.h" -#include "spdlog/async.h" #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/null_sink.h" @@ -95,22 +94,6 @@ int main(int argc, char *argv[]) auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt.log"); bench_mt(howmany, std::move(daily_mt), threads); bench_mt(howmany, spdlog::create("null_mt"), threads); - - int iters = 3; - spdlog::info("**************************************************************"); - spdlog::info("Asyncronous bench {:n} threads sharing same logger", threads); - spdlog::info("Messages: {:n}", howmany); - spdlog::info("Queue size: {:n} slots", queue_size); - auto slot_size = sizeof(spdlog::details::async_msg); - spdlog::info("Total queue memory: {}x{} bytes per slot = {:n} Kb ", queue_size, slot_size, (queue_size * slot_size) / 1024); - spdlog::info("**************************************************************"); - - for (int i = 0; i < iters; ++i) - { - spdlog::init_thread_pool(static_cast(queue_size), 1); - auto as = spdlog::basic_logger_mt("async", "logs/basic_async.log", true); - bench_mt(howmany, std::move(as), threads); - } } catch (std::exception &ex) { diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index f3cdb6ab..8f2bfa0a 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -64,7 +64,7 @@ SPDLOG_INLINE const std::string &logger::name() const return name_; } -// set formatting for the sinks in this logger. redundant +// set formatting for the sinks in this logger. // each sink will get a seperate instance of the formatter object. SPDLOG_INLINE void logger::set_formatter(std::unique_ptr f) {