Adding Timer.record(_ duration: Duration)
Motivation: `Duration` is available starting Swift 5.7, and we should probably support it in our convinience API. Modifications: This commit adds: - Timer.record(_ duration: Duration) implementation - A unit test case - Generated Linux tests
This commit is contained in:
parent
971ba26378
commit
37ae7aa983
|
|
@ -74,3 +74,16 @@ extension Timer {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Timer {
|
||||
/// Convenience for recording a duration based on ``Duration``.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - duration: The duration to record.
|
||||
@available(macOS 13, iOS 16, tvOS 15, watchOS 8, *)
|
||||
@inlinable
|
||||
public func record(_ duration: Duration) {
|
||||
let durationSeconds = Double(duration.components.seconds) + Double(duration.components.attoseconds) / 1e18
|
||||
self.recordSeconds(durationSeconds)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ extension MetricsExtensionsTests {
|
|||
("testTimerWithTimeInterval", testTimerWithTimeInterval),
|
||||
("testTimerWithDispatchTime", testTimerWithDispatchTime),
|
||||
("testTimerWithDispatchTimeInterval", testTimerWithDispatchTimeInterval),
|
||||
("testTimerDuration", testTimerDuration),
|
||||
("testTimerUnits", testTimerUnits),
|
||||
("testPreferDisplayUnit", testPreferDisplayUnit),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -95,6 +95,28 @@ class MetricsExtensionsTests: XCTestCase {
|
|||
XCTAssertEqual(UInt64(testTimer.values.first!), end.uptimeNanoseconds - start.uptimeNanoseconds, "expected value to match")
|
||||
XCTAssertEqual(metrics.timers.count, 1, "timer should have been stored")
|
||||
}
|
||||
|
||||
func testTimerDuration() throws {
|
||||
guard #available(iOS 16, macOS 13, tvOS 15, watchOS 8, *) else {
|
||||
return
|
||||
}
|
||||
|
||||
let metrics = TestMetrics()
|
||||
MetricsSystem.bootstrapInternal(metrics)
|
||||
|
||||
let name = "timer-\(UUID().uuidString)"
|
||||
let duration = Duration(secondsComponent: 3, attosecondsComponent: 123000000000000000)
|
||||
let durationInNanoseconds = duration.components.seconds * 1_000_000_000 + duration.components.attoseconds / 1_000_000_000
|
||||
|
||||
let timer = Timer(label: name)
|
||||
timer.record(duration)
|
||||
|
||||
let testTimer = try metrics.expectTimer(timer)
|
||||
XCTAssertEqual(testTimer.values.count, 1, "expected number of entries to match")
|
||||
XCTAssertEqual(testTimer.values.first, durationInNanoseconds, "expected value to match")
|
||||
XCTAssertEqual(metrics.timers.count, 1, "timer should have been stored")
|
||||
}
|
||||
|
||||
|
||||
func testTimerUnits() throws {
|
||||
let metrics = TestMetrics()
|
||||
|
|
|
|||
Loading…
Reference in New Issue