mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-13 01:10:26 +08:00
83c9ede9e6
Some checks failed
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[asan:ON build_type:Debug … (push) Has been cancelled
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Debug compiler… (push) Has been cancelled
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Release compil… (push) Has been cancelled
macos / macOS Clang (C++17, Release) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:ON BUILD_SHARED:OFF BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Has been cancelled
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019]) (push) Has been cancelled
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019]) (push) Has been cancelled
Replace async logger with async sink
161 lines
5.8 KiB
C++
161 lines
5.8 KiB
C++
//
|
|
// Copyright(c) 2015 Gabi Melman.
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
//
|
|
|
|
//
|
|
// bench.cpp : spdlog benchmarks
|
|
//
|
|
#include <atomic>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <locale>
|
|
#include <algorithm>
|
|
|
|
#include "spdlog/sinks/async_sink.h"
|
|
#include "spdlog/sinks/basic_file_sink.h"
|
|
#include "spdlog/spdlog.h"
|
|
|
|
using namespace std;
|
|
using namespace std::chrono;
|
|
using namespace spdlog;
|
|
using namespace spdlog::sinks;
|
|
|
|
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
|
|
|
|
int count_lines(const char *filename) {
|
|
std::ifstream ifs(filename);
|
|
return std::count(std::istreambuf_iterator(ifs), std::istreambuf_iterator<char>(), '\n');
|
|
}
|
|
|
|
void verify_file(const char *filename, int expected_count) {
|
|
spdlog::info("Verifying {} to contain {} line..", filename, expected_count);
|
|
auto count = count_lines(filename);
|
|
if (count != expected_count) {
|
|
spdlog::error("Test failed. {} has {} lines instead of {}", filename, count, expected_count);
|
|
exit(1);
|
|
}
|
|
spdlog::info("Line count OK ({})\n", count);
|
|
}
|
|
|
|
#ifdef _MSC_VER
|
|
#pragma warning(pop)
|
|
#endif
|
|
|
|
using namespace spdlog::sinks;
|
|
|
|
int main(int argc, char *argv[]) {
|
|
// setlocale to show thousands separators
|
|
std::locale::global(std::locale("en_US.UTF-8"));
|
|
int howmany = 1000000;
|
|
int queue_size = std::min(howmany + 2, 8192);
|
|
int threads = 10;
|
|
int iters = 3;
|
|
|
|
try {
|
|
spdlog::set_pattern("[%^%l%$] %v");
|
|
if (argc > 1 && (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help")) {
|
|
spdlog::info("Usage: {} <message_count> <threads> <q_size> <iterations>", argv[0]);
|
|
return 0;
|
|
}
|
|
|
|
if (argc > 1) howmany = atoi(argv[1]);
|
|
if (argc > 2) threads = atoi(argv[2]);
|
|
if (argc > 3) {
|
|
queue_size = atoi(argv[3]);
|
|
if (queue_size > 500000) {
|
|
spdlog::error("Max queue size allowed: 500,000");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
if (argc > 4) iters = atoi(argv[4]);
|
|
// validate all argc values
|
|
if (howmany < 1 || threads < 1 || queue_size < 1 || iters < 1) {
|
|
spdlog::error("Invalid input values");
|
|
exit(1);
|
|
}
|
|
|
|
auto slot_size = sizeof(details::async_log_msg);
|
|
spdlog::info("-------------------------------------------------");
|
|
spdlog::info("Messages : {:L}", howmany);
|
|
spdlog::info("Threads : {:L}", threads);
|
|
spdlog::info("Queue : {:L} slots", queue_size);
|
|
spdlog::info("Queue memory : {:L} x {:L} = {:L} KB ", queue_size, slot_size, (queue_size * slot_size) / 1024);
|
|
spdlog::info("Total iters : {:L}", iters);
|
|
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 file_sink = std::make_shared<basic_file_sink_mt>(filename, true);
|
|
auto cfg = async_sink::config();
|
|
cfg.queue_size = queue_size;
|
|
cfg.sinks.push_back(std::move(file_sink));
|
|
auto async_sink = std::make_shared<sinks::async_sink>(cfg);
|
|
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
|
|
bench_mt(howmany, std::move(logger), threads);
|
|
}
|
|
//verify_file(filename, howmany); // in separate scope to ensure logger is destroyed and all logs were written
|
|
}
|
|
spdlog::info("");
|
|
spdlog::info("*********************************");
|
|
spdlog::info("Queue Overflow Policy: overrun");
|
|
spdlog::info("*********************************");
|
|
// do same test but discard the oldest if queue is full instead of blocking
|
|
filename = "logs/basic_async-overrun.log";
|
|
for (int i = 0; i < iters; i++) {
|
|
async_sink::config cfg;
|
|
cfg.policy = async_sink::overflow_policy::overrun_oldest;
|
|
cfg.queue_size = queue_size;
|
|
auto file_sink = std::make_shared<basic_file_sink_mt>(filename, true);
|
|
cfg.sinks.push_back(std::move(file_sink));
|
|
auto async_sink = std::make_shared<sinks::async_sink>(cfg);
|
|
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
|
|
bench_mt(howmany, std::move(logger), threads);
|
|
}
|
|
spdlog::shutdown();
|
|
} catch (std::exception &ex) {
|
|
std::cerr << "Error: " << ex.what() << std::endl;
|
|
perror("Last error");
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
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) {
|
|
using std::chrono::high_resolution_clock;
|
|
vector<std::thread> threads;
|
|
auto start = high_resolution_clock::now();
|
|
|
|
int msgs_per_thread = howmany / thread_count;
|
|
int msgs_per_thread_mod = howmany % thread_count;
|
|
for (int t = 0; t < thread_count; ++t) {
|
|
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));
|
|
}
|
|
|
|
for (auto &t : threads) {
|
|
t.join();
|
|
}
|
|
|
|
auto delta = high_resolution_clock::now() - start;
|
|
auto delta_d = duration_cast<duration<double>>(delta).count();
|
|
spdlog::info("Elapsed: {} secs\t {:L}/sec", delta_d, static_cast<int>(howmany / delta_d));
|
|
}
|