diff options
author | Chris Wilson <chris+github@qwirx.com> | 2017-05-11 20:50:26 +0100 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2017-06-22 20:28:06 +0100 |
commit | 809d9ebacc409c4a86edb6077f489da67a2653d4 (patch) | |
tree | 0d2c3bb4422a5362f2617f916a94c8430b0b4996 | |
parent | 105f2620b98bfd4e5d7ed576b5cc4b2317dcf634 (diff) |
Increase precision of timing on Windows.
Should hopefully reduce random failures of timing-dependent tests which
rely on subsecond precision for accuracy.
(cherry picked from commit c845b2e39fffeb560983a301d810616a6495469d)
-rw-r--r-- | lib/common/BoxTime.cpp | 39 | ||||
-rw-r--r-- | test/bbackupd/testbbackupd.cpp | 3 |
2 files changed, 26 insertions, 16 deletions
diff --git a/lib/common/BoxTime.cpp b/lib/common/BoxTime.cpp index 78269def..77daae6d 100644 --- a/lib/common/BoxTime.cpp +++ b/lib/common/BoxTime.cpp @@ -35,21 +35,30 @@ // -------------------------------------------------------------------------- box_time_t GetCurrentBoxTime() { - #ifdef HAVE_GETTIMEOFDAY - struct timeval tv; - if (gettimeofday(&tv, NULL) != 0) - { - BOX_LOG_SYS_ERROR("Failed to gettimeofday(), " - "dropping precision"); - } - else - { - box_time_t timeNow = (tv.tv_sec * MICRO_SEC_IN_SEC_LL) - + tv.tv_usec; - return timeNow; - } - #endif - +#ifdef HAVE_GETTIMEOFDAY + struct timeval tv; + if (gettimeofday(&tv, NULL) != 0) + { + BOX_LOG_SYS_ERROR("Failed to gettimeofday(), " + "dropping precision"); + } + else + { + box_time_t time_now = (tv.tv_sec * MICRO_SEC_IN_SEC_LL) + tv.tv_usec; + return time_now; + } +#elif WIN32 + // There's no Win32 API function that returns the current time as a UNIX timestamp with + // sub-second precision. So we use time(0) and add the fractional part from + // GetSystemTime() in the hope that the difference between these two (if any) is a whole + // number of seconds. + box_time_t time_now = SecondsToBoxTime(time(0)); + SYSTEMTIME system_time; + GetSystemTime(&system_time); + time_now += MilliSecondsToBoxTime(system_time.wMilliseconds); + return time_now; +#endif + return SecondsToBoxTime(time(0)); } diff --git a/test/bbackupd/testbbackupd.cpp b/test/bbackupd/testbbackupd.cpp index 7113880d..cc602f22 100644 --- a/test/bbackupd/testbbackupd.cpp +++ b/test/bbackupd/testbbackupd.cpp @@ -3675,6 +3675,7 @@ bool test_changing_client_store_marker_pauses_daemon() box_time_t sync_start_time = GetCurrentBoxTime(); sync_and_wait(); box_time_t sync_time = GetCurrentBoxTime() - sync_start_time; + BOX_INFO("Sync takes " << BOX_FORMAT_MICROSECONDS(sync_time)); // Time how long a compare takes. On NetBSD it's 3 seconds, and that // interferes with test timing unless we account for it. @@ -3682,7 +3683,7 @@ bool test_changing_client_store_marker_pauses_daemon() // There should be no differences right now (yet). TEST_COMPARE(Compare_Same); box_time_t compare_time = GetCurrentBoxTime() - compare_start_time; - BOX_TRACE("Compare takes " << BOX_FORMAT_MICROSECONDS(compare_time)); + BOX_INFO("Compare takes " << BOX_FORMAT_MICROSECONDS(compare_time)); // Wait for the end of another sync, to give us ~3 seconds to change // the client store marker. |