@@ -35,109 +35,112 @@ let benchmarks: @Sendable () -> Void = {
35
35
. throughput,
36
36
]
37
37
38
- var server : Channel ?
39
- Benchmark ( " GET benchmark " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
40
- let port = server!. localAddress!. port!
41
- let logger = Logger ( label: " test " )
42
- let client = ValkeyClient ( . hostname( " 127.0.0.1 " , port: port) , logger: logger)
43
-
44
- try await withThrowingTaskGroup ( of: Void . self) { group in
45
- group. addTask {
46
- await client. run ( )
47
- }
48
- await Task . yield ( )
49
- try await client. withConnection { connection in
50
- benchmark. startMeasurement ( )
38
+ var server : ( any Channel ) ?
39
+
40
+ 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)
51
45
52
- for _ in benchmark . scaledIterations {
53
- let foo = try await connection . get ( key : " foo " ) ? . decode ( as : String . self )
54
- precondition ( foo == " Bar " )
46
+ try await withThrowingTaskGroup ( of : Void . self ) { group in
47
+ group . addTask {
48
+ await client . run ( )
55
49
}
50
+ await Task . yield ( )
51
+ try await client. withConnection { connection in
52
+ benchmark. startMeasurement ( )
56
53
57
- benchmark. stopMeasurement ( )
58
- }
59
- group. cancelAll ( )
60
- }
61
- } setup: {
62
- struct GetHandler : BenchmarkCommandHandler {
63
- static let expectedCommand = RESPToken . Value. bulkString ( ByteBuffer ( string: " GET " ) )
64
- static let response = ByteBuffer ( string: " $3 \r \n Bar \r \n " )
65
- func handle( command: RESPToken . Value , parameters: RESPToken . Array . Iterator , write: ( ByteBuffer ) -> Void ) {
66
- switch command {
67
- case Self . expectedCommand:
68
- write ( Self . response)
69
- default :
70
- fatalError ( )
54
+ for _ in benchmark. scaledIterations {
55
+ let foo = try await connection. get ( key: " foo " ) ? . decode ( as: String . self)
56
+ precondition ( foo == " Bar " )
57
+ }
58
+
59
+ benchmark. stopMeasurement ( )
71
60
}
61
+ group. cancelAll ( )
72
62
}
73
- }
74
- server = try await ServerBootstrap ( group: NIOSingletons . posixEventLoopGroup)
75
- . childChannelInitializer { channel in
76
- do {
77
- try channel. pipeline. syncOperations. addHandler (
78
- ValkeyServerChannelHandler ( commandHandler: GetHandler ( ) )
79
- )
80
- return channel. eventLoop. makeSucceededVoidFuture ( )
81
- } catch {
82
- return channel. eventLoop. makeFailedFuture ( error)
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
+ }
83
74
}
84
75
}
85
- . bind ( host: " 127.0.0.1 " , port: 0 )
86
- . get ( )
87
- } teardown: {
88
- try await server? . close ( ) . get ( )
89
- }
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
+ }
90
92
91
- Benchmark ( " ValkeyCommandEncoder – Simple GET " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
92
- let command = GET ( key: " foo " )
93
- benchmark. startMeasurement ( )
93
+ Benchmark ( " ValkeyCommandEncoder – Simple GET " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
94
+ let command = GET ( key: " foo " )
95
+ benchmark. startMeasurement ( )
94
96
95
- var encoder = ValkeyCommandEncoder ( )
96
- for _ in benchmark. scaledIterations {
97
- encoder. reset ( )
98
- command. encode ( into: & encoder)
97
+ var encoder = ValkeyCommandEncoder ( )
98
+ for _ in benchmark. scaledIterations {
99
+ encoder. reset ( )
100
+ command. encode ( into: & encoder)
101
+ }
102
+
103
+ benchmark. stopMeasurement ( )
99
104
}
100
105
101
- benchmark. stopMeasurement ( )
102
- }
106
+ Benchmark ( " ValkeyCommandEncoder – Simple MGET 15 keys " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
107
+ let keys = ( 0 ..< 15 ) . map { ValkeyKey ( rawValue: " foo- \( $0) " ) }
108
+ let command = MGET ( key: keys)
109
+ benchmark. startMeasurement ( )
103
110
104
- Benchmark ( " ValkeyCommandEncoder – Simple MGET 15 keys " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
105
- let keys = ( 0 ..< 15 ) . map { ValkeyKey ( rawValue: " foo- \( $0) " ) }
106
- let command = MGET ( key: keys)
107
- benchmark. startMeasurement ( )
111
+ var encoder = ValkeyCommandEncoder ( )
112
+ for _ in benchmark. scaledIterations {
113
+ encoder. reset ( )
114
+ command. encode ( into: & encoder)
115
+ }
108
116
109
- var encoder = ValkeyCommandEncoder ( )
110
- for _ in benchmark. scaledIterations {
111
- encoder. reset ( )
112
- command. encode ( into: & encoder)
117
+ benchmark. stopMeasurement ( )
113
118
}
114
119
115
- benchmark. stopMeasurement ( )
116
- }
120
+ Benchmark ( " ValkeyCommandEncoder – Command with 7 words " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
121
+ let string = " string "
122
+ let optionalString : String ? = " optionalString "
123
+ let array = [ " array " , " of " , " strings " ]
124
+ let number = 456
125
+ let token = RESPPureToken ( " TOKEN " , true )
126
+ benchmark. startMeasurement ( )
127
+
128
+ var encoder = ValkeyCommandEncoder ( )
129
+ for _ in benchmark. scaledIterations {
130
+ encoder. reset ( )
131
+ encoder. encodeArray ( string, optionalString, array, number, token)
132
+ }
117
133
118
- Benchmark ( " ValkeyCommandEncoder – Command with 7 words " , configuration: . init( metrics: defaultMetrics, scalingFactor: . kilo) ) { benchmark in
119
- let string = " string "
120
- let optionalString : String ? = " optionalString "
121
- let array = [ " array " , " of " , " strings " ]
122
- let number = 456
123
- let token = RESPPureToken ( " TOKEN " , true )
124
- benchmark. startMeasurement ( )
125
-
126
- var encoder = ValkeyCommandEncoder ( )
127
- for _ in benchmark. scaledIterations {
128
- encoder. reset ( )
129
- encoder. encodeArray ( string, optionalString, array, number, token)
134
+ benchmark. stopMeasurement ( )
130
135
}
131
136
132
- benchmark. stopMeasurement ( )
133
- }
134
-
135
- Benchmark ( " HashSlot – {user}.whatever " , configuration: . init( metrics: defaultMetrics, scalingFactor: . mega) ) { benchmark in
136
- let key = " {user}.whatever "
137
+ Benchmark ( " HashSlot – {user}.whatever " , configuration: . init( metrics: defaultMetrics, scalingFactor: . mega) ) { benchmark in
138
+ let key = " {user}.whatever "
137
139
138
- benchmark. startMeasurement ( )
139
- for _ in benchmark. scaledIterations {
140
- blackHole ( HashSlot ( key: key) )
140
+ benchmark. startMeasurement ( )
141
+ for _ in benchmark. scaledIterations {
142
+ blackHole ( HashSlot ( key: key) )
143
+ }
141
144
}
142
145
}
143
146
}
0 commit comments