Timer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "Test.h"
  2. #include "Timer.h"
  3. #include <Logging.h>
  4. #include <unistd.h>
  5. #include <algorithm>
  6. #include <chrono>
  7. #include <deque>
  8. #include <memory>
  9. #include <sstream>
  10. #include <string>
  11. namespace Timer {
  12. namespace Test {
  13. void print()
  14. {
  15. Logging::Log(Logging::Severity::Info, "Print");
  16. }
  17. void TestTimerAbort()
  18. {
  19. Timer timer;
  20. std::function<void()> f_print = print;
  21. std::stringstream ss;
  22. ss << "---" << std::endl;
  23. auto start = std::chrono::steady_clock::now();
  24. std::clock_t begin = std::clock();
  25. timer.StartContinuous(1000, f_print);
  26. timer.Stop();
  27. timer.Wait();
  28. auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
  29. ss << "Elapsed: " << elapsed.count() << std::endl;
  30. ss << "---" << std::endl << std::endl;
  31. Logging::Log(Logging::Severity::Info, ss.str());
  32. ss.str("");
  33. ss << "---" << std::endl;
  34. start = std::chrono::steady_clock::now();
  35. timer.StartContinuous(3000, f_print);
  36. timer.Abort();
  37. timer.Wait();
  38. elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
  39. ss << "Elapsed: " << elapsed.count() << std::endl;
  40. ss << "---" << std::endl << std::endl;
  41. Logging::Log(Logging::Severity::Info, ss.str());
  42. ss.str("");
  43. ss << "---" << std::endl;
  44. start = std::chrono::steady_clock::now();
  45. timer.StartContinuous(3000, f_print);
  46. sleep(1);
  47. timer.Abort();
  48. timer.Wait();
  49. elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
  50. ss << "Elapsed: " << elapsed.count() << std::endl;
  51. ss << "---" << std::endl << std::endl;
  52. Logging::Log(Logging::Severity::Info, ss.str());
  53. }
  54. template<typename T, typename... Args>
  55. std::unique_ptr<T> make_unique(Args&&... args) {
  56. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  57. }
  58. void OnTimerEnd(const std::string& type, int id)
  59. {
  60. std::stringstream ss;
  61. ss << "OnTimerEnd() " << type << " " << id << std::endl;
  62. Logging::Log(Logging::Severity::Info, ss.str());
  63. }
  64. void StartNewTimer(std::deque<TimerPointer>& timers)
  65. {
  66. int delay(2000);
  67. std::string identifier("Blabla");
  68. int id(10);
  69. // Protect!
  70. auto timer = make_unique<Timer>();
  71. timer->StartSingle(delay, static_cast<std::function<void(const std::string&, int)>>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id);
  72. timers.push_back(std::move(timer));
  73. }
  74. void Cleanup(std::deque<TimerPointer>& timers)
  75. {
  76. timers.erase(std::remove_if(timers.begin(), timers.end(), [](TimerPointer& t){return !t->IsRunning();}), timers.end());
  77. }
  78. void TestTimerDeque()
  79. {
  80. std::stringstream ss;
  81. std::deque<TimerPointer> timers;
  82. StartNewTimer(timers);
  83. ss << "Count: " << timers.size() << std::endl;
  84. Logging::Log(Logging::Severity::Info, ss.str());
  85. ss.str("");
  86. StartNewTimer(timers);
  87. ss << "Count: " << timers.size() << std::endl;
  88. Logging::Log(Logging::Severity::Info, ss.str());
  89. sleep(5);
  90. ss.str("");
  91. ss << "Count: " << timers.size() << std::endl;
  92. Logging::Log(Logging::Severity::Info, ss.str());
  93. ss.str("");
  94. Cleanup(timers);
  95. ss << "Count: " << timers.size() << std::endl;
  96. Logging::Log(Logging::Severity::Info, ss.str());
  97. }
  98. bool TestTimer()
  99. {
  100. std::stringstream ss;
  101. ss << "----------------------------------" << std::endl;
  102. ss << "| TestTimer |" << std::endl;
  103. ss << "----------------------------------" << std::endl;
  104. Logging::Log(Logging::Severity::Info, ss.str());
  105. TestTimerAbort();
  106. TestTimerDeque();
  107. ss.str("");
  108. ss << "==================================" << std::endl << std::endl;
  109. Logging::Log(Logging::Severity::Info, ss.str());
  110. return true;
  111. }
  112. } // namespace Test
  113. } // namespace Timer
  114. /*
  115. */