@@ -16,11 +16,11 @@ struct WorldController {
16
16
/// simple database table. That row is then serialized as a JSON response.
17
17
@Sendable func single( request: Request , context: Context ) async throws -> World {
18
18
let id = Int32 . random ( in: 1 ... 10_000 )
19
- let rows = try await self . postgresClient. query ( " SELECT id, randomnumber FROM World WHERE id = \( id ) " )
20
- for try await (id , randomNumber ) in rows. decode ( ( Int32 , Int32 ) . self , context : . default ) {
21
- return World ( id : id , randomNumber : randomNumber )
19
+ let rows = try await self . postgresClient. execute ( SelectWorldStatement ( id : id ) )
20
+ guard let row = try await rows. first ( where : { _ in true } ) else {
21
+ throw HTTPError ( . notFound )
22
22
}
23
- throw HTTPError ( . notFound )
23
+ return World ( id : row . 0 , randomNumber : row . 1 )
24
24
}
25
25
26
26
/// In this test, each request is processed by fetching multiple rows from a
@@ -29,21 +29,16 @@ struct WorldController {
29
29
/// All tests are run at 512 concurrency.
30
30
@Sendable func multiple( request: Request , context: Context ) async throws -> [ World ] {
31
31
let queries = ( request. uri. queryParameters. get ( " queries " , as: Int . self) ?? 1 ) . bound ( 1 , 500 )
32
- return try await withThrowingTaskGroup ( of: World . self) { group in
32
+ return try await self . postgresClient. withConnection { conn in
33
+ var result : [ World ] = . init( )
34
+ result. reserveCapacity ( queries)
33
35
for _ in 0 ..< queries {
34
- group. addTask {
35
- let id = Int32 . random ( in: 1 ... 10_000 )
36
- let rows = try await self . postgresClient. query ( " SELECT id, randomnumber FROM World WHERE id = \( id) " )
37
- for try await (id, randomNumber) in rows. decode ( ( Int32, Int32) . self, context: . default) {
38
- return World ( id: id, randomNumber: randomNumber)
39
- }
36
+ let id = Int32 . random ( in: 1 ... 10_000 )
37
+ let rows = try await conn. execute ( SelectWorldStatement ( id: id) , logger: context. logger)
38
+ guard let row = try await rows. first ( where: { _ in true } ) else {
40
39
throw HTTPError ( . notFound)
41
40
}
42
- }
43
- var result : [ World ] = . init( )
44
- result. reserveCapacity ( queries)
45
- for try await world in group {
46
- result. append ( world)
41
+ result. append ( World ( id: row. 0 , randomNumber: row. 1 ) )
47
42
}
48
43
return result
49
44
}
@@ -59,25 +54,54 @@ struct WorldController {
59
54
/// query to fetch the object. All tests are run at 512 concurrency.
60
55
@Sendable func updates( request: Request , context: Context ) async throws -> [ World ] {
61
56
let queries = ( request. uri. queryParameters. get ( " queries " , as: Int . self) ?? 1 ) . bound ( 1 , 500 )
62
- return try await withThrowingTaskGroup ( of: World . self) { group in
57
+ return try await self . postgresClient. withConnection { conn in
58
+ var result : [ World ] = . init( )
59
+ result. reserveCapacity ( queries)
63
60
for _ in 0 ..< queries {
64
- group. addTask {
65
- let id = Int32 . random ( in: 1 ... 10_000 )
66
- let rows = try await self . postgresClient. query ( " SELECT id FROM World WHERE id = \( id) " )
67
- for try await (id) in rows. decode ( ( Int32) . self, context: . default) {
68
- let randomNumber = Int32 . random ( in: 1 ... 10_000 )
69
- try await self . postgresClient. query ( " UPDATE World SET randomnumber = \( randomNumber) WHERE id = \( id) " )
70
- return World ( id: id, randomNumber: randomNumber)
71
- }
61
+ let id = Int32 . random ( in: 1 ... 10_000 )
62
+ let rows = try await conn. execute ( SelectWorldStatement ( id: id) , logger: context. logger)
63
+ guard let row = try await rows. first ( where: { _ in true } ) else {
72
64
throw HTTPError ( . notFound)
73
65
}
74
- }
75
- var result : [ World ] = . init( )
76
- result. reserveCapacity ( queries)
77
- for try await world in group {
78
- result. append ( world)
66
+ let randomNumber = Int32 . random ( in: 1 ... 10_000 )
67
+ _ = try await conn. execute ( UpdateWorldStatement ( id: id, randomNumber: randomNumber) , logger: context. logger)
68
+ result. append ( World ( id: row. 0 , randomNumber: randomNumber) )
79
69
}
80
70
return result
81
71
}
82
72
}
73
+
74
+ struct SelectWorldStatement : PostgresPreparedStatement {
75
+ typealias Row = ( Int32 , Int32 )
76
+
77
+ let id : Int32
78
+
79
+ static var sql = " SELECT id, randomnumber FROM World WHERE id = $1 "
80
+
81
+ func makeBindings( ) throws -> PostgresNIO . PostgresBindings {
82
+ var bindings = PostgresNIO . PostgresBindings ( capacity: 1 )
83
+ bindings. append ( . init( int32: self . id) )
84
+ return bindings
85
+ }
86
+
87
+ func decodeRow( _ row: PostgresNIO . PostgresRow ) throws -> Row { try row. decode ( Row . self) }
88
+ }
89
+
90
+ struct UpdateWorldStatement : PostgresPreparedStatement {
91
+ typealias Row = Int32
92
+
93
+ let id : Int32
94
+ let randomNumber : Int32
95
+
96
+ static var sql = " UPDATE World SET randomnumber = $2 WHERE id = $1 "
97
+
98
+ func makeBindings( ) throws -> PostgresNIO . PostgresBindings {
99
+ var bindings = PostgresNIO . PostgresBindings ( capacity: 2 )
100
+ bindings. append ( . init( int32: self . id) )
101
+ bindings. append ( . init( int32: self . randomNumber) )
102
+ return bindings
103
+ }
104
+
105
+ func decodeRow( _ row: PostgresNIO . PostgresRow ) throws -> Row { try row. decode ( Row . self) }
106
+ }
83
107
}
0 commit comments