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.

latency.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Copyright(c) 2018 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. //
  6. // latency.cpp : spdlog latency 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>
  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, char *[])
  29. {
  30. std::srand(static_cast<unsigned>(std::time(nullptr))); // use current time as seed for random generator
  31. int howmany = 1000000;
  32. int queue_size = howmany + 2;
  33. int threads = 10;
  34. size_t file_size = 30 * 1024 * 1024;
  35. size_t rotating_files = 5;
  36. try
  37. {
  38. cout << "******************************************************************"
  39. "*************\n";
  40. cout << "Single thread\n";
  41. cout << "******************************************************************"
  42. "*************\n";
  43. auto basic_st = spdlog::basic_logger_mt("basic_st", "logs/basic_st.log", true);
  44. bench(howmany, basic_st);
  45. auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st.log", file_size, rotating_files);
  46. bench(howmany, rotating_st);
  47. auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st.log");
  48. bench(howmany, daily_st);
  49. bench(howmany, spdlog::create<null_sink_st>("null_st"));
  50. cout << "\n****************************************************************"
  51. "***************\n";
  52. cout << threads << " threads sharing same logger\n";
  53. cout << "******************************************************************"
  54. "*************\n";
  55. auto basic_mt = spdlog::basic_logger_mt("basic_mt", "logs/basic_mt.log", true);
  56. bench_mt(howmany, basic_mt, threads);
  57. auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt.log", file_size, rotating_files);
  58. bench_mt(howmany, rotating_mt, threads);
  59. auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt.log");
  60. bench_mt(howmany, daily_mt, threads);
  61. bench(howmany, spdlog::create<null_sink_st>("null_mt"));
  62. cout << "\n****************************************************************"
  63. "***************\n";
  64. cout << "async logging.. " << threads << " threads sharing same logger\n";
  65. cout << "******************************************************************"
  66. "*************\n";
  67. for (int i = 0; i < 3; ++i)
  68. {
  69. spdlog::init_thread_pool(static_cast<size_t>(queue_size), 1);
  70. auto as = spdlog::basic_logger_mt<spdlog::async_factory>("async", "logs/basic_async.log", true);
  71. bench_mt(howmany, as, threads);
  72. spdlog::drop("async");
  73. }
  74. }
  75. catch (std::exception &ex)
  76. {
  77. std::cerr << "Error: " << ex.what() << std::endl;
  78. perror("Last error");
  79. return EXIT_FAILURE;
  80. }
  81. return EXIT_SUCCESS;
  82. }
  83. void bench(int howmany, std::shared_ptr<spdlog::logger> log)
  84. {
  85. using namespace std::chrono;
  86. using chrono::high_resolution_clock;
  87. using chrono::milliseconds;
  88. using chrono::nanoseconds;
  89. cout << log->name() << "...\t\t" << flush;
  90. nanoseconds total_nanos = nanoseconds::zero();
  91. for (auto i = 0; i < howmany; ++i)
  92. {
  93. auto start = high_resolution_clock::now();
  94. log->info("Hello logger: msg number {}", i);
  95. auto delta_nanos = chrono::duration_cast<nanoseconds>(high_resolution_clock::now() - start);
  96. total_nanos += delta_nanos;
  97. }
  98. auto avg = total_nanos.count() / howmany;
  99. cout << format(avg) << " ns/call" << endl;
  100. }
  101. void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
  102. {
  103. using namespace std::chrono;
  104. using chrono::high_resolution_clock;
  105. using chrono::milliseconds;
  106. using chrono::nanoseconds;
  107. cout << log->name() << "...\t\t" << flush;
  108. vector<thread> threads;
  109. std::atomic<nanoseconds::rep> total_nanos{0};
  110. for (int t = 0; t < thread_count; ++t)
  111. {
  112. threads.push_back(std::thread([&]() {
  113. for (int j = 0; j < howmany / thread_count; j++)
  114. {
  115. auto start = high_resolution_clock::now();
  116. log->info("Hello logger: msg number {}", j);
  117. auto delta_nanos = chrono::duration_cast<nanoseconds>(high_resolution_clock::now() - start);
  118. total_nanos += delta_nanos.count();
  119. }
  120. }));
  121. }
  122. for (auto &t : threads)
  123. {
  124. t.join();
  125. };
  126. auto avg = total_nanos / howmany;
  127. cout << format(avg) << " ns/call" << endl;
  128. }