@@ -20,75 +20,10 @@ import NIOPosix
20
20
import Valkey
21
21
22
22
let benchmarks : @Sendable ( ) -> Void = {
23
- let defaultMetrics : [ BenchmarkMetric ] =
24
- // There is no point comparing wallClock, cpuTotal or throughput on CI as they are too inconsistent
25
- ProcessInfo . processInfo. environment [ " CI " ] != nil
26
- ? [
27
- . instructions,
28
- . mallocCountTotal,
29
- ]
30
- : [
31
- . wallClock,
32
- . cpuTotal,
33
- . instructions,
34
- . mallocCountTotal,
35
- . throughput,
36
- ]
37
-
38
- var server : ( any Channel ) ?
39
-
40
23
if #available( valkeySwift 1 . 0 , * ) {
41
- Benchmark ( " GET benchmark " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
42
- let port = server!. localAddress!. port!
43
- let logger = Logger ( label: " test " )
44
- let client = ValkeyClient ( . hostname( " 127.0.0.1 " , port: port) , logger: logger)
45
-
46
- try await withThrowingTaskGroup ( of: Void . self) { group in
47
- group. addTask {
48
- await client. run ( )
49
- }
50
- await Task . yield ( )
51
- try await client. withConnection { connection in
52
- benchmark. startMeasurement ( )
53
-
54
- for _ in benchmark. scaledIterations {
55
- let foo = try await connection. get ( " foo " )
56
- precondition ( foo. map { String ( buffer: $0) } == " Bar " )
57
- }
24
+ connectionBenchmarks ( )
58
25
59
- benchmark. stopMeasurement ( )
60
- }
61
- group. cancelAll ( )
62
- }
63
- } setup: {
64
- struct GetHandler : BenchmarkCommandHandler {
65
- static let expectedCommand = RESPToken . Value. bulkString ( ByteBuffer ( string: " GET " ) )
66
- static let response = ByteBuffer ( string: " $3 \r \n Bar \r \n " )
67
- func handle( command: RESPToken . Value , parameters: RESPToken . Array . Iterator , write: ( ByteBuffer ) -> Void ) {
68
- switch command {
69
- case Self . expectedCommand:
70
- write ( Self . response)
71
- default :
72
- fatalError ( )
73
- }
74
- }
75
- }
76
- server = try await ServerBootstrap ( group: NIOSingletons . posixEventLoopGroup)
77
- . childChannelInitializer { channel in
78
- do {
79
- try channel. pipeline. syncOperations. addHandler (
80
- ValkeyServerChannelHandler ( commandHandler: GetHandler ( ) )
81
- )
82
- return channel. eventLoop. makeSucceededVoidFuture ( )
83
- } catch {
84
- return channel. eventLoop. makeFailedFuture ( error)
85
- }
86
- }
87
- . bind ( host: " 127.0.0.1 " , port: 0 )
88
- . get ( )
89
- } teardown: {
90
- try await server? . close ( ) . get ( )
91
- }
26
+ clientBenchmarks ( )
92
27
93
28
Benchmark ( " ValkeyCommandEncoder – Simple GET " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
94
29
let command = GET ( " foo " )
@@ -143,47 +78,3 @@ let benchmarks: @Sendable () -> Void = {
143
78
}
144
79
}
145
80
}
146
-
147
- protocol BenchmarkCommandHandler {
148
- func handle( command: RESPToken . Value , parameters: RESPToken . Array . Iterator , write: ( ByteBuffer ) -> Void )
149
- }
150
-
151
- final class ValkeyServerChannelHandler < Handler: BenchmarkCommandHandler > : ChannelInboundHandler {
152
-
153
- typealias InboundIn = ByteBuffer
154
- typealias OutboundOut = ByteBuffer
155
-
156
- private var decoder = NIOSingleStepByteToMessageProcessor ( RESPTokenDecoder ( ) )
157
- private let helloCommand = RESPToken . Value. bulkString ( ByteBuffer ( string: " HELLO " ) )
158
- private let helloResponse = ByteBuffer ( string: " %1 \r \n +server \r \n +fake \r \n " )
159
- private let commandHandler : Handler
160
-
161
- init ( commandHandler: Handler ) {
162
- self . commandHandler = commandHandler
163
- }
164
-
165
- func channelRead( context: ChannelHandlerContext , data: NIOAny ) {
166
- try ! self . decoder. process ( buffer: self . unwrapInboundIn ( data) ) { token in
167
- self . handleToken ( context: context, token: token)
168
- }
169
- }
170
-
171
- func handleToken( context: ChannelHandlerContext , token: RESPToken ) {
172
- guard case . array( let array) = token. value else {
173
- fatalError ( )
174
- }
175
- var iterator = array. makeIterator ( )
176
- guard let command = iterator. next ( ) ? . value else {
177
- fatalError ( )
178
- }
179
- switch command {
180
- case helloCommand:
181
- context. writeAndFlush ( self . wrapOutboundOut ( helloResponse) , promise: nil )
182
-
183
- default :
184
- commandHandler. handle ( command: command, parameters: iterator) {
185
- context. writeAndFlush ( self . wrapOutboundOut ( $0) , promise: nil )
186
- }
187
- }
188
- }
189
- }
0 commit comments