|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftPrometheus open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018-2023 SwiftPrometheus project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Benchmark |
| 16 | +import Foundation |
| 17 | +import Prometheus |
| 18 | + |
| 19 | +let registry = PrometheusCollectorRegistry() |
| 20 | + |
| 21 | +public func makeLabels(_ idx: Int) -> [(String, String)] { |
| 22 | + [ |
| 23 | + ("job", "api_server_\(idx)"), |
| 24 | + ("handler", "/api/handler_\(idx)"), |
| 25 | + ("status_code", "200"), |
| 26 | + ("version", "\(idx).0.0"), |
| 27 | + ] |
| 28 | +} |
| 29 | + |
| 30 | +let benchmarks = { |
| 31 | + let ciMetrics: [BenchmarkMetric] = [ |
| 32 | + .mallocCountTotal |
| 33 | + ] |
| 34 | + let localMetrics = BenchmarkMetric.default |
| 35 | + |
| 36 | + Benchmark.defaultConfiguration = .init( |
| 37 | + metrics: ProcessInfo.processInfo.environment["CI"] != nil ? ciMetrics : localMetrics, |
| 38 | + warmupIterations: 10, |
| 39 | + scalingFactor: .kilo, |
| 40 | + maxDuration: .seconds(5) |
| 41 | + ) |
| 42 | + |
| 43 | + Benchmark("Counter - setup and increment") { benchmark in |
| 44 | + runCounterBench(benchmark.scaledIterations) |
| 45 | + } |
| 46 | + |
| 47 | + Benchmark("Counter - increment only") { benchmark, run in |
| 48 | + for _ in benchmark.scaledIterations { |
| 49 | + run() |
| 50 | + } |
| 51 | + } setup: { |
| 52 | + setupCounterBench() |
| 53 | + } |
| 54 | + |
| 55 | + Benchmark("Gauge") { benchmark in |
| 56 | + runGaugeBench(benchmark.scaledIterations) |
| 57 | + } |
| 58 | + |
| 59 | + Benchmark("DurationHistogram") { benchmark in |
| 60 | + runDurationHistogramBench(benchmark.scaledIterations) |
| 61 | + } |
| 62 | + |
| 63 | + Benchmark( |
| 64 | + "RegistryEmit - 5000 metrics", |
| 65 | + configuration: .init(scalingFactor: .one) |
| 66 | + ) { benchmark, run in |
| 67 | + for _ in benchmark.scaledIterations { |
| 68 | + run() |
| 69 | + } |
| 70 | + } setup: { |
| 71 | + setupRegistryExport(numberOfMetrics: 5000) |
| 72 | + } |
| 73 | +} |
0 commit comments