mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-28 00:10:21 +08:00
parent
edec38181c
commit
7c328c9e26
@ -16,25 +16,27 @@
|
|||||||
|
|
||||||
using namespace spdlog::sinks;
|
using namespace spdlog::sinks;
|
||||||
|
|
||||||
void worker_thread(spdlog::logger &logger, int n_messages) {
|
void worker_thread(std::shared_ptr<spdlog::logger> logger, int n_messages) {
|
||||||
for (int i = 0; i < n_messages; ++i) {
|
for (int i = 0; i < n_messages; ++i) {
|
||||||
// std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
// std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
logger.info("Worker thread message #{}", i);
|
logger->info("Worker thread message #{}", i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#include <iostream>
|
|
||||||
bool async_example2(size_t n_threads, size_t n_messages) {
|
bool async_example2(size_t n_threads, size_t n_messages) {
|
||||||
auto test_sink = std::make_shared<test_sink_st>();
|
auto test_sink = std::make_shared<test_sink_st>();
|
||||||
auto n_total_messages = n_messages * n_threads;
|
auto n_total_messages = n_messages * n_threads;
|
||||||
{
|
{
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> threads;
|
||||||
auto async_sink = std::make_shared<async_sink_mt>(8192);
|
auto async_sink = std::make_shared<async_sink_mt>();
|
||||||
async_sink->add_sink(test_sink);
|
async_sink->add_sink(test_sink);
|
||||||
//auto logger = std::make_shared<spdlog::logger>("async_logger", async_sink);
|
auto logger = std::make_shared<spdlog::logger>("async_logger", async_sink);
|
||||||
spdlog::logger logger("async_logger", async_sink);
|
logger->set_level(spdlog::level::trace);
|
||||||
|
|
||||||
spdlog::stopwatch sw;
|
spdlog::stopwatch sw;
|
||||||
|
|
||||||
for (int i = 0; i < n_threads; ++i) {
|
for (int i = 0; i < n_threads; ++i) {
|
||||||
threads.emplace_back(worker_thread, std::ref(logger), n_messages);
|
threads.emplace_back(worker_thread, logger, n_messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for worker threads to finish
|
// wait for worker threads to finish
|
||||||
@ -47,7 +49,7 @@ bool async_example2(size_t n_threads, size_t n_messages) {
|
|||||||
if (millis == 0) {
|
if (millis == 0) {
|
||||||
millis = 1;
|
millis = 1;
|
||||||
}
|
}
|
||||||
spdlog::info("+++ {:L}/sec +++ ({} ms)", n_total_messages * 1000 / millis, millis);
|
spdlog::info("Elapsed {} millis ({:L}/sec)", millis, n_total_messages * 1000 / millis);
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for the worker thread to finish
|
// wait for the worker thread to finish
|
||||||
@ -64,17 +66,17 @@ bool async_example2(size_t n_threads, size_t n_messages) {
|
|||||||
#include <random>
|
#include <random>
|
||||||
int main(int, char *[]) {
|
int main(int, char *[]) {
|
||||||
std::locale::global(std::locale("en_US.UTF-8"));
|
std::locale::global(std::locale("en_US.UTF-8"));
|
||||||
|
// constexpr size_t n_messages = 1'00;
|
||||||
|
// constexpr size_t n_threads = 1;
|
||||||
try {
|
try {
|
||||||
// random n_messages and n_threads
|
// random n_messages and n_threads
|
||||||
std::mt19937 gen(std::random_device{}());
|
std::mt19937 gen(std::random_device{}());
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
// auto n_threads = std::uniform_int_distribution<size_t>(1, 10)(gen);
|
auto n_threads = std::uniform_int_distribution<size_t>(1, 10)(gen);
|
||||||
// auto n_messages = std::uniform_int_distribution<size_t>(1, 1'000'000)(gen);
|
auto n_messages = std::uniform_int_distribution<size_t>(1, 1'000'000)(gen);
|
||||||
auto n_threads = 4;
|
|
||||||
auto n_messages = 1'000'000 / n_threads;
|
|
||||||
|
|
||||||
spdlog::info("***********************************");
|
spdlog::info("***********************************");
|
||||||
spdlog::info("Test #{}. {} x {:L} = {:L} msgs", i, n_threads, n_messages, n_threads * n_messages);
|
spdlog::info("Test #{}. Threads: {}, Count: {:L}", i, n_threads, n_messages);
|
||||||
if (!async_example2(n_threads, n_messages)) {
|
if (!async_example2(n_threads, n_messages)) {
|
||||||
spdlog::error("Stopped");
|
spdlog::error("Stopped");
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user