clock_ut.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <gtest/gtest.h>
  2. #include <library/cpp/yt/cpu_clock/clock.h>
  3. namespace NYT {
  4. namespace {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. template <class T>
  7. i64 DiffMS(T a, T b)
  8. {
  9. return a >= b
  10. ? static_cast<i64>(a.MilliSeconds()) - static_cast<i64>(b.MilliSeconds())
  11. : DiffMS(b, a);
  12. }
  13. TEST(TTimingTest, GetInstant)
  14. {
  15. GetInstant();
  16. EXPECT_LE(DiffMS(GetInstant(), TInstant::Now()), 10);
  17. }
  18. TEST(TTimingTest, InstantVSCpuInstant)
  19. {
  20. auto instant1 = TInstant::Now();
  21. auto cpuInstant = InstantToCpuInstant(instant1);
  22. auto instant2 = CpuInstantToInstant(cpuInstant);
  23. EXPECT_LE(DiffMS(instant1, instant2), 10);
  24. }
  25. TEST(TTimingTest, DurationVSCpuDuration)
  26. {
  27. auto cpuInstant1 = GetCpuInstant();
  28. constexpr auto duration1 = TDuration::MilliSeconds(100);
  29. Sleep(duration1);
  30. auto cpuInstant2 = GetCpuInstant();
  31. auto duration2 = CpuDurationToDuration(cpuInstant2 - cpuInstant1);
  32. EXPECT_LE(DiffMS(duration1, duration2), 10);
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////
  35. } // namespace
  36. } // namespace NYT