mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
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.
This commit is contained in:
1 parent
8c3cac7774
commit
fca6ecfdb0
1 file changed
+3
-3
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user