main.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Hummingbird
  2. import HummingbirdFoundation
  3. import PostgresNIO
  4. // tfb-server (aka, citrine) uses 28 hyper-threaded cores
  5. // postgresql.conf specifies max_connections = 2000
  6. //
  7. // 2000 / (28 * 2) = 35.7 (theoretical max)
  8. //
  9. // https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Environment#citrine-self-hosted
  10. // https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/toolset/databases/postgres/postgresql.conf#L64
  11. let maxConnectionsPerEventLoop = 32
  12. extension Int {
  13. func bound(_ minValue: Int, _ maxValue: Int) -> Int {
  14. return Swift.min(maxValue, Swift.max(minValue, self))
  15. }
  16. }
  17. extension HBApplication {
  18. var postgresConnectionGroup: HBConnectionPoolGroup<PostgresConnectionSource> {
  19. get { self.extensions.get(\.postgresConnectionGroup) }
  20. set {
  21. self.extensions.set(\.postgresConnectionGroup, value: newValue) { group in
  22. try group.close().wait()
  23. }
  24. }
  25. }
  26. }
  27. func runApp() throws {
  28. let env = HBEnvironment()
  29. let serverHostName = env.get("SERVER_HOSTNAME") ?? "127.0.0.1"
  30. let serverPort = env.get("SERVER_PORT", as: Int.self) ?? 8080
  31. let configuration = HBApplication.Configuration(
  32. address: .hostname(serverHostName, port: serverPort),
  33. serverName: "Hummingbird"
  34. )
  35. let app = HBApplication(configuration: configuration)
  36. app.encoder = JSONEncoder()
  37. app.postgresConnectionGroup = .init(
  38. source: .init(
  39. configuration: .init(
  40. connection: .init(host: "tfb-database"),
  41. authentication: .init(username: "benchmarkdbuser", database: "hello_world", password: "benchmarkdbpass"),
  42. tls: .disable
  43. )
  44. ),
  45. maxConnections: maxConnectionsPerEventLoop,
  46. eventLoopGroup: app.eventLoopGroup,
  47. logger: app.logger
  48. )
  49. WorldController(connectionPoolGroup: app.postgresConnectionGroup).add(to: app.router)
  50. FortunesController(connectionPoolGroup: app.postgresConnectionGroup).add(to: app.router)
  51. try app.start()
  52. app.wait()
  53. }
  54. try runApp()