Browse Source

[vapor-postgres] Add database updates test (#6938)

Vojtech Rylko 3 years ago
parent
commit
81adfb7659

+ 5 - 1
frameworks/Swift/vapor/README.md

@@ -31,4 +31,8 @@ http://localhost:8080/db
 
 
 ### Multiple database queries test
 ### Multiple database queries test
 
 
-http://localhost:8080/queries/[1...500]
+http://localhost:8080/queries?queries=[1...500]
+
+### Database updates test
+
+http://localhost:8080/updates?queries=[1...500]

+ 1 - 0
frameworks/Swift/vapor/benchmark_config.json

@@ -59,6 +59,7 @@
     "postgres": {
     "postgres": {
       "db_url": "/db",
       "db_url": "/db",
       "query_url": "/queries?queries=",
       "query_url": "/queries?queries=",
+      "update_url": "/updates?queries=",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Fullstack",
       "classification": "Fullstack",

+ 1 - 0
frameworks/Swift/vapor/config.toml

@@ -28,6 +28,7 @@ versus = "None"
 [postgres]
 [postgres]
 urls.db = "/db"
 urls.db = "/db"
 urls.query = "/queries?queries="
 urls.query = "/queries?queries="
+urls.update = "/updates?queries="
 approach = "Realistic"
 approach = "Realistic"
 classification = "Fullstack"
 classification = "Fullstack"
 database = "Postgres"
 database = "Postgres"

+ 41 - 0
frameworks/Swift/vapor/vapor-postgres/Sources/main.swift

@@ -70,6 +70,47 @@ app.get("queries") { req async throws -> [World] in
     return worlds
     return worlds
 }
 }
 
 
+// Database Updates test
+//
+app.get("updates") { req async throws -> [World] in
+    let queries = (req.query["queries"] ?? 1).bounded(to: 1...500)
+
+    var worlds: [World] = []
+
+    let db = req.db(pools)
+
+    for _ in queries {
+        // Get
+        //
+        let rows = try await db.query(
+            "SELECT id, randomnumber FROM World WHERE id = $1 LIMIT 1",
+            [PostgresData(int32: .random(in: 1...10_000))]
+        ).get()
+
+        guard let row = rows.first else {
+            throw Abort(.notFound)
+        }
+
+        var world =  World(
+            id: row.column("id")!.int32!,
+            randomnumber: row.column("randomnumber")!.int!
+        )
+
+        // Update
+        //
+        world.randomnumber = .random(in: 1...10_000)
+
+        _ = try await db.query(
+            "UPDATE World SET randomnumber = $1 WHERE id = $2",
+            [PostgresData(int: world.randomnumber), PostgresData(int32: world.id!)]
+        ).get()
+
+        worlds.append(world)
+    }
+
+    return worlds
+}
+
 extension Int: Sequence {
 extension Int: Sequence {
     public func makeIterator() -> CountableRange<Int>.Iterator {
     public func makeIterator() -> CountableRange<Int>.Iterator {
         return (0..<self).makeIterator()
         return (0..<self).makeIterator()