From 3c9e2260c8ae6d10ae08cad68a0a98cee17b682a Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Tue, 15 Oct 2024 10:59:42 +0800 Subject: [PATCH] feat: chrono update duration=1ms --- CMakeLists.txt | 1 + tile/base/chrono.h | 2 +- tile/base/chrono_test.cc | 13 +++++++------ tile/tile_cpu_test.cc | 5 +++++ 4 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 tile/tile_cpu_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index e2d0272..551351e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -331,6 +331,7 @@ endfunction(add_test_group) add_test_group(${CMAKE_CURRENT_SOURCE_DIR}/tile/net net) add_test_group(${CMAKE_CURRENT_SOURCE_DIR}/tile/rpc rpc) tile_add_custom_test("custom_http_client_test" "tests/http_client_test.cc") + tile_add_custom_test("tile_cpu_test" "tile/tile_cpu_test.cc") endif(TILE_BUILD_TESTS) if(TILE_BUILD_BENCHMARKS) diff --git a/tile/base/chrono.h b/tile/base/chrono.h index 415ff10..5dc4e23 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(500); + static constexpr auto kAccuracy = std::chrono::microseconds(1000); static CoarseClockInitializer *Instance(); // for `tile::Start()` diff --git a/tile/base/chrono_test.cc b/tile/base/chrono_test.cc index 6bee577..cbdaa97 100644 --- a/tile/base/chrono_test.cc +++ b/tile/base/chrono_test.cc @@ -4,8 +4,9 @@ namespace tile { -static constexpr auto one_ms = std::chrono::milliseconds(1); -static constexpr auto kTestN = 1000; +static constexpr auto one_ms = std::chrono::milliseconds(1); +static constexpr auto diff_ms = 10; +static constexpr auto kTestN = 1000; long AvageTime(std::function f, std::size_t n = kTestN) @@ -21,24 +22,24 @@ AvageTime(std::function f, std::size_t n = kTestN) TEST(SystemClock, Compare) { auto diff = AvageTime([] { return (ReadSystemClock() - std::chrono::system_clock::now()) / one_ms; }); - ASSERT_NEAR(diff, 0, 5); + ASSERT_NEAR(diff, 0, diff_ms); } TEST(SteadyClock, Compare) { auto diff = AvageTime([] { return (ReadSteadyClock() - std::chrono::steady_clock::now()) / one_ms; }); - ASSERT_NEAR(diff, 0, 5); + ASSERT_NEAR(diff, 0, diff_ms); } TEST(CoarseSystemClock, Compare) { auto diff = AvageTime([] { return (ReadCoarseSystemClock() - std::chrono::system_clock::now()) / one_ms; }); - ASSERT_NEAR(diff, 0, kTestN); + ASSERT_NEAR(diff, 0, kTestN * diff_ms); } TEST(CoarseSteadyClock, Compare) { auto diff = AvageTime([] { return (ReadCoarseSteadyClock() - std::chrono::steady_clock::now()) / one_ms; }); - ASSERT_NEAR(diff, 0, kTestN); + ASSERT_NEAR(diff, 0, kTestN * diff_ms); } }// namespace tile diff --git a/tile/tile_cpu_test.cc b/tile/tile_cpu_test.cc new file mode 100644 index 0000000..46a1852 --- /dev/null +++ b/tile/tile_cpu_test.cc @@ -0,0 +1,5 @@ +#include "tile/tile.h" + +#include "gtest/gtest.h" + +TEST(CPU, Base) { std::this_thread::sleep_for(std::chrono::seconds(30)); }