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