Skip to content

Commit 44e8bfc

Browse files
slashmoktoso
andauthored
Add Timer.recordInterval method (#83)
* Add Timer.recordInterval method Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
1 parent 5702ee1 commit 44e8bfc

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

Sources/CoreMetrics/Metrics.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import Dispatch
16+
1517
// MARK: User API
1618

1719
extension Counter {

Sources/Metrics/Metrics.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ public extension Timer {
3333
}
3434
return try body()
3535
}
36+
37+
/// Record the time interval (with nanosecond precision) between the passed `since` dispatch time and `end` dispatch time.
38+
///
39+
/// - parameters:
40+
/// - since: Start of the interval as `DispatchTime`.
41+
/// - end: End of the interval, defaulting to `.now()`.
42+
func recordInterval(since: DispatchTime, end: DispatchTime = .now()) {
43+
self.recordNanoseconds(end.uptimeNanoseconds - since.uptimeNanoseconds)
44+
}
3645
}
3746

3847
public extension Timer {

Tests/MetricsTests/MetricsTests+XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extension MetricsExtensionsTests {
2828
("testTimerBlock", testTimerBlock),
2929
("testTimerWithTimeInterval", testTimerWithTimeInterval),
3030
("testTimerWithDispatchTime", testTimerWithDispatchTime),
31+
("testTimerWithDispatchTimeInterval", testTimerWithDispatchTimeInterval),
3132
("testTimerUnits", testTimerUnits),
3233
("testPreferDisplayUnit", testPreferDisplayUnit),
3334
]

Tests/MetricsTests/MetricsTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ class MetricsExtensionsTests: XCTestCase {
7878
XCTAssertEqual(testTimer.values[4].1, 0, "expected value to match")
7979
}
8080

81+
func testTimerWithDispatchTimeInterval() {
82+
let metrics = TestMetrics()
83+
MetricsSystem.bootstrapInternal(metrics)
84+
85+
let name = "timer-\(UUID().uuidString)"
86+
87+
let timer = Timer(label: name)
88+
let start = DispatchTime.now()
89+
let end = DispatchTime(uptimeNanoseconds: start.uptimeNanoseconds + 1000 * 1000 * 1000)
90+
timer.recordInterval(since: start, end: end)
91+
92+
let testTimer = timer.handler as! TestTimer
93+
XCTAssertEqual(testTimer.values.count, 1, "expected number of entries to match")
94+
XCTAssertEqual(UInt64(testTimer.values.first!.1), end.uptimeNanoseconds - start.uptimeNanoseconds, "expected value to match")
95+
XCTAssertEqual(metrics.timers.count, 1, "timer should have been stored")
96+
}
97+
8198
func testTimerUnits() throws {
8299
let metrics = TestMetrics()
83100
MetricsSystem.bootstrapInternal(metrics)

0 commit comments

Comments
 (0)