main.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import PostgresKit
  2. import Vapor
  3. var env = try Environment.detect()
  4. try LoggingSystem.bootstrap(from: &env)
  5. let app = Application(env)
  6. defer { app.shutdown() }
  7. app.http.server.configuration.serverName = "Vapor"
  8. app.logger.notice("💧 VAPOR")
  9. app.logger.notice("System.coreCount: \(System.coreCount)")
  10. app.logger.notice("System.maxConnectionsPerEventLoop: \(System.maxConnectionsPerEventLoop)")
  11. let pools = EventLoopGroupConnectionPool(
  12. source: PostgresConnectionSource(configuration: .init(
  13. hostname: "tfb-database",
  14. username: "benchmarkdbuser",
  15. password: "benchmarkdbpass",
  16. database: "hello_world"
  17. )),
  18. maxConnectionsPerEventLoop: System.maxConnectionsPerEventLoop,
  19. on: app.eventLoopGroup
  20. )
  21. extension Request {
  22. func sql(_ pools: EventLoopGroupConnectionPool<PostgresConnectionSource>) -> SQLDatabase {
  23. pools.pool(for: self.eventLoop).database(logger: self.logger).sql()
  24. }
  25. }
  26. app.get("db") { req in
  27. req.sql(pools).select()
  28. .column("id")
  29. .column("randomnumber")
  30. .from("World")
  31. .where("id", .equal, Int32.random(in: 1...10_000))
  32. .first(decoding: World.self)
  33. .unwrap(or: Abort(.notFound))
  34. }
  35. app.get("queries") { req -> EventLoopFuture<[World]> in
  36. let queries = (req.query["queries"] ?? 1).bounded(to: 1...500)
  37. let db = req.sql(pools)
  38. return (0 ..< queries).map { _ -> EventLoopFuture<World> in
  39. db.select()
  40. .column("id")
  41. .column("randomnumber")
  42. .from("World")
  43. .where("id", .equal, Int32.random(in: 1...10_000))
  44. .first(decoding: World.self)
  45. .unwrap(or: Abort(.notFound))
  46. }.flatten(on: req.eventLoop)
  47. }
  48. try app.run()