Skip to content
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

Added initial benchmarks #40

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ DerivedData/
/Package.resolved
.ci/
.docc-build/
Package.resolved
8 changes: 8 additions & 0 deletions Benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
.benchmarkBaselines/
30 changes: 30 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "benchmarks",
platforms: [
.macOS(.v14),
],
products: [
.executable(name: "Benchmarks", targets: ["Benchmarks"]),
],
dependencies: [
.package(name: "swift-openapi-async-http-client", path: "../"),
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.26.0"),
],
targets: [
.executableTarget(
name: "Benchmarks",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "OpenAPIAsyncHTTPClient", package: "swift-openapi-async-http-client")
],
path: "Sources",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
]
),
]
)
23 changes: 23 additions & 0 deletions Benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Benchmarks

## Running

Run and check results.

```zsh
swift package benchmark
```

## Checking against baselines

Run for a specific Swift version, for example:
```zsh
swift package benchmark baseline check --check-absolute-path Thresholds/5.10/
```

## Updating baselines

Update for a specific Swift version, for example:
```zsh
swift package --allow-writing-to-package-directory benchmark --format metricP90AbsoluteThresholds --path Thresholds/5.10/
```
77 changes: 77 additions & 0 deletions Benchmarks/Sources/Benchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) YEARS Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
@_spi(Benchmarks) import OpenAPIAsyncHTTPClient
import AsyncHTTPClient
import NIOCore
import HTTPTypes
import OpenAPIRuntime
import Foundation

let benchmarks = {
let defaultMetrics: [BenchmarkMetric] = [
.mallocCountTotal,
.cpuTotal
]
let config: Benchmark.Configuration = .init(
metrics: defaultMetrics,
scalingFactor: .kilo,
maxDuration: .seconds(10)
)

Benchmark("Creation.Default", configuration: config) { benchmark in
for _ in benchmark.scaledIterations {
blackHole({
AsyncHTTPClientTransport()
}())
}
}

Benchmark("Conversion", configuration: config) { benchmark in
let request = HTTPRequest(
method: .post,
scheme: nil,
authority: nil,
path: "/stuff",
headerFields: [
.init("x-stuff")!: "things"
]
)
let requestBody = HTTPBody("Hello world")
let baseURL = URL(string: "https://example.com")!
let response = HTTPClientResponse(
status: .ok,
headers: [
"x-stuff": "things"
],
body: .bytes(ByteBuffer(string: "Hello world"))
)
let transport = AsyncHTTPClientTransport(
configuration: .init(),
requestSenderClosure: { _, _, _ in
response
}
)
for _ in benchmark.scaledIterations {
let (response, responseBody) = try await transport.send(
request,
body: requestBody,
baseURL: baseURL,
operationID: "postThings"
)
blackHole((response, responseBody))
}
}
}
21 changes: 21 additions & 0 deletions Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ public struct AsyncHTTPClientTransport: ClientTransport {
self.requestSender = requestSender
}

/// Creates a new transport.
/// - Parameters:
/// - configuration: A set of configuration values used by the transport.
/// - requestSenderClosure: The underlying request sender closure.
@_spi(Benchmarks) public init(
configuration: Configuration,
requestSenderClosure: @Sendable @escaping (HTTPClientRequest, HTTPClient, TimeAmount) async throws ->
HTTPClientResponse
) {
struct ClosureRequestSender: HTTPRequestSending {
var sendClosure:
@Sendable (AsyncHTTPClientTransport.Request, HTTPClient, TimeAmount) async throws ->
AsyncHTTPClientTransport.Response
func send(request: AsyncHTTPClientTransport.Request, with client: HTTPClient, timeout: TimeAmount)
async throws -> AsyncHTTPClientTransport.Response
{ try await sendClosure(request, client, timeout) }
}
self.configuration = configuration
self.requestSender = ClosureRequestSender(sendClosure: requestSenderClosure)
}

/// Creates a new transport.
/// - Parameter configuration: A set of configuration values used by the transport.
public init(configuration: Configuration = .init()) {
Expand Down
3 changes: 3 additions & 0 deletions scripts/check-license-headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ read -ra PATHS_TO_CHECK_FOR_LICENSE <<< "$( \
":(exclude).gitignore" \
":(exclude).spi.yml" \
":(exclude).swift-format" \
":(exclude)Benchmarks/.gitignore" \
":(exclude)Benchmarks/Package.swift" \
":(exclude)Benchmarks/README.md" \
":(exclude)CODE_OF_CONDUCT.md" \
":(exclude)CONTRIBUTING.md" \
":(exclude)CONTRIBUTORS.txt" \
Expand Down