From 7306461b2fdf5c3fd8511201aa94aa74853bdf7e Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:15:38 +0800 Subject: [PATCH] feat update chrono --- tile/base/chrono.cc | 3 ++- tile/base/chrono.h | 2 +- tile/base/chrono_test.cc | 12 +++++++----- tile/base/object_pool/thread_local_test.cc | 13 +++++++++---- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/tile/base/chrono.cc b/tile/base/chrono.cc index f0c6a45..1cba8f4 100644 --- a/tile/base/chrono.cc +++ b/tile/base/chrono.cc @@ -90,7 +90,8 @@ void CoarseClockInitializer::Start() { while (running_.load(std::memory_order_relaxed)) { // std::this_thread::sleep_for(std::chrono::nanoseconds(500)); UpdateCoarseTimestamps(); - Sleep(accuracy_as_ns / 2); + // Sleep(accuracy_as_ns / 2); + std::this_thread::sleep_for(kAccuracy / 2); } }); } diff --git a/tile/base/chrono.h b/tile/base/chrono.h index 00599b1..7107782 100644 --- a/tile/base/chrono.h +++ b/tile/base/chrono.h @@ -17,7 +17,7 @@ namespace chrono { class CoarseClockInitializer { public: - static constexpr auto kAccuracy = std::chrono::microseconds(100); + static constexpr auto kAccuracy = std::chrono::microseconds(500); static CoarseClockInitializer *Instance(); // for `tile::Start()` diff --git a/tile/base/chrono_test.cc b/tile/base/chrono_test.cc index be71bf0..8df941c 100644 --- a/tile/base/chrono_test.cc +++ b/tile/base/chrono_test.cc @@ -4,7 +4,7 @@ namespace tile { -static constexpr auto one = detail::chrono::CoarseClockInitializer::kAccuracy; +static constexpr auto one_ms = std::chrono::milliseconds(1); long AvageTime(std::function f, std::size_t n = 100) { long double total = 0; @@ -17,28 +17,30 @@ long AvageTime(std::function f, std::size_t n = 100) { TEST(SystemClock, Compare) { auto diff = AvageTime([] { - return (ReadSystemClock() - std::chrono::system_clock::now()) / one; + return (ReadSystemClock() - std::chrono::system_clock::now()) / one_ms; }); ASSERT_NEAR(diff, 0, 5); } TEST(SteadyClock, Compare) { auto diff = AvageTime([] { - return (ReadSteadyClock() - std::chrono::steady_clock::now()) / one; + return (ReadSteadyClock() - std::chrono::steady_clock::now()) / one_ms; }); ASSERT_NEAR(diff, 0, 5); } TEST(CoarseSystemClock, Compare) { auto diff = AvageTime([] { - return (ReadCoarseSystemClock() - std::chrono::system_clock::now()) / one; + return (ReadCoarseSystemClock() - std::chrono::system_clock::now()) / + one_ms; }); ASSERT_NEAR(diff, 0, 50); } TEST(CoarseSteadyClock, Compare) { auto diff = AvageTime([] { - return (ReadCoarseSteadyClock() - std::chrono::steady_clock::now()) / one; + return (ReadCoarseSteadyClock() - std::chrono::steady_clock::now()) / + one_ms; }); ASSERT_NEAR(diff, 0, 50); } diff --git a/tile/base/object_pool/thread_local_test.cc b/tile/base/object_pool/thread_local_test.cc index 09aa8cf..c504032 100644 --- a/tile/base/object_pool/thread_local_test.cc +++ b/tile/base/object_pool/thread_local_test.cc @@ -61,6 +61,7 @@ static void BusySleep(std::chrono::duration dur) { namespace object_pool { TEST(ThreadLocalPool, All) { + auto start = ReadCoarseSteadyClock(); // create 1000 unref objects; ASSERT_EQ(alive, 0); { @@ -76,13 +77,17 @@ TEST(ThreadLocalPool, All) { BusySleep(std::chrono::milliseconds(10)); Get().Reset(); // Trigger wash out if possible. } - ASSERT_EQ(PoolTraits::kHighWaterMark + PoolTraits::kLowWaterMark, - alive); // High-water mark. + if ((ReadCoarseSteadyClock() - start) < PoolTraits::kMaxIdle) { + ASSERT_EQ(PoolTraits::kHighWaterMark + PoolTraits::kLowWaterMark, + alive); // High-water mark. + } // Max idle not reached. No effect. Get().Reset(); - ASSERT_EQ(PoolTraits::kHighWaterMark + PoolTraits::kLowWaterMark, - alive); + if ((ReadCoarseSteadyClock() - start) < PoolTraits::kMaxIdle) { + ASSERT_EQ(PoolTraits::kHighWaterMark + PoolTraits::kLowWaterMark, + alive); + } BusySleep(std::chrono::milliseconds(5000)); // std::this_thread::sleep_for(std::chrono::milliseconds(5000));