Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

bench.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. //
  6. // bench.cpp : spdlog benchmarks
  7. //
  8. #include "spdlog/async.h"
  9. #include "spdlog/sinks/basic_file_sink.h"
  10. #include "spdlog/sinks/daily_file_sink.h"
  11. #include "spdlog/sinks/null_sink.h"
  12. #include "spdlog/sinks/rotating_file_sink.h"
  13. #include "spdlog/spdlog.h"
  14. #include "utils.h"
  15. #include <atomic>
  16. #include <cstdlib> // EXIT_FAILURE
  17. #include <iostream>
  18. #include <memory>
  19. #include <string>
  20. #include <thread>
  21. using namespace std;
  22. using namespace std::chrono;
  23. using namespace spdlog;
  24. using namespace spdlog::sinks;
  25. using namespace utils;
  26. void bench(int howmany, std::shared_ptr<spdlog::logger> log);
  27. void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
  28. int main(int argc, char *argv[])
  29. {
  30. int howmany = 1000000;
  31. int queue_size = howmany + 2;
  32. int threads = 10;
  33. size_t file_size = 30 * 1024 * 1024;
  34. size_t rotating_files = 5;
  35. try
  36. {
  37. if (argc > 1)
  38. howmany = atoi(argv[1]);
  39. if (argc > 2)
  40. threads = atoi(argv[2]);
  41. if (argc > 3)
  42. queue_size = atoi(argv[3]);
  43. cout << "******************************************************************"
  44. "*************\n";
  45. cout << "Single thread, " << format(howmany) << " iterations" << endl;
  46. cout << "******************************************************************"
  47. "*************\n";
  48. auto basic_st = spdlog::basic_logger_st("basic_st", "logs/basic_st.log", true);
  49. bench(howmany, basic_st);
  50. auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st.log", file_size, rotating_files);
  51. bench(howmany, rotating_st);
  52. auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st.log");
  53. bench(howmany, daily_st);
  54. bench(howmany, spdlog::create<null_sink_st>("null_st"));
  55. cout << "\n****************************************************************"
  56. "***************\n";
  57. cout << threads << " threads sharing same logger, " << format(howmany) << " iterations" << endl;
  58. cout << "******************************************************************"
  59. "*************\n";
  60. auto basic_mt = spdlog::basic_logger_mt("basic_mt", "logs/basic_mt.log", true);
  61. bench_mt(howmany, basic_mt, threads);
  62. auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt.log", file_size, rotating_files);
  63. bench_mt(howmany, rotating_mt, threads);
  64. auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt.log");
  65. bench_mt(howmany, daily_mt, threads);
  66. bench_mt(howmany, spdlog::create<null_sink_mt>("null_mt"), threads);
  67. cout << "\n****************************************************************"
  68. "***************\n";
  69. cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations " << endl;
  70. cout << "******************************************************************"
  71. "*************\n";
  72. for (int i = 0; i < 3; ++i)
  73. {
  74. spdlog::init_thread_pool(static_cast<size_t>(queue_size), 1);
  75. auto as = spdlog::basic_logger_mt<spdlog::async_factory>("async", "logs/basic_async.log", true);
  76. bench_mt(howmany, as, threads);
  77. spdlog::drop("async");
  78. }
  79. }
  80. catch (std::exception &ex)
  81. {
  82. std::cerr << "Error: " << ex.what() << std::endl;
  83. perror("Last error");
  84. return EXIT_FAILURE;
  85. }
  86. return EXIT_SUCCESS;
  87. }
  88. void bench(int howmany, std::shared_ptr<spdlog::logger> log)
  89. {
  90. using std::chrono::high_resolution_clock;
  91. cout << log->name() << "...\t\t" << flush;
  92. auto start = high_resolution_clock::now();
  93. for (auto i = 0; i < howmany; ++i)
  94. {
  95. log->info("Hello logger: msg number {}", i);
  96. }
  97. auto delta = high_resolution_clock::now() - start;
  98. auto delta_d = duration_cast<duration<double>>(delta).count();
  99. cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl;
  100. spdlog::drop(log->name());
  101. }
  102. void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
  103. {
  104. using std::chrono::high_resolution_clock;
  105. cout << log->name() << "...\t\t" << flush;
  106. vector<thread> threads;
  107. auto start = high_resolution_clock::now();
  108. for (int t = 0; t < thread_count; ++t)
  109. {
  110. threads.push_back(std::thread([&]() {
  111. for (int j = 0; j < howmany / thread_count; j++)
  112. {
  113. log->info("Hello logger: msg number {}", j);
  114. }
  115. }));
  116. }
  117. for (auto &t : threads)
  118. {
  119. t.join();
  120. };
  121. auto delta = high_resolution_clock::now() - start;
  122. auto delta_d = duration_cast<duration<double>>(delta).count();
  123. cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl;
  124. }