spdlog
Loading...
Searching...
No Matches
async_bench.cpp
Go to the documentation of this file.
1//
2// Copyright(c) 2015 Gabi Melman.
3// Distributed under the MIT License (http://opensource.org/licenses/MIT)
4//
5
6//
7// bench.cpp : spdlog benchmarks
8//
9#include "spdlog/spdlog.h"
10#include "spdlog/async.h"
12
13#ifdef SPDLOG_FMT_EXTERNAL
14# include <fmt/format.h>
15#else
17#endif
18
19#include "utils.h"
20#include <atomic>
21#include <iostream>
22#include <memory>
23#include <string>
24#include <thread>
25
26using namespace std;
27using namespace std::chrono;
28using namespace spdlog;
29using namespace spdlog::sinks;
30using namespace utils;
31
32void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
33
34#ifdef _MSC_VER
35# pragma warning(push)
36# pragma warning(disable : 4996) // disable fopen warning under msvc
37#endif // _MSC_VER
38
39int count_lines(const char *filename)
40{
41 int counter = 0;
42 auto *infile = fopen(filename, "r");
43 int ch;
44 while (EOF != (ch = getc(infile)))
45 {
46 if ('\n' == ch)
47 counter++;
48 }
49 fclose(infile);
50
51 return counter;
52}
53
54void verify_file(const char *filename, int expected_count)
55{
56 spdlog::info("Verifying {} to contain {} line..", filename, expected_count);
57 auto count = count_lines(filename);
58 if (count != expected_count)
59 {
60 spdlog::error("Test failed. {} has {} lines instead of {}", filename, count, expected_count);
61 exit(1);
62 }
63 spdlog::info("Line count OK ({})\n", count);
64}
65
66#ifdef _MSC_VER
67# pragma warning(pop)
68#endif
69
70int main(int argc, char *argv[])
71{
72
73 int howmany = 1000000;
74 int queue_size = std::min(howmany + 2, 8192);
75 int threads = 10;
76 int iters = 3;
77
78 try
79 {
80 spdlog::set_pattern("[%^%l%$] %v");
81 if (argc == 1)
82 {
83 spdlog::info("Usage: {} <message_count> <threads> <q_size> <iterations>", argv[0]);
84 return 0;
85 }
86
87 if (argc > 1)
88 howmany = atoi(argv[1]);
89 if (argc > 2)
90 threads = atoi(argv[2]);
91 if (argc > 3)
92 {
93 queue_size = atoi(argv[3]);
94 if (queue_size > 500000)
95 {
96 spdlog::error("Max queue size allowed: 500,000");
97 exit(1);
98 }
99 }
100
101 if (argc > 4)
102 iters = atoi(argv[4]);
103
104 auto slot_size = sizeof(spdlog::details::async_msg);
105 spdlog::info("-------------------------------------------------");
106 spdlog::info("Messages : {:L}", howmany);
107 spdlog::info("Threads : {:L}", threads);
108 spdlog::info("Queue : {:L} slots", queue_size);
109 spdlog::info("Queue memory : {:L} x {:L} = {:L} KB ", queue_size, slot_size, (queue_size * slot_size) / 1024);
110 spdlog::info("Total iters : {:L}", iters);
111 spdlog::info("-------------------------------------------------");
112
113 const char *filename = "logs/basic_async.log";
114 spdlog::info("");
115 spdlog::info("*********************************");
116 spdlog::info("Queue Overflow Policy: block");
117 spdlog::info("*********************************");
118 for (int i = 0; i < iters; i++)
119 {
120 auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
122 auto logger = std::make_shared<async_logger>("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::block);
123 bench_mt(howmany, std::move(logger), threads);
124 // verify_file(filename, howmany);
125 }
126
127 spdlog::info("");
128 spdlog::info("*********************************");
129 spdlog::info("Queue Overflow Policy: overrun");
130 spdlog::info("*********************************");
131 // do same test but discard oldest if queue is full instead of blocking
132 filename = "logs/basic_async-overrun.log";
133 for (int i = 0; i < iters; i++)
134 {
135 auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
137 auto logger =
138 std::make_shared<async_logger>("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::overrun_oldest);
139 bench_mt(howmany, std::move(logger), threads);
140 }
142 }
143 catch (std::exception &ex)
144 {
145 std::cerr << "Error: " << ex.what() << std::endl;
146 perror("Last error");
147 return 1;
148 }
149 return 0;
150}
151
153{
154 for (int i = 0; i < howmany; i++)
155 {
156 logger->info("Hello logger: msg number {}", i);
157 }
158}
159
161{
164 auto start = high_resolution_clock::now();
165
168 for (int t = 0; t < thread_count; ++t)
169 {
170 if (t == 0 && msgs_per_thread_mod)
172 else
174 }
175
176 for (auto &t : threads)
177 {
178 t.join();
179 };
180
181 auto delta = high_resolution_clock::now() - start;
183 spdlog::info("Elapsed: {} secs\t {:L}/sec", delta_d, int(howmany / delta_d));
184}
int main(int argc, char *argv[])
void thread_fun(std::shared_ptr< spdlog::logger > logger, int howmany)
int count_lines(const char *filename)
void bench_mt(int howmany, std::shared_ptr< spdlog::logger > log, int thread_count)
void verify_file(const char *filename, int expected_count)
T atoi(T... args)
void info(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:156
constexpr auto count() -> size_t
Definition core.h:1039
T endl(T... args)
T exit(T... args)
T fclose(T... args)
T getc(T... args)
T fopen(T... args)
T log(T... args)
T make_shared(T... args)
T min(T... args)
Definition async.h:25
void error(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:167
SPDLOG_INLINE void shutdown()
Definition spdlog-inl.h:100
void info(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:155
SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type)
Definition spdlog-inl.h:30
T perror(T... args)