| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Copyright(c) 2015 Gabi Melman.
 | 
					
						
							|  |  |  | // Distributed under the MIT License (http://opensource.org/licenses/MIT)
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // bench.cpp : spdlog benchmarks
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2018-10-12 03:04:55 +03:00
										 |  |  | #include "spdlog/spdlog.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | #include "spdlog/async.h"
 | 
					
						
							|  |  |  | #include "spdlog/sinks/basic_file_sink.h"
 | 
					
						
							|  |  |  | #include "spdlog/sinks/stdout_color_sinks.h"
 | 
					
						
							| 
									
										
										
										
											2018-10-12 03:04:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | #include "utils.h"
 | 
					
						
							|  |  |  | #include <atomic>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <memory>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <thread>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace std::chrono; | 
					
						
							|  |  |  | using namespace spdlog; | 
					
						
							|  |  |  | using namespace spdlog::sinks; | 
					
						
							|  |  |  | using namespace utils; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 18:13:30 +03:00
										 |  |  | #ifdef _MSC_VER
 | 
					
						
							| 
									
										
										
										
											2019-06-14 00:23:51 +03:00
										 |  |  | #pragma warning(push)
 | 
					
						
							|  |  |  | #pragma warning(disable : 4996) // disable fopen warning under msvc
 | 
					
						
							|  |  |  | #endif                          // _MSC_VER
 | 
					
						
							| 
									
										
										
										
											2019-06-06 18:13:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | int count_lines(const char *filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int counter = 0; | 
					
						
							|  |  |  |     auto *infile = fopen(filename, "r"); | 
					
						
							|  |  |  |     int ch; | 
					
						
							|  |  |  |     while (EOF != (ch = getc(infile))) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ('\n' == ch) | 
					
						
							|  |  |  |             counter++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-05 14:52:38 +03:00
										 |  |  |     fclose(infile); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return counter; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-06 19:23:15 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 18:13:30 +03:00
										 |  |  | #ifdef _MSC_VER
 | 
					
						
							| 
									
										
										
										
											2019-06-14 00:23:51 +03:00
										 |  |  | #pragma warning(pop)
 | 
					
						
							| 
									
										
										
										
											2019-06-06 18:13:30 +03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | int main(int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int howmany = 1000000; | 
					
						
							| 
									
										
										
										
											2019-06-01 13:44:21 +03:00
										 |  |  |     int queue_size = std::min(howmany + 2, 8192); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |     int threads = 10; | 
					
						
							|  |  |  |     int iters = 3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-06-06 19:23:15 +03:00
										 |  |  |         spdlog::set_pattern("[%^%l%$] %v"); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |         if (argc == 1) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-10-16 19:31:25 +03:00
										 |  |  |             spdlog::info("Usage: {} <message_count> <threads> <q_size> <iterations>", argv[0]); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (argc > 1) | 
					
						
							|  |  |  |             howmany = atoi(argv[1]); | 
					
						
							|  |  |  |         if (argc > 2) | 
					
						
							|  |  |  |             threads = atoi(argv[2]); | 
					
						
							|  |  |  |         if (argc > 3) | 
					
						
							| 
									
										
										
										
											2019-06-01 13:41:39 +03:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |             queue_size = atoi(argv[3]); | 
					
						
							| 
									
										
										
										
											2019-06-04 00:09:16 +03:00
										 |  |  |             if (queue_size > 500000) | 
					
						
							| 
									
										
										
										
											2019-06-01 13:41:39 +03:00
										 |  |  |             { | 
					
						
							|  |  |  |                 spdlog::error("Max queue size allowed: 500,000"); | 
					
						
							|  |  |  |                 exit(1); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (argc > 4) | 
					
						
							|  |  |  |             iters = atoi(argv[4]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-01 13:41:39 +03:00
										 |  |  |         auto slot_size = sizeof(spdlog::details::async_msg); | 
					
						
							| 
									
										
										
										
											2018-10-16 19:31:25 +03:00
										 |  |  |         spdlog::info("-------------------------------------------------"); | 
					
						
							| 
									
										
										
										
											2019-06-01 13:41:39 +03:00
										 |  |  |         spdlog::info("Messages     : {:n}", howmany); | 
					
						
							|  |  |  |         spdlog::info("Threads      : {:n}", threads); | 
					
						
							|  |  |  |         spdlog::info("Queue        : {:n} slots", queue_size); | 
					
						
							| 
									
										
										
										
											2019-06-04 00:09:16 +03:00
										 |  |  |         spdlog::info("Queue memory : {:n} x {} = {:n} KB ", queue_size, slot_size, (queue_size * slot_size) / 1024); | 
					
						
							| 
									
										
										
										
											2019-06-01 13:41:39 +03:00
										 |  |  |         spdlog::info("Total iters  : {:n}", iters); | 
					
						
							| 
									
										
										
										
											2018-10-16 19:31:25 +03:00
										 |  |  |         spdlog::info("-------------------------------------------------"); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const char *filename = "logs/basic_async.log"; | 
					
						
							| 
									
										
										
										
											2019-06-06 19:23:15 +03:00
										 |  |  |         spdlog::info(""); | 
					
						
							|  |  |  |         spdlog::info("*********************************"); | 
					
						
							|  |  |  |         spdlog::info("Queue Overflow Policy: block"); | 
					
						
							|  |  |  |         spdlog::info("*********************************"); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |         for (int i = 0; i < iters; i++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             auto tp = std::make_shared<details::thread_pool>(queue_size, 1); | 
					
						
							|  |  |  |             auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true); | 
					
						
							|  |  |  |             auto logger = std::make_shared<async_logger>("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::block); | 
					
						
							|  |  |  |             bench_mt(howmany, std::move(logger), threads); | 
					
						
							| 
									
										
										
										
											2019-06-06 19:23:15 +03:00
										 |  |  | #ifdef SPDLOG_ASYNC_BENCH_VERIFY
 | 
					
						
							|  |  |  |             verify_file(filename, howmany); | 
					
						
							|  |  |  | #endif // SPDLOG_ASYNC_BENCH_VERIFY
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 19:23:15 +03:00
										 |  |  |         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<details::thread_pool>(queue_size, 1); | 
					
						
							|  |  |  |             auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true); | 
					
						
							| 
									
										
										
										
											2019-06-14 00:23:51 +03:00
										 |  |  |             auto logger = | 
					
						
							|  |  |  |                 std::make_shared<async_logger>("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::overrun_oldest); | 
					
						
							| 
									
										
										
										
											2019-06-06 19:23:15 +03:00
										 |  |  |             bench_mt(howmany, std::move(logger), threads); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-10-30 01:08:32 +02:00
										 |  |  |         spdlog::shutdown(); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (std::exception &ex) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         std::cerr << "Error: " << ex.what() << std::endl; | 
					
						
							|  |  |  |         perror("Last error"); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-30 01:08:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 01:25:40 +03:00
										 |  |  | void thread_fun(std::shared_ptr<spdlog::logger> logger, int howmany) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (int i = 0; i < howmany; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         logger->info("Hello logger: msg number {}", i); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_count) | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     using std::chrono::high_resolution_clock; | 
					
						
							|  |  |  |     vector<thread> threads; | 
					
						
							|  |  |  |     auto start = high_resolution_clock::now(); | 
					
						
							| 
									
										
										
										
											2018-08-05 01:25:40 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int msgs_per_thread = howmany / thread_count; | 
					
						
							|  |  |  |     int msgs_per_thread_mod = howmany % thread_count; | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |     for (int t = 0; t < thread_count; ++t) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-08-05 01:25:40 +03:00
										 |  |  |         if (t == 0 && msgs_per_thread_mod) | 
					
						
							|  |  |  |             threads.push_back(std::thread(thread_fun, logger, msgs_per_thread + msgs_per_thread_mod)); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             threads.push_back(std::thread(thread_fun, logger, msgs_per_thread)); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (auto &t : threads) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         t.join(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto delta = high_resolution_clock::now() - start; | 
					
						
							|  |  |  |     auto delta_d = duration_cast<duration<double>>(delta).count(); | 
					
						
							| 
									
										
										
										
											2018-10-16 19:31:25 +03:00
										 |  |  |     spdlog::info("Elapsed: {} secs\t {:n}/sec", delta_d, int(howmany / delta_d)); | 
					
						
							| 
									
										
										
										
											2018-08-05 00:14:01 +03:00
										 |  |  | } |