Skip to content

Add Benchmarks for metrics #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ jobs:
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"

benchmarks:
name: Benchmarks
# Workaround https://github.com/nektos/act/issues/1875
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Workaround https://github.com/nektos/act/issues/1875

Tiny nit: this is only needed in the NIO repo itself (but has accidentally been spread around), it relates to how we are using a full repo reference to a workflow which is local to NIO to workaround an issue.

uses: apple/swift-nio/.github/workflows/benchmarks.yml@main
with:
benchmark_package_path: "Benchmarks"

cxx-interop:
name: Cxx interop
uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main
10 changes: 10 additions & 0 deletions Benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
.build
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
.benchmarkBaselines/
72 changes: 72 additions & 0 deletions Benchmarks/Benchmarks/PrometheusBenchmarks/Benchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftPrometheus open source project
//
// Copyright (c) 2018-2023 SwiftPrometheus project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
import Foundation
import Prometheus

let registry = PrometheusCollectorRegistry()

public func makeLabels(_ idx: Int) -> [(String, String)] {
[
("job", "api_server_\(idx)"),
("handler", "/api/handler_\(idx)"),
("status_code", "200"),
("version", "\(idx).0.0"),
]
}

let benchmarks = {
let ciMetrics: [BenchmarkMetric] = [
.mallocCountTotal,
]
let localMetrics = BenchmarkMetric.default

Benchmark.defaultConfiguration = .init(
metrics: ProcessInfo.processInfo.environment["CI"] != nil ? ciMetrics : localMetrics,
warmupIterations: 10,
scalingFactor: .kilo,
maxDuration: .seconds(5),
)

Benchmark("Counter - setup and increment") { benchmark in
runCounterBench(benchmark.scaledIterations)
}

Benchmark("Counter - increment only") { benchmark, run in
for _ in benchmark.scaledIterations {
run()
}
} setup: {
setupCounterBench()
}

Benchmark("Gauge") { benchmark in
runGaugeBench(benchmark.scaledIterations)
}

Benchmark("DurationHistogram") { benchmark in
runDurationHistogramBench(benchmark.scaledIterations)
}

Benchmark("RegistryEmit - 5000 metrics",
configuration: .init(scalingFactor: .one))
{ benchmark, run in
for _ in benchmark.scaledIterations {
run()
}
} setup: {
setupRegistryExport(numberOfMetrics: 5000)
}
}
30 changes: 30 additions & 0 deletions Benchmarks/Benchmarks/PrometheusBenchmarks/Counter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftPrometheus open source project
//
// Copyright (c) 2018-2023 SwiftPrometheus project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
import Prometheus

public func runCounterBench(_ iterations: Range<Int>) {
let ctr = registry.makeCounter(name: "counter_1", labels: makeLabels(1))
for _ in iterations {
blackHole(ctr.increment())
}
}

public func setupCounterBench() -> () -> Void {
let ctr = registry.makeCounter(name: "counter_2", labels: makeLabels(2))
return {
blackHole(ctr.increment())
}
}
32 changes: 32 additions & 0 deletions Benchmarks/Benchmarks/PrometheusBenchmarks/DurationHistogram.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftPrometheus open source project
//
// Copyright (c) 2018-2023 SwiftPrometheus project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
import Prometheus

public func runDurationHistogramBench(_ iterations: Range<Int>) {
let histogram = registry.makeDurationHistogram(
name: "histogram_1",
labels: makeLabels(3),
buckets: [
.milliseconds(100),
.milliseconds(250),
.milliseconds(500),
.seconds(1),
]
)
for _ in iterations {
blackHole(histogram.record(Duration.milliseconds(400)))
}
}
30 changes: 30 additions & 0 deletions Benchmarks/Benchmarks/PrometheusBenchmarks/Gauge.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftPrometheus open source project
//
// Copyright (c) 2018-2023 SwiftPrometheus project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
import Prometheus

public func runGaugeBench(_ iterations: Range<Int>) {
let gauge = registry.makeGauge(name: "gauge_1", labels: makeLabels(2))
for _ in iterations {
blackHole(gauge.increment())
}
}

public func setupGaugeBench() -> () -> Void {
let gauge = registry.makeGauge(name: "gauge_1", labels: makeLabels(2))
return {
blackHole(gauge.increment())
}
}
42 changes: 42 additions & 0 deletions Benchmarks/Benchmarks/PrometheusBenchmarks/RegistryEmit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftPrometheus open source project
//
// Copyright (c) 2018-2023 SwiftPrometheus project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
import Prometheus

public func setupRegistryExport(numberOfMetrics: Int) -> () -> Void {
let registryExport = PrometheusCollectorRegistry()

var counterArray = [Counter]()
var gaugeArray = [Gauge]()
var buffer = [UInt8]()

let counterExportSize = 620_000
counterArray.reserveCapacity(numberOfMetrics)
gaugeArray.reserveCapacity(numberOfMetrics)
buffer.reserveCapacity(counterExportSize)

for i in 0 ..< (numberOfMetrics / 2) {
let counter = registryExport.makeCounter(name: "http_requests_total", labels: makeLabels(i))
counter.increment()
counterArray.append(counter)

let gauge = registryExport.makeGauge(name: "export_gauge_\(i)", labels: makeLabels(i))
gauge.increment()
gaugeArray.append(gauge)
}
return {
blackHole(registryExport.emit(into: &buffer))
}
}
40 changes: 40 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// swift-tools-version:5.9
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftPrometheus open source project
//
// Copyright (c) 2018-2023 SwiftPrometheus project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import PackageDescription

let package = Package(
name: "benchmarks",
platforms: [
.macOS(.v13),
],
dependencies: [
.package(path: "../"),
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.29.0"),
],
targets: [
.executableTarget(
name: "PrometheusBenchmarks",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "Prometheus", package: "swift-prometheus"),
],
path: "Benchmarks/PrometheusBenchmarks",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark"),
]
),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 2
}
3 changes: 3 additions & 0 deletions Benchmarks/Thresholds/6.0/PrometheusBenchmarks.Gauge.p90.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 2
}
3 changes: 3 additions & 0 deletions Benchmarks/Thresholds/6.1/PrometheusBenchmarks.Gauge.p90.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 1
}
Loading