test_time_utility.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************
  2. * copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <atomic>
  22. #include <chrono>
  23. #include <future>
  24. #include <string>
  25. #include "util/cnstream_timer.hpp"
  26. using std::chrono::microseconds;
  27. using std::chrono::milliseconds;
  28. using std::chrono::steady_clock;
  29. namespace cnstream {
  30. TEST(TimeUtilityTest, TimeStampTest) {
  31. size_t ts1, ts2 = 0;
  32. std::string ts2_str;
  33. ts1 = TimeStamp::Current();
  34. auto start_time = steady_clock::now();
  35. while (1) {
  36. if (steady_clock::now() - start_time > milliseconds(100)) {
  37. ts2 = TimeStamp::Current();
  38. ts2_str = TimeStamp::CurrentToString();
  39. break;
  40. }
  41. }
  42. // Accurary depends on the current usage of CPU core.
  43. // The accuracy can reach to 1 in 100,000 when monopolize the core,
  44. // but also can less than 1 in 100 when the core is very heavy.
  45. // So do not test accuracy anymore in below unit tests.
  46. EXPECT_GE(ts2 - ts1, 1E5);
  47. EXPECT_GE(std::stoll(ts2_str) - ts1, 1E5);
  48. }
  49. TEST(TimeUtilityTest, TickClockTest) {
  50. TickClock tick_clock;
  51. int tick_times = 10;
  52. auto start_time = steady_clock::now();
  53. while (1) {
  54. if (steady_clock::now() - start_time > milliseconds(10)) {
  55. tick_clock.Tick();
  56. start_time = steady_clock::now();
  57. if (--tick_times < 0) break;
  58. }
  59. }
  60. double avg_time = tick_clock.ElapsedAverageAsDouble();
  61. EXPECT_GE(avg_time, 1E4);
  62. double total_time = tick_clock.ElapsedTotalAsDouble();
  63. EXPECT_GE(total_time, 1E5);
  64. tick_clock.Clear();
  65. avg_time = tick_clock.ElapsedAverageAsDouble();
  66. EXPECT_DOUBLE_EQ(avg_time, 0.0);
  67. }
  68. TEST(TimeUtilityTest, TickTockClockTest) {
  69. TickTockClock duration_recorder;
  70. for (int i = 0; i < 10; ++i) {
  71. duration_recorder.Tick();
  72. auto start_time = steady_clock::now();
  73. while (steady_clock::now() - start_time < milliseconds(10)) {
  74. }
  75. duration_recorder.Tock();
  76. }
  77. double avg_duration = duration_recorder.ElapsedAverageAsDouble();
  78. EXPECT_GE(avg_duration, 1E4);
  79. double total_time = duration_recorder.ElapsedTotalAsDouble();
  80. EXPECT_GE(total_time, 1E5);
  81. duration_recorder.Clear();
  82. avg_duration = duration_recorder.ElapsedAverageAsDouble();
  83. EXPECT_DOUBLE_EQ(avg_duration, 0.0);
  84. }
  85. } // namespace cnstream