main.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Hummingbird
  2. import PostgresNIO
  3. // postgresql.conf specifies max_connections = 2000
  4. // https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Environment#citrine-self-hosted
  5. // https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/toolset/databases/postgres/postgresql.conf#L64
  6. extension Int {
  7. func bound(_ minValue: Int, _ maxValue: Int) -> Int {
  8. return Swift.min(maxValue, Swift.max(minValue, self))
  9. }
  10. }
  11. struct TechFrameworkRequestContext: RequestContext {
  12. static let jsonEncoder = JSONEncoder()
  13. static let jsonDecoder = JSONDecoder()
  14. var coreContext: Hummingbird.CoreRequestContext
  15. // Use a global JSON Encoder
  16. var responseEncoder: JSONEncoder { Self.jsonEncoder }
  17. // Use a global JSON Decoder
  18. var requestDecoder: JSONDecoder { Self.jsonDecoder }
  19. init(channel: any Channel, logger: Logger) {
  20. self.coreContext = .init(allocator: channel.allocator, logger: logger)
  21. }
  22. }
  23. func runApp() async throws {
  24. let env = Environment()
  25. let serverHostName = env.get("SERVER_HOSTNAME") ?? "127.0.0.1"
  26. let serverPort = env.get("SERVER_PORT", as: Int.self) ?? 8080
  27. var postgresConfiguration = PostgresClient.Configuration(
  28. host: "tfb-database",
  29. username: "benchmarkdbuser",
  30. password: "benchmarkdbpass",
  31. database: "hello_world",
  32. tls: .disable
  33. )
  34. postgresConfiguration.options.maximumConnections = 1900
  35. let postgresClient = PostgresClient(
  36. configuration: postgresConfiguration,
  37. eventLoopGroup: MultiThreadedEventLoopGroup.singleton
  38. )
  39. let router = Router(context: TechFrameworkRequestContext.self)
  40. router.addRoutes(WorldController(postgresClient: postgresClient).routes)
  41. router.addRoutes(FortunesController(postgresClient: postgresClient).routes)
  42. var app = Application(
  43. router: router,
  44. configuration: .init(
  45. address: .hostname(serverHostName, port: serverPort),
  46. serverName: "HB2",
  47. backlog: 8192
  48. )
  49. )
  50. app.addServices(postgresClient)
  51. try await app.runService()
  52. }
  53. try await runApp()