|
| 1 | +// |
| 2 | +// PostgresPerf.swift |
| 3 | +// benchmarks |
| 4 | +// |
| 5 | +// Created by Fabian Fett on 12.05.25. |
| 6 | +// |
| 7 | + |
| 8 | +import Synchronization |
| 9 | +import PostgresNIO |
| 10 | +@preconcurrency import PostgresKit |
| 11 | +@preconcurrency import AsyncKit |
| 12 | + |
| 13 | +@main |
| 14 | +@available(macOS 15.0, *) |
| 15 | +enum PostgresPerf { |
| 16 | + |
| 17 | + static let maxConnections: Int = 400 |
| 18 | + static let tasks: Int = 400 |
| 19 | + static let iterationsPerTask: Int = 1000 |
| 20 | + static let logger = Logger(label: "TestLogger") |
| 21 | + static let clock = ContinuousClock() |
| 22 | + |
| 23 | + static let eventLoopCount = { |
| 24 | + NIOSingletons.posixEventLoopGroup.makeIterator().reduce(0, { (res, _) in res + 1 }) |
| 25 | + }() |
| 26 | + |
| 27 | + static func main() async throws { |
| 28 | +// if CommandLine.arguments.first == "kit" { |
| 29 | + try await Self.runPostgresKit() |
| 30 | +// } else { |
| 31 | + try await self.runPostgresNIO() |
| 32 | +// } |
| 33 | + } |
| 34 | + |
| 35 | + static func runPostgresKit() async throws { |
| 36 | + let configuration = SQLPostgresConfiguration( |
| 37 | + hostname: "localhost", port: 5432, |
| 38 | + username: "test_username", |
| 39 | + password: "test_password", |
| 40 | + database: "test_database", |
| 41 | + tls: .disable |
| 42 | + ) |
| 43 | + |
| 44 | + let pools = EventLoopGroupConnectionPool( |
| 45 | + source: PostgresConnectionSource(sqlConfiguration: configuration), |
| 46 | + maxConnectionsPerEventLoop: Self.maxConnections / Self.eventLoopCount, |
| 47 | + on: NIOSingletons.posixEventLoopGroup |
| 48 | + ) |
| 49 | + |
| 50 | + let start = self.clock.now |
| 51 | + await withThrowingTaskGroup(of: Void.self) { taskGroup in |
| 52 | + for _ in 0..<Self.tasks { |
| 53 | + taskGroup.addTask { |
| 54 | + for _ in 0..<Self.iterationsPerTask { |
| 55 | + _ = try await pools.withConnection { connection in |
| 56 | + connection.query("SELECT 1;", logger: Self.logger) { row in |
| 57 | + let foo = try row.decode(Int.self) |
| 58 | + } |
| 59 | + }.get() |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + for _ in 0..<Self.tasks { |
| 65 | + _ = await taskGroup.nextResult()! |
| 66 | + } |
| 67 | + |
| 68 | + taskGroup.cancelAll() |
| 69 | + } |
| 70 | + |
| 71 | + try await pools.shutdownAsync() |
| 72 | + let end = self.clock.now |
| 73 | + |
| 74 | + self.logger.info("PostgresKit completed", metadata: ["Took": "\(end - start)"]) |
| 75 | + } |
| 76 | + |
| 77 | + static func runPostgresNIO() async throws { |
| 78 | + var tlsConfiguration = TLSConfiguration.makeClientConfiguration() |
| 79 | + tlsConfiguration.certificateVerification = .none |
| 80 | + var clientConfig = PostgresClient.Configuration( |
| 81 | + host: env("POSTGRES_HOSTNAME") ?? "localhost", |
| 82 | + port: env("POSTGRES_PORT").flatMap({ Int($0) }) ?? 5432, |
| 83 | + username: env("POSTGRES_USER") ?? "test_username", |
| 84 | + password: env("POSTGRES_PASSWORD") ?? "test_password", |
| 85 | + database: env("POSTGRES_DB") ?? "test_database", |
| 86 | + tls: .prefer(tlsConfiguration) |
| 87 | + ) |
| 88 | + clientConfig.options.minimumConnections = 0 |
| 89 | + clientConfig.options.maximumConnections = Self.maxConnections |
| 90 | + clientConfig.options.keepAliveBehavior = .init(frequency: .seconds(5)) |
| 91 | + clientConfig.options.connectionIdleTimeout = .seconds(15) |
| 92 | + |
| 93 | + |
| 94 | + let client = PostgresClient( |
| 95 | + configuration: clientConfig, |
| 96 | + eventLoopGroup: NIOSingletons.posixEventLoopGroup |
| 97 | + ) |
| 98 | + |
| 99 | + let start = self.clock.now |
| 100 | + await withThrowingTaskGroup(of: Void.self) { taskGroup in |
| 101 | + taskGroup.addTask { |
| 102 | + await client.run() |
| 103 | + } |
| 104 | + |
| 105 | +// let atomic = Atomic(0) |
| 106 | + for _ in 0..<Self.tasks { |
| 107 | + taskGroup.addTask { |
| 108 | + for _ in 0..<Self.iterationsPerTask { |
| 109 | + let rows = try await client.query("SELECT 1;") |
| 110 | + for try await row in rows.decode(Int.self) { |
| 111 | +// atomic.add(row, ordering: .relaxed) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + for _ in 0..<Self.tasks { |
| 118 | + _ = await taskGroup.nextResult()! |
| 119 | + } |
| 120 | + |
| 121 | + taskGroup.cancelAll() |
| 122 | + } |
| 123 | + let end = self.clock.now |
| 124 | + |
| 125 | + self.logger.info("PostgresNIO completed", metadata: ["Took": "\(end - start)"]) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func env(_ name: String) -> String? { |
| 130 | + getenv(name).flatMap { String(cString: $0) } |
| 131 | +} |
0 commit comments