Skip to content

Commit 8e5110d

Browse files
Mordiltomerd
authored andcommitted
Add Timer.recordNanoseconds Generic Overload (#28) (#30)
Motivation: As discussed in Issue #28, there are a few preferred lightweight APIs for getting timestamps such as `DispatchTime` but those APIs return `UInt64` types, which need to be handled for overflow. Modifications: Added a generic `BinaryInteger` overload method for `recordNanoseconds` that guards against accidental overflows from `UInt64` to `Int64` Result: Users now have access to a new method to record nanosecond measurements with `Timer` that guards against overflows.
1 parent e4883da commit 8e5110d

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

Sources/CoreMetrics/Metrics.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@ public class Timer {
267267
self.handler.recordNanoseconds(duration)
268268
}
269269

270+
/// Record a duration in nanoseconds.
271+
///
272+
/// - parameters:
273+
/// - value: Duration to record.
274+
@inlinable
275+
public func recordNanoseconds<DataType: BinaryInteger>(_ duration: DataType) {
276+
self.recordNanoseconds(duration >= Int64.max ? Int64.max : Int64(duration))
277+
}
278+
270279
/// Record a duration in microseconds.
271280
///
272281
/// - parameters:

Sources/Metrics/Metrics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public extension Timer {
5252
func record(_ duration: DispatchTimeInterval) {
5353
switch duration {
5454
case .nanoseconds(let value):
55-
self.recordNanoseconds(Int64(value))
55+
self.recordNanoseconds(value)
5656
case .microseconds(let value):
5757
self.recordMicroseconds(value)
5858
case .milliseconds(let value):

Tests/MetricsTests/CoreMetricsTests.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,22 @@ class MetricsTests: XCTestCase {
224224
// run the test
225225
let timer = Timer(label: "test-timer")
226226
let testTimer = timer.handler as! TestTimer
227-
// micro
228-
timer.recordMicroseconds(UInt64.max)
227+
// nano
228+
timer.recordNanoseconds(UInt64.max)
229229
XCTAssertEqual(testTimer.values.count, 1, "expected number of entries to match")
230230
XCTAssertEqual(testTimer.values[0].1, Int64.max, "expected value to match")
231-
// milli
232-
timer.recordMilliseconds(UInt64.max)
231+
// micro
232+
timer.recordMicroseconds(UInt64.max)
233233
XCTAssertEqual(testTimer.values.count, 2, "expected number of entries to match")
234234
XCTAssertEqual(testTimer.values[1].1, Int64.max, "expected value to match")
235-
// seconds
236-
timer.recordSeconds(UInt64.max)
235+
// milli
236+
timer.recordMilliseconds(UInt64.max)
237237
XCTAssertEqual(testTimer.values.count, 3, "expected number of entries to match")
238238
XCTAssertEqual(testTimer.values[2].1, Int64.max, "expected value to match")
239+
// seconds
240+
timer.recordSeconds(UInt64.max)
241+
XCTAssertEqual(testTimer.values.count, 4, "expected number of entries to match")
242+
XCTAssertEqual(testTimer.values[3].1, Int64.max, "expected value to match")
239243
}
240244

241245
func testGauge() throws {

0 commit comments

Comments
 (0)