|
| 1 | +// swift-tools-version:5.7 |
| 2 | +//===----------------------------------------------------------------------===// |
| 3 | +// |
| 4 | +// This source file is part of the SwiftPrometheus open source project |
| 5 | +// |
| 6 | +// Copyright (c) 2018-2023 SwiftPrometheus project authors |
| 7 | +// Licensed under Apache License v2.0 |
| 8 | +// |
| 9 | +// See LICENSE.txt for license information |
| 10 | +// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors |
| 11 | +// |
| 12 | +// SPDX-License-Identifier: Apache-2.0 |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +import Benchmark |
| 17 | +import Prometheus |
| 18 | + |
| 19 | +let benchmarks = { |
| 20 | + Benchmark.defaultConfiguration.maxDuration = .seconds(5) |
| 21 | + Benchmark.defaultConfiguration.scalingFactor = .kilo |
| 22 | + |
| 23 | + let registry = PrometheusCollectorRegistry() |
| 24 | + |
| 25 | + func metricsDimensions(_ idx: Int) -> [(String, String)] { |
| 26 | + [ |
| 27 | + ("job", "api_server_\(idx)"), |
| 28 | + ("handler", "/api/handler_\(idx)"), |
| 29 | + ("status_code", "200"), |
| 30 | + ("version", "\(idx).0.0"), |
| 31 | + ] |
| 32 | + } |
| 33 | + |
| 34 | + Benchmark("1 - Metrics: Counter benchmark") { benchmark in |
| 35 | + let ctr = registry.makeCounter(name: "counter_1", labels: metricsDimensions(1)) |
| 36 | + benchmark.startMeasurement() |
| 37 | + for _ in benchmark.scaledIterations { |
| 38 | + blackHole(ctr.increment()) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + Benchmark("2 - Metrics: Gauge benchmark") { benchmark in |
| 43 | + let gauge = registry.makeGauge(name: "gauge_1", labels: metricsDimensions(2)) |
| 44 | + benchmark.startMeasurement() |
| 45 | + for _ in benchmark.scaledIterations { |
| 46 | + blackHole(gauge.increment()) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + Benchmark("3 - Metrics: Histogram benchmark") { benchmark in |
| 51 | + let histogram = registry.makeDurationHistogram(name: "histogram_1", labels: metricsDimensions(3), |
| 52 | + buckets: [ |
| 53 | + .milliseconds(100), |
| 54 | + .milliseconds(250), |
| 55 | + .milliseconds(500), |
| 56 | + .seconds(1), |
| 57 | + ]) |
| 58 | + benchmark.startMeasurement() |
| 59 | + for _ in benchmark.scaledIterations { |
| 60 | + histogram.record(Duration.milliseconds(400)) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + Benchmark("4 - Metrics: export 5000 metrics", |
| 65 | + configuration: .init(scalingFactor: .one)) { benchmark in |
| 66 | + let metricsCount = 5000 |
| 67 | + |
| 68 | + let registryExport = PrometheusCollectorRegistry() |
| 69 | + |
| 70 | + var counterArray = [Counter]() |
| 71 | + var gaugeArray = [Gauge]() |
| 72 | + var buffer = [UInt8]() |
| 73 | + |
| 74 | + let counterExportSize = 620_000 |
| 75 | + counterArray.reserveCapacity(metricsCount) |
| 76 | + gaugeArray.reserveCapacity(metricsCount) |
| 77 | + buffer.reserveCapacity(counterExportSize) |
| 78 | + |
| 79 | + for i in 0..<(metricsCount / 2) { |
| 80 | + let counter = registryExport.makeCounter(name: "http_requests_total", labels: metricsDimensions(i)) |
| 81 | + counter.increment() |
| 82 | + counterArray.append(counter) |
| 83 | + |
| 84 | + let gauge = registryExport.makeGauge(name: "export_gauge_\(i)", labels: metricsDimensions(i)) |
| 85 | + gauge.increment() |
| 86 | + gaugeArray.append(gauge) |
| 87 | + } |
| 88 | + |
| 89 | + benchmark.startMeasurement() |
| 90 | + for _ in benchmark.scaledIterations { |
| 91 | + blackHole(registryExport.emit(into: &buffer)) |
| 92 | + |
| 93 | + } |
| 94 | + benchmark.stopMeasurement() |
| 95 | + buffer.removeAll(keepingCapacity: true) |
| 96 | + } |
| 97 | +} |
0 commit comments