Add Timer.recordInterval method (#83)
* Add Timer.recordInterval method Co-authored-by: Konrad `ktoso` Malawski <ktoso@apple.com>
This commit is contained in:
parent
5702ee1174
commit
44e8bfc7f5
|
|
@ -12,6 +12,8 @@
|
||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
import Dispatch
|
||||||
|
|
||||||
// MARK: User API
|
// MARK: User API
|
||||||
|
|
||||||
extension Counter {
|
extension Counter {
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,15 @@ public extension Timer {
|
||||||
}
|
}
|
||||||
return try body()
|
return try body()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Record the time interval (with nanosecond precision) between the passed `since` dispatch time and `end` dispatch time.
|
||||||
|
///
|
||||||
|
/// - parameters:
|
||||||
|
/// - since: Start of the interval as `DispatchTime`.
|
||||||
|
/// - end: End of the interval, defaulting to `.now()`.
|
||||||
|
func recordInterval(since: DispatchTime, end: DispatchTime = .now()) {
|
||||||
|
self.recordNanoseconds(end.uptimeNanoseconds - since.uptimeNanoseconds)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension Timer {
|
public extension Timer {
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ extension MetricsExtensionsTests {
|
||||||
("testTimerBlock", testTimerBlock),
|
("testTimerBlock", testTimerBlock),
|
||||||
("testTimerWithTimeInterval", testTimerWithTimeInterval),
|
("testTimerWithTimeInterval", testTimerWithTimeInterval),
|
||||||
("testTimerWithDispatchTime", testTimerWithDispatchTime),
|
("testTimerWithDispatchTime", testTimerWithDispatchTime),
|
||||||
|
("testTimerWithDispatchTimeInterval", testTimerWithDispatchTimeInterval),
|
||||||
("testTimerUnits", testTimerUnits),
|
("testTimerUnits", testTimerUnits),
|
||||||
("testPreferDisplayUnit", testPreferDisplayUnit),
|
("testPreferDisplayUnit", testPreferDisplayUnit),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,23 @@ class MetricsExtensionsTests: XCTestCase {
|
||||||
XCTAssertEqual(testTimer.values[4].1, 0, "expected value to match")
|
XCTAssertEqual(testTimer.values[4].1, 0, "expected value to match")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testTimerWithDispatchTimeInterval() {
|
||||||
|
let metrics = TestMetrics()
|
||||||
|
MetricsSystem.bootstrapInternal(metrics)
|
||||||
|
|
||||||
|
let name = "timer-\(UUID().uuidString)"
|
||||||
|
|
||||||
|
let timer = Timer(label: name)
|
||||||
|
let start = DispatchTime.now()
|
||||||
|
let end = DispatchTime(uptimeNanoseconds: start.uptimeNanoseconds + 1000 * 1000 * 1000)
|
||||||
|
timer.recordInterval(since: start, end: end)
|
||||||
|
|
||||||
|
let testTimer = timer.handler as! TestTimer
|
||||||
|
XCTAssertEqual(testTimer.values.count, 1, "expected number of entries to match")
|
||||||
|
XCTAssertEqual(UInt64(testTimer.values.first!.1), end.uptimeNanoseconds - start.uptimeNanoseconds, "expected value to match")
|
||||||
|
XCTAssertEqual(metrics.timers.count, 1, "timer should have been stored")
|
||||||
|
}
|
||||||
|
|
||||||
func testTimerUnits() throws {
|
func testTimerUnits() throws {
|
||||||
let metrics = TestMetrics()
|
let metrics = TestMetrics()
|
||||||
MetricsSystem.bootstrapInternal(metrics)
|
MetricsSystem.bootstrapInternal(metrics)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue