@@ -16,23 +16,27 @@ enum poolSize = 64;
16
16
17
17
PostgresClient client;
18
18
19
- shared static this ()
20
- {
21
- import derelict.pq.pq;
22
- import derelict.util.exception : ShouldThrow;
23
- ShouldThrow myMissingSymCB ( string symbolName ) { return ShouldThrow.No; }
24
- DerelictPQ.missingSymbolCallback = &myMissingSymCB;
25
- }
26
-
27
19
void main ()
28
20
{
29
- runWorkerTaskDist(&runServer);
30
- runApplication();
31
- }
21
+ auto connectionInfo = " host=tfb-database port=5432 "
22
+ ~ " dbname=hello_world user=benchmarkdbuser password=benchmarkdbpass" ;
23
+
24
+ client = new PostgresClient(connectionInfo, poolSize, (Connection cn){
25
+ cn.prepareEx(" fortune_prpq" , " SELECT id, message::text FROM Fortune" );
26
+ cn.prepareEx(" db_prpq" , " SELECT randomNumber, id FROM world WHERE id = $1" );
27
+ cn.prepareEx(" db_update_prpq" , " UPDATE world SET randomNumber = $1 WHERE id = $2" );
28
+ } );
29
+
30
+ {
31
+ // Establishing each connection in the pool and performing the PREPARE procedures
32
+ LockedConnection[poolSize] conns;
33
+
34
+ foreach (ref c; conns)
35
+ c = client.lockConnection();
36
+ }
32
37
33
- void runServer ()
34
- {
35
38
import std.datetime : seconds;
39
+
36
40
auto router = new URLRouter;
37
41
router.registerWebInterface(new WebInterface);
38
42
router.rebuild();
@@ -42,6 +46,7 @@ void runServer()
42
46
settings.options |= HTTPServerOption.reusePort;
43
47
settings.port = 8080 ;
44
48
listenHTTP(settings, router);
49
+ runEventLoop();
45
50
}
46
51
47
52
class WebInterface {
@@ -79,7 +84,6 @@ class WebInterface {
79
84
int id = _uniformVariable(_gen);
80
85
QueryParams qp;
81
86
qp.preparedStatementName(" db_prpq" );
82
- qp.resultFormat = ValueFormat.BINARY ;
83
87
qp.argsVariadic(id);
84
88
immutable result = conn.execPrepared(qp).rangify.front;
85
89
auto w = WorldResponse(id, result[0 ].as! PGinteger);
@@ -109,11 +113,9 @@ class WebInterface {
109
113
scope data = new WorldResponse[count];
110
114
QueryParams qp;
111
115
qp.preparedStatementName(" db_prpq" );
112
- qp.resultFormat = ValueFormat.BINARY ;
113
116
foreach (ref w; data) {
114
117
int id = _uniformVariable(_gen);
115
118
qp.argsVariadic(id);
116
- immutable query = " SELECT randomNumber, id FROM world WHERE id = " ~ id.to! string ;
117
119
immutable result = conn.execPrepared(qp).rangify.front;
118
120
w = WorldResponse(id, result[0 ].as! PGinteger);
119
121
}
@@ -147,7 +149,6 @@ class WebInterface {
147
149
import std.uni : isNumber;
148
150
149
151
auto conn = client.lockConnection();
150
- scope (exit) destroy (conn);
151
152
152
153
int count = 1 ;
153
154
if (queries.length && isNumber(queries[0 ]))
@@ -159,24 +160,22 @@ class WebInterface {
159
160
scope data = new WorldResponse[count];
160
161
QueryParams qp;
161
162
qp.preparedStatementName(" db_prpq" );
162
- qp.resultFormat = ValueFormat.BINARY ;
163
163
164
164
QueryParams qp_update;
165
165
qp_update.preparedStatementName(" db_update_prpq" );
166
- qp_update.resultFormat = ValueFormat.BINARY ;
167
166
168
167
foreach (ref w; data) {
169
168
int id = _uniformVariable(_gen);
170
169
qp.argsVariadic(id);
171
- immutable query = " SELECT id, randomNumber FROM world WHERE id = " ~ id.to! string ;
172
170
immutable result = conn.execPrepared(qp).rangify.front;
173
171
w = WorldResponse(id, result[0 ].as! PGinteger);
174
172
175
173
// update random number
176
174
w.randomNumber = _uniformVariable(_gen);
177
175
qp_update.argsVariadic(w.randomNumber, id);
176
+
178
177
// persist to DB
179
- conn.sendQueryPrepared (qp_update);
178
+ conn.execPrepared (qp_update);
180
179
}
181
180
182
181
// write response as JSON
@@ -203,15 +202,3 @@ struct FortuneResponse {
203
202
int id;
204
203
string message;
205
204
}
206
-
207
- static this ()
208
- {
209
- import std.process : environment;
210
- auto connectionInfo = " host=tfb-database port=5432 "
211
- ~ " dbname=hello_world user=benchmarkdbuser password=benchmarkdbpass" ;
212
- client = new PostgresClient(connectionInfo, poolSize, (Connection cn){
213
- cn.prepare(" fortune_prpq" , " SELECT id, message::text FROM Fortune" );
214
- cn.prepare(" db_prpq" , " SELECT randomNumber, id FROM world WHERE id = $1" );
215
- cn.prepare(" db_update_prpq" , " UPDATE world SET randomNumber = $1 WHERE id = $2" );
216
- } );
217
- }
0 commit comments