Skip to content

Commit

Permalink
add increment and decrement public API to Meter (#132)
Browse files Browse the repository at this point in the history
motivation: expose increment and decrement public APIs

changes:
* add increment and decrement public API to Meter
* improve the implementation of AccumulatingMeter and TestMeter
* add tests for new APIs
* refactor/modernize other tests
  • Loading branch information
tomerd committed Jun 21, 2023
1 parent bf7ea93 commit 971ba26
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 61 deletions.
66 changes: 66 additions & 0 deletions Sources/CoreMetrics/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,36 @@ public final class Meter {
public func set<DataType: BinaryFloatingPoint>(_ value: DataType) {
self._handler.set(Double(value))
}

/// Increment the Meter.
///
/// - parameters:
/// - by: Amount to increment by.
@inlinable
public func increment<DataType: BinaryFloatingPoint>(by amount: DataType) {
self._handler.increment(by: Double(amount))
}

/// Increment the Meter by one.
@inlinable
public func increment() {
self.increment(by: 1.0)
}

/// Decrement the Meter.
///
/// - parameters:
/// - by: Amount to decrement by.
@inlinable
public func decrement<DataType: BinaryFloatingPoint>(by amount: DataType) {
self._handler.decrement(by: Double(amount))
}

/// Decrement the Meter by one.
@inlinable
public func decrement() {
self.decrement(by: 1.0)
}
}

extension Meter {
Expand Down Expand Up @@ -787,6 +817,24 @@ internal final class AccumulatingMeter: MeterHandler {
}

func increment(by amount: Double) {
// Drop illegal values
// - cannot increment by NaN
guard !amount.isNaN else {
return
}
// - cannot increment by infinite quantities
guard !amount.isInfinite else {
return
}
// - cannot increment by negative values
guard amount.sign == .plus else {
return
}
// - cannot increment by zero
guard !amount.isZero else {
return
}

let newValue: Double = self.lock.withLock {
self.value += amount
return self.value
Expand All @@ -795,6 +843,24 @@ internal final class AccumulatingMeter: MeterHandler {
}

func decrement(by amount: Double) {
// Drop illegal values
// - cannot decrement by NaN
guard !amount.isNaN else {
return
}
// - cannot decrement by infinite quantities
guard !amount.isInfinite else {
return
}
// - cannot decrement by negative values
guard amount.sign == .plus else {
return
}
// - cannot decrement by zero
guard !amount.isZero else {
return
}

let newValue: Double = self.lock.withLock {
self.value -= amount
return self.value
Expand Down
52 changes: 44 additions & 8 deletions Sources/MetricsTestKit/TestMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class TestMetrics: MetricsFactory {
// nothing to do
}

/// Reset method to destroy all created ``TestCounter``, ``TestRecorder`` and ``TestTimer``.
/// Reset method to destroy all created ``TestCounter``, ``TestMeter``, ``TestRecorder`` and ``TestTimer``.
/// Invoke this method in between test runs to verify that Counters are created as needed.
public func reset() {
self.lock.withLock {
Expand Down Expand Up @@ -361,7 +361,7 @@ public final class TestMeter: TestMetric, MeterHandler, Equatable {
private var _values = [(Date, Double)]()

init(label: String, dimensions: [(String, String)]) {
self.id = NSUUID().uuidString
self.id = UUID().uuidString
self.label = label
self.dimensions = dimensions
}
Expand All @@ -378,18 +378,54 @@ public final class TestMeter: TestMetric, MeterHandler, Equatable {
}

public func increment(by amount: Double) {
// Drop illegal values
// - cannot increment by NaN
guard !amount.isNaN else {
return
}
// - cannot increment by infinite quantities
guard !amount.isInfinite else {
return
}
// - cannot increment by negative values
guard amount.sign == .plus else {
return
}
// - cannot increment by zero
guard !amount.isZero else {
return
}

self.lock.withLock {
let lastValue = self._values.last?.1 ?? 0
let newValue = lastValue - amount
_values.append((Date(), Double(newValue)))
let lastValue: Double = self._values.last?.1 ?? 0
let newValue = lastValue + amount
_values.append((Date(), newValue))
}
}

public func decrement(by amount: Double) {
// Drop illegal values
// - cannot decrement by NaN
guard !amount.isNaN else {
return
}
// - cannot decrement by infinite quantities
guard !amount.isInfinite else {
return
}
// - cannot decrement by negative values
guard amount.sign == .plus else {
return
}
// - cannot decrement by zero
guard !amount.isZero else {
return
}

self.lock.withLock {
let lastValue = self._values.last?.1 ?? 0
let lastValue: Double = self._values.last?.1 ?? 0
let newValue = lastValue - amount
_values.append((Date(), Double(newValue)))
_values.append((Date(), newValue))
}
}

Expand Down Expand Up @@ -428,7 +464,7 @@ public final class TestRecorder: TestMetric, RecorderHandler, Equatable {
private var _values = [(Date, Double)]()

init(label: String, dimensions: [(String, String)], aggregate: Bool) {
self.id = NSUUID().uuidString
self.id = UUID().uuidString
self.label = label
self.dimensions = dimensions
self.aggregate = aggregate
Expand Down
8 changes: 8 additions & 0 deletions Tests/MetricsTests/CoreMetricsTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ extension MetricsTests {
("testGaugeBlock", testGaugeBlock),
("testMeter", testMeter),
("testMeterBlock", testMeterBlock),
("testMeterInt", testMeterInt),
("testMeterFloat", testMeterFloat),
("testMeterIncrement", testMeterIncrement),
("testMeterDecrement", testMeterDecrement),
("testDefaultMeterIgnoresNan", testDefaultMeterIgnoresNan),
("testDefaultMeterIgnoresInfinity", testDefaultMeterIgnoresInfinity),
("testDefaultMeterIgnoresNegativeValues", testDefaultMeterIgnoresNegativeValues),
("testDefaultMeterIgnoresZero", testDefaultMeterIgnoresZero),
("testMUX_Counter", testMUX_Counter),
("testMUX_Meter", testMUX_Meter),
("testMUX_Recorder", testMUX_Recorder),
Expand Down

0 comments on commit 971ba26

Please sign in to comment.