Skip to content

Commit

Permalink
Add Timer.recordInterval method (#83)
Browse files Browse the repository at this point in the history
* Add Timer.recordInterval method

Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
  • Loading branch information
slashmo and ktoso authored Oct 12, 2020
1 parent 5702ee1 commit 44e8bfc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/CoreMetrics/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//
//===----------------------------------------------------------------------===//

import Dispatch

// MARK: User API

extension Counter {
Expand Down
9 changes: 9 additions & 0 deletions Sources/Metrics/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public extension Timer {
}
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 {
Expand Down
1 change: 1 addition & 0 deletions Tests/MetricsTests/MetricsTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension MetricsExtensionsTests {
("testTimerBlock", testTimerBlock),
("testTimerWithTimeInterval", testTimerWithTimeInterval),
("testTimerWithDispatchTime", testTimerWithDispatchTime),
("testTimerWithDispatchTimeInterval", testTimerWithDispatchTimeInterval),
("testTimerUnits", testTimerUnits),
("testPreferDisplayUnit", testPreferDisplayUnit),
]
Expand Down
17 changes: 17 additions & 0 deletions Tests/MetricsTests/MetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ class MetricsExtensionsTests: XCTestCase {
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 {
let metrics = TestMetrics()
MetricsSystem.bootstrapInternal(metrics)
Expand Down

0 comments on commit 44e8bfc

Please sign in to comment.