|
| 1 | +// |
| 2 | +// Copyright The OpenTelemetry Authors |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | + |
| 6 | +import OpenTelemetryApi |
| 7 | +@testable import OpenTelemetrySdk |
| 8 | +import XCTest |
| 9 | + |
| 10 | +class DoubleExplicitBucketHistogramAggregatorTests: XCTestCase { |
| 11 | + // Test the creation of a new handle |
| 12 | + func testCreateHandle() { |
| 13 | + let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [1.0, 2.0], reservoirSupplier: { ExemplarReservoir() }) |
| 14 | + let handle = aggregator.createHandle() as! DoubleExplicitBucketHistogramAggregator.Handle |
| 15 | + XCTAssertEqual(handle.count, 0) |
| 16 | + XCTAssertEqual(handle.sum, 0) |
| 17 | + XCTAssertEqual(handle.min, Double.greatestFiniteMagnitude) |
| 18 | + XCTAssertEqual(handle.max, -1) |
| 19 | + XCTAssertEqual(handle.counts, [0, 0, 0]) |
| 20 | + XCTAssertEqual(handle.boundaries, [1.0, 2.0]) |
| 21 | + } |
| 22 | + |
| 23 | + // Test the aggregation of double values |
| 24 | + func testDoRecordDouble() { |
| 25 | + let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [1.0, 2.0], reservoirSupplier: { ExemplarReservoir() }) |
| 26 | + let handle = aggregator.createHandle() as! DoubleExplicitBucketHistogramAggregator.Handle |
| 27 | + handle.recordDouble(value: 1.5) |
| 28 | + handle.recordDouble(value: 2.5) |
| 29 | + |
| 30 | + let pointData = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1, attributes: [:], reset: false) as! HistogramPointData |
| 31 | + |
| 32 | + XCTAssertEqual(pointData.sum, 4.0) |
| 33 | + XCTAssertEqual(pointData.count, 2) |
| 34 | + XCTAssertEqual(pointData.min, 1.5) |
| 35 | + XCTAssertEqual(pointData.max, 2.5) |
| 36 | + XCTAssertEqual(pointData.counts, [0, 1, 1]) |
| 37 | + XCTAssertEqual(pointData.boundaries, [1.0, 2.0]) |
| 38 | + XCTAssertEqual(pointData.hasMin, true) |
| 39 | + XCTAssertEqual(pointData.hasMax, true) |
| 40 | + } |
| 41 | + |
| 42 | + // Test the aggregation of long values |
| 43 | + func testDoRecordLong() { |
| 44 | + let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [1.0, 2.0], reservoirSupplier: { ExemplarReservoir() }) |
| 45 | + let handle = aggregator.createHandle() |
| 46 | + handle.recordLong(value: 1) |
| 47 | + handle.recordLong(value: 3) |
| 48 | + |
| 49 | + let pointData = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1, attributes: [:], reset: false) as! HistogramPointData |
| 50 | + |
| 51 | + XCTAssertEqual(pointData.sum, 4.0) |
| 52 | + XCTAssertEqual(pointData.count, 2) |
| 53 | + XCTAssertEqual(pointData.min, 1.0) |
| 54 | + XCTAssertEqual(pointData.max, 3.0) |
| 55 | + XCTAssertEqual(pointData.counts, [1, 0, 1]) |
| 56 | + XCTAssertEqual(pointData.boundaries, [1.0, 2.0]) |
| 57 | + XCTAssertEqual(pointData.hasMin, true) |
| 58 | + XCTAssertEqual(pointData.hasMax, true) |
| 59 | + } |
| 60 | + |
| 61 | + // Test the aggregation of double values and reset of the handle |
| 62 | + func testAggregateThenMaybeReset() { |
| 63 | + let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [0.5, 1.0, 2.0], reservoirSupplier: { NoopExemplarReservoir() }) |
| 64 | + let handle = aggregator.createHandle() |
| 65 | + handle.recordDouble(value: 1.5) |
| 66 | + handle.recordDouble(value: 0.25) |
| 67 | + handle.recordDouble(value: 0.75) |
| 68 | + |
| 69 | + // Aggregate without resetting |
| 70 | + let pointData = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1000, attributes: [:], reset: false) as! HistogramPointData |
| 71 | + XCTAssertEqual(pointData.sum, 2.5) |
| 72 | + XCTAssertEqual(pointData.count, 3) |
| 73 | + XCTAssertEqual(pointData.min, 0.25) |
| 74 | + XCTAssertEqual(pointData.max, 1.5) |
| 75 | + XCTAssertEqual(pointData.counts, [1, 1, 1, 0]) |
| 76 | + |
| 77 | + // Aggregate and reset |
| 78 | + let pointData2 = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1000, attributes: [:], reset: true) as! HistogramPointData |
| 79 | + XCTAssertEqual(pointData2.sum, 2.5) |
| 80 | + XCTAssertEqual(pointData2.count, 3) |
| 81 | + XCTAssertEqual(pointData2.min, 0.25) |
| 82 | + XCTAssertEqual(pointData2.max, 1.5) |
| 83 | + XCTAssertEqual(pointData2.counts, [1, 1, 1, 0]) |
| 84 | + |
| 85 | + // Aggregate after reset |
| 86 | + let pointData3 = handle.aggregateThenMaybeReset(startEpochNano: 0, endEpochNano: 1000, attributes: [:], reset: false) as! HistogramPointData |
| 87 | + XCTAssertEqual(pointData3.sum, 0) |
| 88 | + XCTAssertEqual(pointData3.count, 0) |
| 89 | + XCTAssertEqual(pointData3.min, Double.greatestFiniteMagnitude) |
| 90 | + XCTAssertEqual(pointData3.max, -1) |
| 91 | + XCTAssertEqual(pointData3.counts, [0, 0, 0, 0]) |
| 92 | + } |
| 93 | + |
| 94 | + func testDiff() { |
| 95 | + // Initialize aggregator |
| 96 | + let aggregator = DoubleExplicitBucketHistogramAggregator(boundaries: [1.0, 2.0, 3.0], reservoirSupplier: { NoopExemplarReservoir() }) |
| 97 | + |
| 98 | + // Create some dummy PointData objects |
| 99 | + let previousCumulative = HistogramPointData(startEpochNanos: 0, endEpochNanos: 100, attributes: [:], exemplars: [], sum: 10.0, count: 1, min: 5.0, max: 5.0, boundaries: [], counts: [], hasMin: true, hasMax: true) |
| 100 | + let currentCumulative = HistogramPointData(startEpochNanos: 0, endEpochNanos: 200, attributes: [:], exemplars: [], sum: 20.0, count: 2, min: 3.0, max: 7.0, boundaries: [], counts: [], hasMin: true, hasMax: true) |
| 101 | + |
| 102 | + // Verify that the diff() method throws the expected error |
| 103 | + XCTAssertThrowsError(try aggregator.diff(previousCumulative: previousCumulative, currentCumulative: currentCumulative)) |
| 104 | + } |
| 105 | +} |
0 commit comments