From fca6ecfdb07991794598d2aed343272553ecfc4f Mon Sep 17 00:00:00 2001 From: Rick Newton-Rogers Date: Wed, 11 Oct 2023 10:58:16 +0100 Subject: [PATCH] measureRunTime use DispatchTime (#2545) Motivation: Using `Date()` in benchmarks leaves us open to the possibility that a clock update could occur between the start and end times affecting the measured interval. Modifications: Switch `measureRunTime` and related functions to use `DispatchTime` uptime in nanoseconds to calculate intervals Result: Tests should no longer be susceptible to clock updates. --- Tests/NIOPosixTests/SystemCallWrapperHelpers.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/NIOPosixTests/SystemCallWrapperHelpers.swift b/Tests/NIOPosixTests/SystemCallWrapperHelpers.swift index ae1ed7ac1..24b208658 100644 --- a/Tests/NIOPosixTests/SystemCallWrapperHelpers.swift +++ b/Tests/NIOPosixTests/SystemCallWrapperHelpers.swift @@ -19,10 +19,10 @@ import Foundation public func measureRunTime(_ body: () throws -> Int) rethrows -> TimeInterval { func measureOne(_ body: () throws -> Int) rethrows -> TimeInterval { - let start = Date() + let start = DispatchTime.now().uptimeNanoseconds _ = try body() - let end = Date() - return end.timeIntervalSince(start) + let end = DispatchTime.now().uptimeNanoseconds + return Double(end - start)/1_000_000 } _ = try measureOne(body)