make changes to examples,bench and test to use spdlog::chrono and spdlog::mutex.

pull/793/head
Gabor Janak 7 years ago
parent 440e0302f5
commit 566a92a183

@ -113,7 +113,11 @@ void thread_fun(std::shared_ptr<spdlog::logger> logger, int howmany)
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_count) void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_count)
{ {
using std::chrono::high_resolution_clock; using spdlog::chrono::high_resolution_clock;
using spdlog::chrono::duration_cast;
using spdlog::chrono::duration;
using spdlog::details::thread;
vector<thread> threads; vector<thread> threads;
auto start = high_resolution_clock::now(); auto start = high_resolution_clock::now();
@ -122,9 +126,9 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_co
for (int t = 0; t < thread_count; ++t) for (int t = 0; t < thread_count; ++t)
{ {
if (t == 0 && msgs_per_thread_mod) if (t == 0 && msgs_per_thread_mod)
threads.push_back(std::thread(thread_fun, logger, msgs_per_thread + msgs_per_thread_mod)); threads.push_back(thread(thread_fun, logger, msgs_per_thread + msgs_per_thread_mod));
else else
threads.push_back(std::thread(thread_fun, logger, msgs_per_thread)); threads.push_back(thread(thread_fun, logger, msgs_per_thread));
} }
for (auto &t : threads) for (auto &t : threads)

@ -18,12 +18,10 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
#include <thread>
using namespace std;
using namespace std::chrono;
using namespace spdlog; using namespace spdlog;
using namespace spdlog::sinks; using namespace spdlog::sinks;
using namespace std;
using namespace utils; using namespace utils;
void bench(int howmany, std::shared_ptr<spdlog::logger> log); void bench(int howmany, std::shared_ptr<spdlog::logger> log);
@ -106,7 +104,7 @@ int main(int argc, char *argv[])
void bench(int howmany, std::shared_ptr<spdlog::logger> log) void bench(int howmany, std::shared_ptr<spdlog::logger> log)
{ {
using std::chrono::high_resolution_clock; using chrono::high_resolution_clock;
cout << log->name() << "...\t\t" << flush; cout << log->name() << "...\t\t" << flush;
auto start = high_resolution_clock::now(); auto start = high_resolution_clock::now();
for (auto i = 0; i < howmany; ++i) for (auto i = 0; i < howmany; ++i)
@ -115,7 +113,7 @@ void bench(int howmany, std::shared_ptr<spdlog::logger> log)
} }
auto delta = high_resolution_clock::now() - start; auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count(); auto delta_d = spdlog::chrono::duration_cast<spdlog::chrono::duration<double>>(delta).count();
cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl; cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl;
spdlog::drop(log->name()); spdlog::drop(log->name());
@ -123,13 +121,13 @@ void bench(int howmany, std::shared_ptr<spdlog::logger> log)
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count) void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{ {
using std::chrono::high_resolution_clock; using spdlog::chrono::high_resolution_clock;
cout << log->name() << "...\t\t" << flush; cout << log->name() << "...\t\t" << flush;
vector<thread> threads; vector<spdlog::details::thread> threads;
auto start = high_resolution_clock::now(); auto start = high_resolution_clock::now();
for (int t = 0; t < thread_count; ++t) for (int t = 0; t < thread_count; ++t)
{ {
threads.push_back(std::thread([&]() { threads.push_back(spdlog::details::thread([&]() {
for (int j = 0; j < howmany / thread_count; j++) for (int j = 0; j < howmany / thread_count; j++)
{ {
log->info("Hello logger: msg number {}", j); log->info("Hello logger: msg number {}", j);
@ -143,6 +141,6 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count
}; };
auto delta = high_resolution_clock::now() - start; auto delta = high_resolution_clock::now() - start;
auto delta_d = duration_cast<duration<double>>(delta).count(); auto delta_d = spdlog::chrono::duration_cast<spdlog::chrono::duration<double>>(delta).count();
cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl; cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl;
} }

@ -18,10 +18,8 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
#include <thread>
using namespace std; using namespace std;
using namespace std::chrono;
using namespace spdlog; using namespace spdlog;
using namespace spdlog::sinks; using namespace spdlog::sinks;
using namespace utils; using namespace utils;
@ -99,10 +97,9 @@ int main(int, char *[])
void bench(int howmany, std::shared_ptr<spdlog::logger> log) void bench(int howmany, std::shared_ptr<spdlog::logger> log)
{ {
using namespace std::chrono; using spdlog::chrono::high_resolution_clock;
using chrono::high_resolution_clock; using spdlog::chrono::milliseconds;
using chrono::milliseconds; using spdlog::chrono::nanoseconds;
using chrono::nanoseconds;
cout << log->name() << "...\t\t" << flush; cout << log->name() << "...\t\t" << flush;
nanoseconds total_nanos = nanoseconds::zero(); nanoseconds total_nanos = nanoseconds::zero();
@ -120,22 +117,24 @@ void bench(int howmany, std::shared_ptr<spdlog::logger> log)
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count) void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{ {
using namespace std::chrono;
using chrono::high_resolution_clock; using spdlog::chrono::high_resolution_clock;
using chrono::milliseconds; using spdlog::chrono::milliseconds;
using chrono::nanoseconds; using spdlog::chrono::nanoseconds;
using spdlog::chrono::duration_cast;
using spdlog::details::thread;
cout << log->name() << "...\t\t" << flush; cout << log->name() << "...\t\t" << flush;
vector<thread> threads; vector<thread> threads;
std::atomic<nanoseconds::rep> total_nanos{0}; std::atomic<nanoseconds::rep> total_nanos{0};
for (int t = 0; t < thread_count; ++t) for (int t = 0; t < thread_count; ++t)
{ {
threads.push_back(std::thread([&]() { threads.push_back(thread([&]() {
for (int j = 0; j < howmany / thread_count; j++) for (int j = 0; j < howmany / thread_count; j++)
{ {
auto start = high_resolution_clock::now(); auto start = high_resolution_clock::now();
log->info("Hello logger: msg number {}", j); log->info("Hello logger: msg number {}", j);
auto delta_nanos = chrono::duration_cast<nanoseconds>(high_resolution_clock::now() - start); auto delta_nanos = duration_cast<nanoseconds>(high_resolution_clock::now() - start);
total_nanos += delta_nanos.count(); total_nanos += delta_nanos.count();
} }
})); }));

@ -48,7 +48,7 @@ int main(int, char *[])
// flush all *registered* loggers using a worker thread every 3 seconds. // flush all *registered* loggers using a worker thread every 3 seconds.
// note: registered loggers *must* be thread safe for this to work correctly! // note: registered loggers *must* be thread safe for this to work correctly!
spdlog::flush_every(std::chrono::seconds(3)); spdlog::flush_every(spdlog::chrono::seconds(3));
// apply some function on all registered loggers // apply some function on all registered loggers
spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); }); spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); });

@ -360,7 +360,11 @@ inline size_t thread_id()
inline void sleep_for_millis(int milliseconds) inline void sleep_for_millis(int milliseconds)
{ {
#if defined(_WIN32) #if defined(_WIN32)
# ifdef SPDLOG_USE_BOOST_THREAD
boost::this_thread::sleep_for(chrono::milliseconds(milliseconds));
# else
::Sleep(milliseconds); ::Sleep(milliseconds);
# endif
#else #else
std::this_thread::sleep_for(chrono::milliseconds(milliseconds)); std::this_thread::sleep_for(chrono::milliseconds(milliseconds));
#endif #endif

@ -5,7 +5,7 @@
#include <iostream> #include <iostream>
class failing_sink : public spdlog::sinks::base_sink<std::mutex> class failing_sink : public spdlog::sinks::base_sink<spdlog::mutex>
{ {
public: public:
failing_sink() = default; failing_sink() = default;

@ -81,7 +81,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]") TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
{ {
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>; using sink_type = spdlog::sinks::daily_file_sink<spdlog::mutex, spdlog::sinks::daily_filename_calculator>;
prepare_logdir(); prepare_logdir();
// calculate filename (time based) // calculate filename (time based)
@ -113,7 +113,7 @@ struct custom_daily_file_name_calculator
TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]") TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
{ {
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>; using sink_type = spdlog::sinks::daily_file_sink<spdlog::mutex, custom_daily_file_name_calculator>;
prepare_logdir(); prepare_logdir();
// calculate filename (time based) // calculate filename (time based)

@ -84,10 +84,10 @@ TEST_CASE("async periodic flush", "[async]")
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]); auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
spdlog::flush_every(std::chrono::seconds(1)); spdlog::flush_every(chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(1100)); spdlog::details::os::sleep_for_millis(1100);
REQUIRE(test_sink->flush_counter() == 1); REQUIRE(test_sink->flush_counter() == 1);
spdlog::flush_every(std::chrono::seconds(0)); spdlog::flush_every(chrono::seconds(0));
spdlog::drop_all(); spdlog::drop_all();
} }
@ -95,7 +95,7 @@ TEST_CASE("tp->wait_empty() ", "[async]")
{ {
using namespace spdlog; using namespace spdlog;
auto test_sink = std::make_shared<sinks::test_sink_mt>(); auto test_sink = std::make_shared<sinks::test_sink_mt>();
test_sink->set_delay(std::chrono::milliseconds(5)); test_sink->set_delay(chrono::milliseconds(5));
size_t messages = 100; size_t messages = 100;
auto tp = std::make_shared<details::thread_pool>(messages, 2); auto tp = std::make_shared<details::thread_pool>(messages, 2);
@ -114,6 +114,8 @@ TEST_CASE("tp->wait_empty() ", "[async]")
TEST_CASE("multi threads", "[async]") TEST_CASE("multi threads", "[async]")
{ {
using namespace spdlog; using namespace spdlog;
using spdlog::details::thread;
auto test_sink = std::make_shared<sinks::test_sink_mt>(); auto test_sink = std::make_shared<sinks::test_sink_mt>();
size_t queue_size = 128; size_t queue_size = 128;
size_t messages = 256; size_t messages = 256;
@ -122,7 +124,7 @@ TEST_CASE("multi threads", "[async]")
auto tp = std::make_shared<details::thread_pool>(queue_size, 1); auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block); auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
std::vector<std::thread> threads; std::vector<thread> threads;
for (size_t i = 0; i < n_threads; i++) for (size_t i = 0; i < n_threads; i++)
{ {
threads.emplace_back([logger, messages] { threads.emplace_back([logger, messages] {

@ -85,9 +85,9 @@ TEST_CASE("periodic flush", "[periodic_flush]")
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]); auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
spdlog::flush_every(std::chrono::seconds(1)); spdlog::flush_every(spdlog::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(1100)); spdlog::details::os::sleep_for_millis(1100);
REQUIRE(test_sink->flush_counter() == 1); REQUIRE(test_sink->flush_counter() == 1);
spdlog::flush_every(std::chrono::seconds(0)); spdlog::flush_every(spdlog::chrono::seconds(0));
spdlog::drop_all(); spdlog::drop_all();
} }

@ -5,12 +5,9 @@
#pragma once #pragma once
#include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include "spdlog/details/null_mutex.h"
#include <chrono>
#include <mutex>
#include <thread>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -29,7 +26,7 @@ public:
return flush_counter_; return flush_counter_;
} }
void set_delay(std::chrono::milliseconds delay) void set_delay(spdlog::chrono::milliseconds delay)
{ {
delay_ = delay; delay_ = delay;
} }
@ -38,7 +35,7 @@ protected:
void sink_it_(const details::log_msg &) override void sink_it_(const details::log_msg &) override
{ {
msg_counter_++; msg_counter_++;
std::this_thread::sleep_for(delay_); spdlog::details::os::sleep_for_millis(delay_.count());
} }
void flush_() override void flush_() override
@ -47,10 +44,10 @@ protected:
} }
size_t msg_counter_{0}; size_t msg_counter_{0};
size_t flush_counter_{0}; size_t flush_counter_{0};
std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()}; spdlog::chrono::milliseconds delay_{spdlog::chrono::milliseconds::zero()};
}; };
using test_sink_mt = test_sink<std::mutex>; using test_sink_mt = test_sink<spdlog::mutex>;
using test_sink_st = test_sink<details::null_mutex>; using test_sink_st = test_sink<details::null_mutex>;
} // namespace sinks } // namespace sinks

Loading…
Cancel
Save