Bench: eat our own dog food

This commit is contained in:
gabime 2018-10-16 16:11:28 +03:00
parent c465250c21
commit 13cc6478fb
1 changed files with 23 additions and 33 deletions

View File

@ -16,7 +16,6 @@
#include "utils.h" #include "utils.h"
#include <atomic> #include <atomic>
#include <cstdlib> // EXIT_FAILURE #include <cstdlib> // EXIT_FAILURE
#include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
#include <thread> #include <thread>
@ -34,6 +33,7 @@ void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
spdlog::default_logger()->set_pattern("[%^%l%$] %v");
int howmany = 1000000; int howmany = 1000000;
int queue_size = howmany + 2; int queue_size = howmany + 2;
int threads = 10; int threads = 10;
@ -50,11 +50,10 @@ int main(int argc, char *argv[])
if (argc > 3) if (argc > 3)
queue_size = atoi(argv[3]); queue_size = atoi(argv[3]);
cout << "******************************************************************" spdlog::info("**************************************************************");
"*************\n"; spdlog::info("Single thread, {:n} iterations", howmany);
cout << "Single thread, " << format(howmany) << " iterations" << endl; spdlog::info("**************************************************************");
cout << "******************************************************************"
"*************\n";
auto basic_st = spdlog::basic_logger_st("basic_st", "logs/basic_st.log", true); auto basic_st = spdlog::basic_logger_st("basic_st", "logs/basic_st.log", true);
bench(howmany, std::move(basic_st)); bench(howmany, std::move(basic_st));
@ -68,11 +67,9 @@ int main(int argc, char *argv[])
bench(howmany, spdlog::create<null_sink_st>("null_st")); bench(howmany, spdlog::create<null_sink_st>("null_st"));
cout << "******************************************************************" spdlog::info("**************************************************************");
"*************\n"; spdlog::info("Default API. Single thread, {:n} iterations", howmany);
cout << "Default API. Single thread, " << format(howmany) << " iterations" << endl; spdlog::info("**************************************************************");
cout << "******************************************************************"
"*************\n";
basic_st = spdlog::basic_logger_st("basic_st", "logs/basic_st.log", true); basic_st = spdlog::basic_logger_st("basic_st", "logs/basic_st.log", true);
bench_default_api(howmany, std::move(basic_st)); bench_default_api(howmany, std::move(basic_st));
@ -85,11 +82,9 @@ int main(int argc, char *argv[])
bench_default_api(howmany, spdlog::create<null_sink_st>("null_st")); bench_default_api(howmany, spdlog::create<null_sink_st>("null_st"));
cout << "\n****************************************************************" spdlog::info("**************************************************************");
"***************\n"; spdlog::info("{:n} threads sharing same logger, {:n} iterations", threads, howmany);
cout << threads << " threads sharing same logger, " << format(howmany) << " iterations" << endl; spdlog::info("**************************************************************");
cout << "******************************************************************"
"*************\n";
auto basic_mt = spdlog::basic_logger_mt("basic_mt", "logs/basic_mt.log", true); auto basic_mt = spdlog::basic_logger_mt("basic_mt", "logs/basic_mt.log", true);
bench_mt(howmany, std::move(basic_mt), threads); bench_mt(howmany, std::move(basic_mt), threads);
@ -101,11 +96,9 @@ int main(int argc, char *argv[])
bench_mt(howmany, std::move(daily_mt), threads); bench_mt(howmany, std::move(daily_mt), threads);
bench_mt(howmany, spdlog::create<null_sink_mt>("null_mt"), threads); bench_mt(howmany, spdlog::create<null_sink_mt>("null_mt"), threads);
cout << "\n****************************************************************" spdlog::info("**************************************************************");
"***************\n"; spdlog::info("Asyncronous.. {:n} threads sharing same logger, {:n} iterations", threads, howmany);
cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations " << endl; spdlog::info("**************************************************************");
cout << "******************************************************************"
"*************\n";
for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
{ {
@ -116,8 +109,7 @@ int main(int argc, char *argv[])
} }
catch (std::exception &ex) catch (std::exception &ex)
{ {
std::cerr << "Error: " << ex.what() << std::endl; spdlog::error(ex.what());
perror("Last error");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -126,7 +118,6 @@ 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 std::chrono::high_resolution_clock;
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)
{ {
@ -136,14 +127,13 @@ 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 = duration_cast<duration<double>>(delta).count();
cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl; spdlog::info("{:<16} Elapsed: {:0.2f} secs {:>16n}/sec", log->name(), delta_d, int(howmany / delta_d));
spdlog::drop_all(); spdlog::drop(log->name());
} }
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 std::chrono::high_resolution_clock;
cout << log->name() << "...\t\t" << flush;
vector<thread> threads; vector<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)
@ -163,14 +153,14 @@ 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 = duration_cast<duration<double>>(delta).count();
cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl; spdlog::info("{:<16} Elapsed: {:0.2f} secs {:>16n}/sec", log->name(), delta_d, int(howmany / delta_d));
spdlog::drop_all(); spdlog::drop(log->name());
} }
void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log) void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log)
{ {
using std::chrono::high_resolution_clock; using std::chrono::high_resolution_clock;
cout << log->name() << "...\t\t" << flush; auto orig_default = spdlog::default_logger();
spdlog::set_default_logger(log); spdlog::set_default_logger(log);
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)
@ -180,7 +170,7 @@ void bench_default_api(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 = duration_cast<duration<double>>(delta).count();
spdlog::drop(log->name());
cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl; spdlog::set_default_logger(std::move(orig_default));
spdlog::drop_all(); spdlog::info("{:<16} Elapsed: {:0.2f} secs {:>16n}/sec", log->name(), delta_d, int(howmany / delta_d));
} }