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.

async_bench.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/stdout_color_sinks.h"
  11. #include "spdlog/spdlog.h"
  12. #include "utils.h"
  13. #include <atomic>
  14. #include <iostream>
  15. #include <memory>
  16. #include <string>
  17. #include <thread>
  18. using namespace std;
  19. using namespace std::chrono;
  20. using namespace spdlog;
  21. using namespace spdlog::sinks;
  22. using namespace utils;
  23. void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
  24. int count_lines(const char *filename)
  25. {
  26. int counter = 0;
  27. auto *infile = fopen(filename, "r");
  28. int ch;
  29. while (EOF != (ch = getc(infile)))
  30. {
  31. if ('\n' == ch)
  32. counter++;
  33. }
  34. fclose(infile);
  35. return counter;
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. int howmany = 1000000;
  40. int queue_size = howmany + 2;
  41. int threads = 10;
  42. int iters = 3;
  43. try
  44. {
  45. auto console = spdlog::stdout_color_mt("console");
  46. if (argc == 1)
  47. {
  48. console->set_pattern("%v");
  49. console->info("Usage: {} <message_count> <threads> <q_size> <iterations>", argv[0]);
  50. return 0;
  51. }
  52. if (argc > 1)
  53. howmany = atoi(argv[1]);
  54. if (argc > 2)
  55. threads = atoi(argv[2]);
  56. if (argc > 3)
  57. queue_size = atoi(argv[3]);
  58. if (argc > 4)
  59. iters = atoi(argv[4]);
  60. console->info("-------------------------------------------------");
  61. console->info("Messages: {:14n}", howmany);
  62. console->info("Threads : {:14n}", threads);
  63. console->info("Queue : {:14n}", queue_size);
  64. console->info("Iters : {:>14n}", iters);
  65. console->info("-------------------------------------------------");
  66. const char *filename = "logs/basic_async.log";
  67. for (int i = 0; i < iters; i++)
  68. {
  69. auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
  70. auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
  71. auto logger = std::make_shared<async_logger>("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::block);
  72. bench_mt(howmany, std::move(logger), threads);
  73. auto count = count_lines(filename);
  74. if (count != howmany)
  75. {
  76. console->error("Test failed. {} has {:n} lines instead of {:n}", filename, count, howmany);
  77. exit(1);
  78. }
  79. else
  80. {
  81. console->info("Line count OK ({:n})\n", count);
  82. }
  83. }
  84. }
  85. catch (std::exception &ex)
  86. {
  87. std::cerr << "Error: " << ex.what() << std::endl;
  88. perror("Last error");
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. void thread_fun(std::shared_ptr<spdlog::logger> logger, int howmany)
  94. {
  95. for (int i = 0; i < howmany; i++)
  96. {
  97. logger->info("Hello logger: msg number {}", i);
  98. }
  99. }
  100. void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_count)
  101. {
  102. using std::chrono::high_resolution_clock;
  103. vector<thread> threads;
  104. auto start = high_resolution_clock::now();
  105. int msgs_per_thread = howmany / thread_count;
  106. int msgs_per_thread_mod = howmany % thread_count;
  107. for (int t = 0; t < thread_count; ++t)
  108. {
  109. if (t == 0 && msgs_per_thread_mod)
  110. threads.push_back(std::thread(thread_fun, logger, msgs_per_thread + msgs_per_thread_mod));
  111. else
  112. threads.push_back(std::thread(thread_fun, logger, msgs_per_thread));
  113. }
  114. for (auto &t : threads)
  115. {
  116. t.join();
  117. };
  118. auto delta = high_resolution_clock::now() - start;
  119. auto delta_d = duration_cast<duration<double>>(delta).count();
  120. spdlog::get("console")->info("Elapsed: {} secs\t {:n}/sec", delta_d, int(howmany / delta_d));
  121. }