Skip to content

Commit bcdd288

Browse files
authored
Merge pull request #133 from ktoso/benchmark
2 parents 6f4da61 + 06cf0be commit bcdd288

File tree

36 files changed

+406
-0
lines changed

36 files changed

+406
-0
lines changed

.github/workflows/pull_request.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ jobs:
1919
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2020
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2121

22+
benchmarks:
23+
name: Benchmarks
24+
uses: apple/swift-nio/.github/workflows/benchmarks.yml@main
25+
with:
26+
benchmark_package_path: "Benchmarks"
27+
2228
cxx-interop:
2329
name: Cxx interop
2430
uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main

Benchmarks/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
.build
3+
/.build
4+
/Packages
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/configuration/registries.json
8+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
9+
.netrc
10+
.benchmarkBaselines/
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 Prometheus
17+
18+
public func runCounterBench(_ iterations: Range<Int>) {
19+
let ctr = registry.makeCounter(name: "counter_1", labels: makeLabels(1))
20+
for _ in iterations {
21+
blackHole(ctr.increment())
22+
}
23+
}
24+
25+
public func setupCounterBench() -> () -> Void {
26+
let ctr = registry.makeCounter(name: "counter_2", labels: makeLabels(2))
27+
return {
28+
blackHole(ctr.increment())
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 Prometheus
17+
18+
public func runDurationHistogramBench(_ iterations: Range<Int>) {
19+
let histogram = registry.makeDurationHistogram(
20+
name: "histogram_1",
21+
labels: makeLabels(3),
22+
buckets: [
23+
.milliseconds(100),
24+
.milliseconds(250),
25+
.milliseconds(500),
26+
.seconds(1),
27+
]
28+
)
29+
for _ in iterations {
30+
blackHole(histogram.record(Duration.milliseconds(400)))
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 Prometheus
17+
18+
public func runGaugeBench(_ iterations: Range<Int>) {
19+
let gauge = registry.makeGauge(name: "gauge_1", labels: makeLabels(2))
20+
for _ in iterations {
21+
blackHole(gauge.increment())
22+
}
23+
}
24+
25+
public func setupGaugeBench() -> () -> Void {
26+
let gauge = registry.makeGauge(name: "gauge_1", labels: makeLabels(2))
27+
return {
28+
blackHole(gauge.increment())
29+
}
30+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 Prometheus
17+
18+
public func setupRegistryExport(numberOfMetrics: Int) -> () -> Void {
19+
let registryExport = PrometheusCollectorRegistry()
20+
21+
var counterArray = [Counter]()
22+
var gaugeArray = [Gauge]()
23+
var buffer = [UInt8]()
24+
25+
let counterExportSize = 620_000
26+
counterArray.reserveCapacity(numberOfMetrics)
27+
gaugeArray.reserveCapacity(numberOfMetrics)
28+
buffer.reserveCapacity(counterExportSize)
29+
30+
for i in 0..<(numberOfMetrics / 2) {
31+
let counter = registryExport.makeCounter(name: "http_requests_total", labels: makeLabels(i))
32+
counter.increment()
33+
counterArray.append(counter)
34+
35+
let gauge = registryExport.makeGauge(name: "export_gauge_\(i)", labels: makeLabels(i))
36+
gauge.increment()
37+
gaugeArray.append(gauge)
38+
}
39+
return {
40+
blackHole(registryExport.emit(into: &buffer))
41+
}
42+
}

Benchmarks/Package.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// swift-tools-version:5.9
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 PackageDescription
17+
18+
let package = Package(
19+
name: "benchmarks",
20+
platforms: [
21+
.macOS(.v13)
22+
],
23+
dependencies: [
24+
.package(path: "../"),
25+
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.29.0"),
26+
],
27+
targets: [
28+
.executableTarget(
29+
name: "PrometheusBenchmarks",
30+
dependencies: [
31+
.product(name: "Benchmark", package: "package-benchmark"),
32+
.product(name: "Prometheus", package: "swift-prometheus"),
33+
],
34+
path: "Benchmarks/PrometheusBenchmarks",
35+
plugins: [
36+
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
37+
]
38+
)
39+
]
40+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mallocCountTotal" : 0
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mallocCountTotal" : 1
3+
}

0 commit comments

Comments
 (0)