|
| 1 | +import XCTest |
| 2 | +import NIO |
| 3 | +@testable import Prometheus |
| 4 | +@testable import CoreMetrics |
| 5 | + |
| 6 | +final class SummaryTests: XCTestCase { |
| 7 | + struct BaseSummaryLabels: SummaryLabels { |
| 8 | + var quantile: String = "" |
| 9 | + let myValue: String |
| 10 | + |
| 11 | + init() { |
| 12 | + self.myValue = "*" |
| 13 | + } |
| 14 | + |
| 15 | + init(myValue: String) { |
| 16 | + self.myValue = myValue |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + var prom: PrometheusClient! |
| 21 | + var group: EventLoopGroup! |
| 22 | + var eventLoop: EventLoop { |
| 23 | + return group.next() |
| 24 | + } |
| 25 | + |
| 26 | + override func setUp() { |
| 27 | + self.prom = PrometheusClient() |
| 28 | + self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 29 | + MetricsSystem.bootstrapInternal(prom) |
| 30 | + } |
| 31 | + |
| 32 | + override func tearDown() { |
| 33 | + self.prom = nil |
| 34 | + try! self.group.syncShutdownGracefully() |
| 35 | + } |
| 36 | + |
| 37 | + func testSummary() { |
| 38 | + let summary = Timer(label: "my_summary") |
| 39 | + |
| 40 | + summary.recordNanoseconds(1) |
| 41 | + summary.recordNanoseconds(2) |
| 42 | + summary.recordNanoseconds(4) |
| 43 | + summary.recordNanoseconds(10000) |
| 44 | + |
| 45 | + let summaryTwo = Timer(label: "my_summary", dimensions: [("myValue", "labels")]) |
| 46 | + summaryTwo.recordNanoseconds(123) |
| 47 | + |
| 48 | + let promise = self.eventLoop.makePromise(of: String.self) |
| 49 | + prom.collect(promise.succeed) |
| 50 | + |
| 51 | + XCTAssertEqual(try! promise.futureResult.wait(), """ |
| 52 | + # TYPE my_summary summary |
| 53 | + my_summary{quantile="0.01"} 1.0 |
| 54 | + my_summary{quantile="0.05"} 1.0 |
| 55 | + my_summary{quantile="0.5"} 4.0 |
| 56 | + my_summary{quantile="0.9"} 10000.0 |
| 57 | + my_summary{quantile="0.95"} 10000.0 |
| 58 | + my_summary{quantile="0.99"} 10000.0 |
| 59 | + my_summary{quantile="0.999"} 10000.0 |
| 60 | + my_summary_count 5 |
| 61 | + my_summary_sum 10130.0 |
| 62 | + my_summary{quantile="0.01", myValue="labels"} 123.0 |
| 63 | + my_summary{quantile="0.05", myValue="labels"} 123.0 |
| 64 | + my_summary{quantile="0.5", myValue="labels"} 123.0 |
| 65 | + my_summary{quantile="0.9", myValue="labels"} 123.0 |
| 66 | + my_summary{quantile="0.95", myValue="labels"} 123.0 |
| 67 | + my_summary{quantile="0.99", myValue="labels"} 123.0 |
| 68 | + my_summary{quantile="0.999", myValue="labels"} 123.0 |
| 69 | + my_summary_count{myValue="labels"} 1 |
| 70 | + my_summary_sum{myValue="labels"} 123.0 |
| 71 | + """) |
| 72 | + } |
| 73 | + |
| 74 | + func testSummaryWithPreferredDisplayUnit() { |
| 75 | + let summary = Timer(label: "my_summary", preferredDisplayUnit: .seconds) |
| 76 | + |
| 77 | + summary.recordSeconds(1) |
| 78 | + summary.recordMilliseconds(2 * 1_000) |
| 79 | + summary.recordNanoseconds(4 * 1_000_000_000) |
| 80 | + summary.recordSeconds(10000) |
| 81 | + |
| 82 | + let promise = self.eventLoop.makePromise(of: String.self) |
| 83 | + prom.collect(promise.succeed) |
| 84 | + |
| 85 | + XCTAssertEqual(try! promise.futureResult.wait(), """ |
| 86 | + # TYPE my_summary summary |
| 87 | + my_summary{quantile="0.01"} 1.0 |
| 88 | + my_summary{quantile="0.05"} 1.0 |
| 89 | + my_summary{quantile="0.5"} 3.0 |
| 90 | + my_summary{quantile="0.9"} 10000.0 |
| 91 | + my_summary{quantile="0.95"} 10000.0 |
| 92 | + my_summary{quantile="0.99"} 10000.0 |
| 93 | + my_summary{quantile="0.999"} 10000.0 |
| 94 | + my_summary_count 4 |
| 95 | + my_summary_sum 10007.0 |
| 96 | + """) |
| 97 | + } |
| 98 | + |
| 99 | + func testSummaryTime() { |
| 100 | + let summary = prom.createSummary(forType: Double.self, named: "my_summary", helpText: "Summary for testing", quantiles: [0.5, 0.9, 0.99], labels: BaseSummaryLabels.self) |
| 101 | + let delay = 0.05 |
| 102 | + summary.time { |
| 103 | + Thread.sleep(forTimeInterval: delay) |
| 104 | + } |
| 105 | + // This setup checks `.startsWith` on a per-line basis |
| 106 | + // to prevent issues with subsecond differences per test run |
| 107 | + let lines = [ |
| 108 | + "# HELP my_summary Summary for testing", |
| 109 | + "# TYPE my_summary summary", |
| 110 | + #"my_summary{quantile="0.5", myValue="*"} 0.05"#, |
| 111 | + #"my_summary{quantile="0.9", myValue="*"} 0.05"#, |
| 112 | + #"my_summary{quantile="0.99", myValue="*"} 0.05"#, |
| 113 | + #"my_summary_count{myValue="*"} 1.0"#, |
| 114 | + #"my_summary_sum{myValue="*"} 0.05"# |
| 115 | + ] |
| 116 | + let sections = summary.collect().split(separator: "\n").map(String.init).enumerated().map { i, s in s.starts(with: lines[i]) } |
| 117 | + XCTAssert(sections.filter { !$0 }.isEmpty) |
| 118 | + } |
| 119 | + |
| 120 | + func testSummaryStandalone() { |
| 121 | + let summary = prom.createSummary(forType: Double.self, named: "my_summary", helpText: "Summary for testing", quantiles: [0.5, 0.9, 0.99], labels: BaseSummaryLabels.self) |
| 122 | + let summaryTwo = prom.createSummary(forType: Double.self, named: "my_summary", helpText: "Summary for testing", quantiles: [0.5, 0.9, 0.99], labels: BaseSummaryLabels.self) |
| 123 | + |
| 124 | + summary.observe(1) |
| 125 | + summary.observe(2) |
| 126 | + summary.observe(4) |
| 127 | + summaryTwo.observe(10000) |
| 128 | + |
| 129 | + summary.observe(123, .init(myValue: "labels")) |
| 130 | + |
| 131 | + XCTAssertEqual(summary.collect(), """ |
| 132 | + # HELP my_summary Summary for testing |
| 133 | + # TYPE my_summary summary |
| 134 | + my_summary{quantile=\"0.5\", myValue=\"*\"} 4.0 |
| 135 | + my_summary{quantile=\"0.9\", myValue=\"*\"} 10000.0 |
| 136 | + my_summary{quantile=\"0.99\", myValue=\"*\"} 10000.0 |
| 137 | + my_summary_count{myValue=\"*\"} 5.0 |
| 138 | + my_summary_sum{myValue=\"*\"} 10130.0 |
| 139 | + my_summary{quantile=\"0.5\", myValue=\"labels\"} 123.0 |
| 140 | + my_summary{quantile=\"0.9\", myValue=\"labels\"} 123.0 |
| 141 | + my_summary{quantile=\"0.99\", myValue=\"labels\"} 123.0 |
| 142 | + my_summary_count{myValue=\"labels\"} 1.0 |
| 143 | + my_summary_sum{myValue=\"labels\"} 123.0 |
| 144 | + """) |
| 145 | + } |
| 146 | +} |
0 commit comments