Skip to content

Commit 3e1521f

Browse files
authored
Add async implementation of QPSBenchmark (#1471)
Motivation: We want to provide an async/await version of the QPSBenchmark implementation, and be able to choose whether we want to use that or the ELF version when running the performance tests via the gRPC JSON driver. Modifications: - Added full async/await-based implementations of the worker, server, and client. - Created a new command line flag (`--use-async`) to choose between the EventLoopFuture and async/await versions when running the tests. Result: QPSBenchmark can optionally be executed using an async/await implementation.
1 parent 7e6d8fa commit 3e1521f

21 files changed

+1051
-47
lines changed

Performance/QPSBenchmark/Package.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ let package = Package(
2525
],
2626
dependencies: [
2727
.package(path: "../../"),
28-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.32.0"),
29-
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
30-
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.0"),
28+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.41.0"),
29+
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.3"),
30+
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.1.1"),
3131
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
3232
.package(
3333
url: "https://github.com/swift-server/swift-service-lifecycle.git",
3434
from: "1.0.0-alpha"
3535
),
3636
.package(
37-
name: "SwiftProtobuf",
3837
url: "https://github.com/apple/swift-protobuf.git",
3938
from: "1.19.0"
4039
),
@@ -51,7 +50,7 @@ let package = Package(
5150
.product(name: "ArgumentParser", package: "swift-argument-parser"),
5251
.product(name: "Logging", package: "swift-log"),
5352
.product(name: "Lifecycle", package: "swift-service-lifecycle"),
54-
.product(name: "SwiftProtobuf", package: "SwiftProtobuf"),
53+
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
5554
.target(name: "BenchmarkUtils"),
5655
],
5756
exclude: [
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2022, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import GRPC
18+
import NIOCore
19+
20+
/// Protocol which async clients must implement.
21+
protocol AsyncQPSClient {
22+
/// Start the execution of the client.
23+
func startClient()
24+
25+
/// Send the status of the current test
26+
/// - parameters:
27+
/// - reset: Indicates if the stats collection should be reset after publication or not.
28+
/// - responseStream: the response stream to write the response to.
29+
func sendStatus(
30+
reset: Bool,
31+
responseStream: GRPCAsyncResponseStreamWriter<Grpc_Testing_ClientStatus>
32+
) async throws
33+
34+
/// Shut down the client.
35+
func shutdown() async throws
36+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2022, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Atomics
18+
import Foundation
19+
import GRPC
20+
import Logging
21+
import NIOCore
22+
23+
/// Makes streaming requests and listens to responses ping-pong style.
24+
/// Iterations can be limited by config.
25+
/// Class is marked as `@unchecked Sendable` because `ManagedAtomic<Bool>` doesn't conform
26+
/// to `Sendable`, but we know it's safe.
27+
final class AsyncPingPongRequestMaker: AsyncRequestMaker, @unchecked Sendable {
28+
private let client: Grpc_Testing_BenchmarkServiceAsyncClient
29+
private let requestMessage: Grpc_Testing_SimpleRequest
30+
private let logger: Logger
31+
private let stats: StatsWithLock
32+
33+
/// If greater than zero gives a limit to how many messages are exchanged before termination.
34+
private let messagesPerStream: Int
35+
/// Stops more requests being made after stop is requested.
36+
private let stopRequested = ManagedAtomic<Bool>(false)
37+
38+
/// Initialiser to gather requirements.
39+
/// - Parameters:
40+
/// - config: config from the driver describing what to do.
41+
/// - client: client interface to the server.
42+
/// - requestMessage: Pre-made request message to use possibly repeatedly.
43+
/// - logger: Where to log useful diagnostics.
44+
/// - stats: Where to record statistics on latency.
45+
init(
46+
config: Grpc_Testing_ClientConfig,
47+
client: Grpc_Testing_BenchmarkServiceAsyncClient,
48+
requestMessage: Grpc_Testing_SimpleRequest,
49+
logger: Logger,
50+
stats: StatsWithLock
51+
) {
52+
self.client = client
53+
self.requestMessage = requestMessage
54+
self.logger = logger
55+
self.stats = stats
56+
57+
self.messagesPerStream = Int(config.messagesPerStream)
58+
}
59+
60+
/// Initiate a request sequence to the server - in this case the sequence is streaming requests to the server and waiting
61+
/// to see responses before repeating ping-pong style. The number of iterations can be limited by config.
62+
func makeRequest() async throws {
63+
var startTime = grpcTimeNow()
64+
var messagesSent = 0
65+
66+
let streamingCall = self.client.makeStreamingCallCall()
67+
var responseStream = streamingCall.responseStream.makeAsyncIterator()
68+
while !self.stopRequested.load(ordering: .relaxed),
69+
self.messagesPerStream == 0 || messagesSent < self.messagesPerStream {
70+
try await streamingCall.requestStream.send(self.requestMessage)
71+
let _ = try await responseStream.next()
72+
let endTime = grpcTimeNow()
73+
self.stats.add(latency: endTime - startTime)
74+
messagesSent += 1
75+
startTime = endTime
76+
}
77+
}
78+
79+
/// Request termination of the request-response sequence.
80+
func requestStop() {
81+
self.logger.info("AsyncPingPongRequestMaker stop requested")
82+
// Flag stop as requested - this will prevent any more requests being made.
83+
self.stopRequested.store(true, ordering: .relaxed)
84+
}
85+
}

0 commit comments

Comments
 (0)