From 7b389016087a0acccc37948eb1e4c663c576bb66 Mon Sep 17 00:00:00 2001 From: Viktor Govako Date: Fri, 10 Jun 2022 13:34:56 +0300 Subject: [PATCH] [base] Removed copy-paste in Timer. Signed-off-by: Viktor Govako --- base/base_tests/small_set_test.cpp | 14 +++--- base/timer.cpp | 34 +------------ base/timer.hpp | 49 ++++++++++--------- routing/index_graph.cpp | 2 +- .../routes_builder_tool/utils.cpp | 24 +++++---- search/search_params.hpp | 26 +++++----- .../test_search_request.cpp | 2 +- .../test_search_request.hpp | 10 ++-- testing/testingmain.cpp | 2 +- .../archival_reporter_tests.cpp | 13 ++--- 10 files changed, 77 insertions(+), 99 deletions(-) diff --git a/base/base_tests/small_set_test.cpp b/base/base_tests/small_set_test.cpp index f9f863991a..c67f2d4c1d 100644 --- a/base/base_tests/small_set_test.cpp +++ b/base/base_tests/small_set_test.cpp @@ -121,7 +121,7 @@ UNIT_TEST(SmallMap_Benchmark1) // 3. Run unordered_map. { - base::Timer timer; + base::HighResTimer timer; for (auto i : indices) sum1 += (uMap.find(i) != uMap.end() ? 1 : 0); t1 = timer.ElapsedMilliseconds(); @@ -129,7 +129,7 @@ UNIT_TEST(SmallMap_Benchmark1) // 4. Run SmallMap. { - base::Timer timer; + base::HighResTimer timer; for (auto i : indices) sum2 += (sMap.Find(i) ? 1 : 0); t2 = timer.ElapsedMilliseconds(); @@ -193,7 +193,7 @@ UNIT_TEST(SmallMap_Benchmark2) // 3. Run unordered_map. { - base::Timer timer; + base::HighResTimer timer; for (auto i : indices) { auto const it = uMap.find(keys[i]); @@ -205,7 +205,7 @@ UNIT_TEST(SmallMap_Benchmark2) // 4. Run SmallMap. { - base::Timer timer; + base::HighResTimer timer; for (auto i : indices) { auto const * p = sMap.Find(keys[i]); @@ -242,7 +242,7 @@ UNIT_TEST(SmallMap_Benchmark3) // 3. Run unordered_map. { - base::Timer timer; + base::HighResTimer timer; for (auto i : indices) sum1 += uMap.find(keys[i])->second; t1 = timer.ElapsedMilliseconds(); @@ -250,7 +250,7 @@ UNIT_TEST(SmallMap_Benchmark3) // 4. Run SmallMap. { - base::Timer timer; + base::HighResTimer timer; for (auto i : indices) sum2 += *sMap.Find(keys[i]); t2 = timer.ElapsedMilliseconds(); @@ -258,7 +258,7 @@ UNIT_TEST(SmallMap_Benchmark3) // 5. Run SmallMapBase. { - base::Timer timer; + base::HighResTimer timer; for (auto i : indices) sum3 += *sbMap.Find(keys[i]); t3 = timer.ElapsedMilliseconds(); diff --git a/base/timer.cpp b/base/timer.cpp index 6b4a0a3e2a..8e7c36453f 100644 --- a/base/timer.cpp +++ b/base/timer.cpp @@ -19,12 +19,6 @@ namespace base { -Timer::Timer(bool start/* = true*/) -{ - if (start) - Reset(); -} - // static double Timer::LocalTime() { @@ -171,32 +165,6 @@ time_t StringToTimestamp(std::string const & s) return res; } -HighResTimer::HighResTimer(bool start/* = true*/) -{ - if (start) - Reset(); -} - -void HighResTimer::Reset() -{ - m_start = std::chrono::high_resolution_clock::now(); -} - -uint64_t HighResTimer::ElapsedNano() const -{ - return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - m_start).count(); -} - -uint64_t HighResTimer::ElapsedMillis() const -{ - return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - m_start).count(); -} - -double HighResTimer::ElapsedSeconds() const -{ - return std::chrono::duration_cast>(std::chrono::high_resolution_clock::now() - m_start).count(); -} - time_t SecondsSinceEpochToTimeT(uint64_t secondsSinceEpoch) { std::chrono::time_point const tpoint{std::chrono::seconds(secondsSinceEpoch)}; @@ -220,7 +188,7 @@ ScopedTimerWithLog::~ScopedTimerWithLog() { case Measure::MilliSeconds: { - LOG(LINFO, (m_name, "time:", m_timer.ElapsedMillis(), "ms")); + LOG(LINFO, (m_name, "time:", m_timer.ElapsedMilliseconds(), "ms")); return; } case Measure::Seconds: diff --git a/base/timer.hpp b/base/timer.hpp index d918265751..49499e1fad 100644 --- a/base/timer.hpp +++ b/base/timer.hpp @@ -7,19 +7,22 @@ namespace base { -/// Cross platform timer -class Timer +namespace impl { - std::chrono::steady_clock::time_point m_startTime; +template class StdTimer +{ + typename ClockT::time_point m_startTime; public: - explicit Timer(bool start = true); - - /// @return current UTC time in seconds, elapsed from 1970. - static double LocalTime(); + explicit StdTimer(bool start = true) + { + if (start) + Reset(); + } + using DurationT = typename ClockT::duration; /// @return Elapsed time from start (@see Reset). - inline std::chrono::steady_clock::duration TimeElapsed() const { return std::chrono::steady_clock::now() - m_startTime; } + inline DurationT TimeElapsed() const { return ClockT::now() - m_startTime; } template inline Duration TimeElapsedAs() const @@ -31,7 +34,21 @@ public: inline uint64_t ElapsedMilliseconds() const { return TimeElapsedAs().count(); } inline uint64_t ElapsedNanoseconds() const { return TimeElapsedAs().count(); } - inline void Reset() { m_startTime = std::chrono::steady_clock::now(); } + inline void Reset() { m_startTime = ClockT::now(); } +}; +} // namespace impl + + +/// Cross platform timer +class Timer : public impl::StdTimer +{ + using BaseT = impl::StdTimer; + +public: + using BaseT::BaseT; + + /// @return current UTC time in seconds, elapsed from 1970. + static double LocalTime(); }; std::string FormatCurrentTime(); @@ -62,19 +79,7 @@ time_t StringToTimestamp(std::string const & s); /// High resolution timer to use in comparison tests. -class HighResTimer -{ - typedef std::chrono::high_resolution_clock::time_point PointT; - PointT m_start; - -public: - explicit HighResTimer(bool start = true); - - void Reset(); - uint64_t ElapsedNano() const; - uint64_t ElapsedMillis() const; - double ElapsedSeconds() const; -}; +using HighResTimer = impl::StdTimer; class ScopedTimerWithLog { diff --git a/routing/index_graph.cpp b/routing/index_graph.cpp index 6ab2fde34f..92dd7a3df4 100644 --- a/routing/index_graph.cpp +++ b/routing/index_graph.cpp @@ -226,7 +226,7 @@ void IndexGraph::SetRestrictions(RestrictionVec && restrictions) m_restrictionsBackward[restriction.front()].emplace_back(next(restriction.begin()), restriction.end()); } - LOG(LDEBUG, ("Restrictions are loaded in:", timer.ElapsedNano() / 1e6, "ms")); + LOG(LDEBUG, ("Restrictions are loaded in:", timer.ElapsedMilliseconds(), "ms")); } void IndexGraph::SetUTurnRestrictions(vector && noUTurnRestrictions) diff --git a/routing/routes_builder/routes_builder_tool/utils.cpp b/routing/routes_builder/routes_builder_tool/utils.cpp index 74d3bf4184..bfe4866753 100644 --- a/routing/routes_builder/routes_builder_tool/utils.cpp +++ b/routing/routes_builder/routes_builder_tool/utils.cpp @@ -25,6 +25,11 @@ #include #include + +namespace routing +{ +namespace routes_builder +{ using namespace routing_quality; namespace @@ -58,10 +63,6 @@ routing::VehicleType ConvertVehicleTypeFromString(std::string const & str) } } // namespace -namespace routing -{ -namespace routes_builder -{ void BuildRoutes(std::string const & routesPath, std::string const & dumpPath, uint64_t startFrom, @@ -180,8 +181,9 @@ void BuildRoutesWithApi(std::unique_ptr routingApi, size_t rps = 0; base::HighResTimer timer; - auto const getElapsedMilliSeconds = [&timer]() { - double ms = timer.ElapsedNano() / 1e6; + auto const getElapsedMilliSeconds = [&timer]() + { + auto const ms = timer.ElapsedMilliseconds(); LOG(LDEBUG, ("Elapsed:", ms, "ms")); return ms; }; @@ -191,9 +193,10 @@ void BuildRoutesWithApi(std::unique_ptr routingApi, timer.Reset(); }; - auto const sleepIfNeed = [&]() { - double constexpr kMsInSecond = 1000.0; - if (getElapsedMilliSeconds() > kMsInSecond) + auto const sleepIfNeed = [&]() + { + // Greater than 1 second. + if (getElapsedMilliSeconds() > 1000) { drop(); return; @@ -210,7 +213,8 @@ void BuildRoutesWithApi(std::unique_ptr routingApi, size_t count = 0; size_t prevDumpedNumber = 0; - auto const dump = [&]() { + auto const dump = [&]() + { for (size_t i = 0; i < count; ++i) { std::string filepath = diff --git a/search/search_params.hpp b/search/search_params.hpp index f7070385d0..63323c21bb 100644 --- a/search/search_params.hpp +++ b/search/search_params.hpp @@ -21,18 +21,18 @@ class Tracer; struct SearchParams { - inline static size_t const kDefaultNumBookmarksResults = 1000; - inline static size_t const kDefaultBatchSizeEverywhere = 10; - inline static size_t const kDefaultNumResultsEverywhere = 30; - inline static size_t const kDefaultNumResultsInViewport = 200; - inline static size_t const kPreResultsCount = 200; - inline static double const kDefaultStreetSearchRadiusM = 8e4; - inline static double const kDefaultVillageSearchRadiusM = 2e5; - // TODO: Short timeouts leads to a non-working search on slow devices. Design a better solution. - inline static std::chrono::steady_clock::duration const kDefaultTimeout = - std::chrono::seconds(8); - inline static std::chrono::steady_clock::duration const kDefaultDesktopTimeout = - std::chrono::seconds(8); + static size_t constexpr kDefaultNumBookmarksResults = 1000; + static size_t constexpr kDefaultBatchSizeEverywhere = 10; + static size_t constexpr kDefaultNumResultsEverywhere = 30; + static size_t constexpr kDefaultNumResultsInViewport = 200; + static size_t constexpr kPreResultsCount = 200; + static double constexpr kDefaultStreetSearchRadiusM = 8e4; + static double constexpr kDefaultVillageSearchRadiusM = 2e5; + + using TimeDurationT = base::Timer::DurationT; + /// @todo Short timeouts leads to a non-working search on slow devices. Design a better solution. + static TimeDurationT constexpr kDefaultTimeout = std::chrono::seconds(8); + static TimeDurationT constexpr kDefaultDesktopTimeout = std::chrono::seconds(8); using OnStarted = std::function; using OnResults = std::function; @@ -95,7 +95,7 @@ struct SearchParams bookmarks::GroupId m_bookmarksGroupId = bookmarks::kInvalidGroupId; // Amount of time after which the search is aborted. - std::chrono::steady_clock::duration m_timeout = kDefaultTimeout; + TimeDurationT m_timeout = kDefaultTimeout; std::shared_ptr m_tracer; }; diff --git a/search/search_tests_support/test_search_request.cpp b/search/search_tests_support/test_search_request.cpp index 657ca7b99c..676a303069 100644 --- a/search/search_tests_support/test_search_request.cpp +++ b/search/search_tests_support/test_search_request.cpp @@ -60,7 +60,7 @@ void TestSearchRequest::Run() Wait(); } -steady_clock::duration TestSearchRequest::ResponseTime() const +TestSearchRequest::TimeDurationT TestSearchRequest::ResponseTime() const { lock_guard lock(m_mu); CHECK(m_done, ("This function may be called only when request is processed.")); diff --git a/search/search_tests_support/test_search_request.hpp b/search/search_tests_support/test_search_request.hpp index d5a92d0b61..5c5dca41a7 100644 --- a/search/search_tests_support/test_search_request.hpp +++ b/search/search_tests_support/test_search_request.hpp @@ -25,8 +25,8 @@ class TestSearchEngine; class TestSearchRequest { public: - inline static double const kDefaultTestStreetSearchRadiusM = 2e7; - inline static double const kDefaultTestVillageSearchRadiusM = 2e7; + static double constexpr kDefaultTestStreetSearchRadiusM = 2e7; + static double constexpr kDefaultTestVillageSearchRadiusM = 2e7; TestSearchRequest(TestSearchEngine & engine, std::string const & query, std::string const & locale, Mode mode, m2::RectD const & viewport); @@ -42,7 +42,8 @@ public: void Wait(); // Call these functions only after call to Wait(). - std::chrono::steady_clock::duration ResponseTime() const; + using TimeDurationT = base::Timer::DurationT; + TimeDurationT ResponseTime() const; std::vector const & Results() const; protected: @@ -67,8 +68,7 @@ protected: bool m_done = false; base::Timer m_timer; - std::chrono::steady_clock::duration m_startTime; - std::chrono::steady_clock::duration m_endTime; + TimeDurationT m_startTime, m_endTime; TestSearchEngine & m_engine; SearchParams m_params; diff --git a/testing/testingmain.cpp b/testing/testingmain.cpp index 810f78279e..8d32817eae 100644 --- a/testing/testingmain.cpp +++ b/testing/testingmain.cpp @@ -277,7 +277,7 @@ int main(int argc, char * argv[]) } g_lastTestOK = true; - uint64_t const elapsed = timer.ElapsedNano(); + uint64_t const elapsed = timer.ElapsedNanoseconds(); LOG(LINFO, ("Test took", elapsed / 1000000, "ms\n")); } diff --git a/tracking/tracking_tests/archival_reporter_tests.cpp b/tracking/tracking_tests/archival_reporter_tests.cpp index ae76c71e91..824e7bc29f 100644 --- a/tracking/tracking_tests/archival_reporter_tests.cpp +++ b/tracking/tracking_tests/archival_reporter_tests.cpp @@ -66,26 +66,27 @@ UNIT_TEST(PacketCar_OperationsConsistency) location::GpsInfo point = GetStartingPoint(); traffic::SpeedGroup sg = traffic::SpeedGroup::G0; - base::HighResTimer timerStart; + base::HighResTimer timer; + for (size_t i = 0; i < kItemsForDump; ++i) { archive.Add(point, sg); UpdateLocation(point); UpdateSpeedGroup(sg); } + auto const track = archive.Extract(); - base::HighResTimer timerStartSaving; + LOG(LINFO, ("Duration of dumping", timer.ElapsedMilliseconds(), "ms")); + + timer.Reset(); std::string const fileName = "archival_reporter_car.track"; { FileWriter writer(fileName); CHECK(archive.Write(writer), ()); } - LOG(LINFO, ("Duration of serializing", - timerStart.ElapsedMillis() - timerStartSaving.ElapsedMillis(), "ms")); - LOG(LINFO, - ("Duration of dumping", timerStart.ElapsedMillis() - timerStartSaving.ElapsedMillis(), "ms")); + LOG(LINFO, ("Duration of serializing", timer.ElapsedMilliseconds(), "ms")); uint64_t sizeBytes; CHECK(GetPlatform().GetFileSizeByFullPath(fileName, sizeBytes), ());