|
1 | 1 | import _ConnectionPoolModule
|
2 | 2 | import _ConnectionPoolTestUtils
|
3 | 3 | import Benchmark
|
| 4 | +import NIOCore |
| 5 | +import NIOPosix |
4 | 6 |
|
5 | 7 | let benchmarks: @Sendable () -> Void = {
|
6 |
| - Benchmark("Lease/Release 1k requests: 50 parallel", configuration: .init(scalingFactor: .kilo)) { benchmark in |
| 8 | + Benchmark("Pool: Lease/Release 1k requests: 50 parallel", configuration: .init(scalingFactor: .kilo)) { benchmark in |
7 | 9 | let clock = MockClock()
|
8 |
| - let factory = MockConnectionFactory<MockClock>(autoMaxStreams: 1) |
| 10 | + let factory = MockConnectionFactory<MockClock, MockExecutor>(autoMaxStreams: 1) |
9 | 11 | var configuration = ConnectionPoolConfiguration()
|
10 | 12 | configuration.maximumConnectionSoftLimit = 50
|
11 | 13 | configuration.maximumConnectionHardLimit = 50
|
12 | 14 |
|
13 | 15 | let pool = ConnectionPool(
|
14 | 16 | configuration: configuration,
|
| 17 | + connectionConfiguration: MockConnectionConfiguration(username: "username", password: "password"), |
15 | 18 | idGenerator: ConnectionIDGenerator(),
|
16 | 19 | keepAliveBehavior: MockPingPongBehavior(keepAliveFrequency: nil, connectionType: MockConnection.self),
|
17 |
| - observabilityDelegate: NoOpConnectionPoolMetrics(connectionIDType: MockConnection.ID.self), |
| 20 | + executor: MockExecutor(), |
| 21 | + observabilityDelegate: NoOpConnectionPoolMetrics(connectionIDType: MockConnection<MockExecutor>.ID.self), |
18 | 22 | clock: clock
|
19 | 23 | ) {
|
20 |
| - try await factory.makeConnection(id: $0, for: $1) |
| 24 | + try await factory.makeConnection(id: $0, configuration: $1, for: $2) |
21 | 25 | }
|
22 | 26 |
|
23 | 27 | await withTaskGroup { taskGroup in
|
@@ -54,21 +58,23 @@ let benchmarks: @Sendable () -> Void = {
|
54 | 58 | }
|
55 | 59 | }
|
56 | 60 |
|
57 |
| - Benchmark("Lease/Release 1k requests: sequential", configuration: .init(scalingFactor: .kilo)) { benchmark in |
| 61 | + Benchmark("Pool: Lease/Release 1k requests: sequential", configuration: .init(scalingFactor: .kilo)) { benchmark in |
58 | 62 | let clock = MockClock()
|
59 |
| - let factory = MockConnectionFactory<MockClock>(autoMaxStreams: 1) |
| 63 | + let factory = MockConnectionFactory<MockClock, MockExecutor>(autoMaxStreams: 1) |
60 | 64 | var configuration = ConnectionPoolConfiguration()
|
61 | 65 | configuration.maximumConnectionSoftLimit = 50
|
62 | 66 | configuration.maximumConnectionHardLimit = 50
|
63 | 67 |
|
64 | 68 | let pool = ConnectionPool(
|
65 | 69 | configuration: configuration,
|
| 70 | + connectionConfiguration: MockConnectionConfiguration(username: "username", password: "password"), |
66 | 71 | idGenerator: ConnectionIDGenerator(),
|
67 | 72 | keepAliveBehavior: MockPingPongBehavior(keepAliveFrequency: nil, connectionType: MockConnection.self),
|
68 |
| - observabilityDelegate: NoOpConnectionPoolMetrics(connectionIDType: MockConnection.ID.self), |
| 73 | + executor: MockExecutor(), |
| 74 | + observabilityDelegate: NoOpConnectionPoolMetrics(connectionIDType: MockConnection<MockExecutor>.ID.self), |
69 | 75 | clock: clock
|
70 | 76 | ) {
|
71 |
| - try await factory.makeConnection(id: $0, for: $1) |
| 77 | + try await factory.makeConnection(id: $0, configuration: $1, for: $2) |
72 | 78 | }
|
73 | 79 |
|
74 | 80 | await withTaskGroup { taskGroup in
|
@@ -96,4 +102,123 @@ let benchmarks: @Sendable () -> Void = {
|
96 | 102 | taskGroup.cancelAll()
|
97 | 103 | }
|
98 | 104 | }
|
| 105 | + |
| 106 | + Benchmark("PoolManager/TaskExecutor: Lease/Release 1k requests: 50 parallel – 10 MockExecutors", configuration: .init(scalingFactor: .kilo)) { benchmark in |
| 107 | + let clock = MockClock() |
| 108 | + let factory = MockConnectionFactory<MockClock, MockExecutor>(autoMaxStreams: 1) |
| 109 | + var configuration = ConnectionPoolManagerConfiguration() |
| 110 | + let executorCount = 10 |
| 111 | + let executors = (0..<executorCount).map { _ in MockExecutor() } |
| 112 | + |
| 113 | + let concurrency = 50 |
| 114 | + |
| 115 | + configuration.maximumConnectionPerExecutorSoftLimit = concurrency / executorCount |
| 116 | + configuration.maximumConnectionPerExecutorHardLimit = concurrency / executorCount |
| 117 | + |
| 118 | + let pool = ConnectionPoolManager( |
| 119 | + configuration: configuration, |
| 120 | + connectionConfiguration: MockConnectionConfiguration(username: "username", password: "password"), |
| 121 | + idGenerator: ConnectionIDGenerator(), |
| 122 | + requestType: ConnectionRequest<MockConnection>.self, |
| 123 | + keepAliveBehavior: MockPingPongBehavior(keepAliveFrequency: nil, connectionType: MockConnection.self), |
| 124 | + executors: executors, |
| 125 | + observabilityDelegate: NoOpConnectionPoolMetrics(connectionIDType: MockConnection<MockExecutor>.ID.self), |
| 126 | + clock: clock |
| 127 | + ) { |
| 128 | + try await factory.makeConnection(id: $0, configuration: $1, for: $2) |
| 129 | + } |
| 130 | + |
| 131 | + await withTaskGroup { taskGroup in |
| 132 | + taskGroup.addTask { |
| 133 | + await pool.run() |
| 134 | + } |
| 135 | + |
| 136 | + let sequential = benchmark.scaledIterations.upperBound / concurrency |
| 137 | + |
| 138 | + benchmark.startMeasurement() |
| 139 | + |
| 140 | + for parallel in 0..<concurrency { |
| 141 | + taskGroup.addTask { |
| 142 | + for _ in 0..<sequential { |
| 143 | + do { |
| 144 | + try await pool.withConnection { connection in |
| 145 | + blackHole(connection) |
| 146 | + } |
| 147 | + } catch { |
| 148 | + fatalError() |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + for i in 0..<concurrency { |
| 155 | + await taskGroup.next() |
| 156 | + } |
| 157 | + |
| 158 | + benchmark.stopMeasurement() |
| 159 | + |
| 160 | + taskGroup.cancelAll() |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *) { |
| 165 | + let eventLoops = NIOSingletons.posixEventLoopGroup |
| 166 | + let count = eventLoops.makeIterator().reduce(into: 0, { (result, _) in result += 1 }) |
| 167 | + Benchmark("PoolManager/TaskExecutor: Lease/Release 1k requests: 10 parallel – \(count) NIO executors", configuration: .init(scalingFactor: .kilo)) { benchmark in |
| 168 | + let clock = MockClock() |
| 169 | + let factory = MockConnectionFactory<MockClock, NIOTaskExecutor>(autoMaxStreams: 1) |
| 170 | + var configuration = ConnectionPoolManagerConfiguration() |
| 171 | + try await NIOTaskExecutor.withExecutors(eventLoops) { executors in |
| 172 | + let concurrency = 50 |
| 173 | + |
| 174 | + configuration.maximumConnectionPerExecutorSoftLimit = concurrency / executors.count |
| 175 | + configuration.maximumConnectionPerExecutorHardLimit = concurrency / executors.count |
| 176 | + |
| 177 | + let pool = ConnectionPoolManager( |
| 178 | + configuration: configuration, |
| 179 | + connectionConfiguration: MockConnectionConfiguration(username: "username", password: "password"), |
| 180 | + idGenerator: ConnectionIDGenerator(), |
| 181 | + requestType: ConnectionRequest<MockConnection>.self, |
| 182 | + keepAliveBehavior: MockPingPongBehavior(keepAliveFrequency: nil, connectionType: MockConnection.self), |
| 183 | + executors: executors, |
| 184 | + observabilityDelegate: NoOpConnectionPoolMetrics(connectionIDType: MockConnection<NIOTaskExecutor>.ID.self), |
| 185 | + clock: clock |
| 186 | + ) { |
| 187 | + try await factory.makeConnection(id: $0, configuration: $1, for: $2) |
| 188 | + } |
| 189 | + |
| 190 | + await withTaskGroup { taskGroup in |
| 191 | + taskGroup.addTask { |
| 192 | + await pool.run() |
| 193 | + } |
| 194 | + |
| 195 | + let sequential = benchmark.scaledIterations.upperBound / executors.count |
| 196 | + |
| 197 | + benchmark.startMeasurement() |
| 198 | + |
| 199 | + for executor in executors { |
| 200 | + taskGroup.addTask(executorPreference: executor) { |
| 201 | + for _ in 0..<sequential { |
| 202 | + do { |
| 203 | + try await pool.withConnection { connection in |
| 204 | + blackHole(connection) |
| 205 | + } |
| 206 | + } catch { |
| 207 | + fatalError() |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + for _ in executors { |
| 214 | + await taskGroup.next() |
| 215 | + } |
| 216 | + |
| 217 | + benchmark.stopMeasurement() |
| 218 | + |
| 219 | + taskGroup.cancelAll() |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + } |
99 | 224 | }
|
0 commit comments