|
1 | 1 | import std.stdio; |
2 | 2 | import std.socket; |
3 | 3 | import std.array; |
| 4 | +import std.algorithm; |
| 5 | +import std.conv; |
| 6 | +import std.ascii; |
4 | 7 |
|
5 | 8 | import mir.ser; |
6 | 9 | import mir.ser.json; |
7 | 10 |
|
| 11 | +import core.time; |
| 12 | + |
8 | 13 | import std.range.primitives; |
9 | 14 |
|
10 | 15 | import glow.xbuf; |
11 | 16 |
|
12 | 17 | import photon, photon.http; |
13 | 18 |
|
14 | | -struct Message |
15 | | -{ |
| 19 | +import mir.random : unpredictableSeedOf; |
| 20 | +import mir.random.variable : UniformVariable; |
| 21 | +import mir.random.engine.xorshift : Xorshift; |
| 22 | + |
| 23 | +import dpq2; |
| 24 | + |
| 25 | +struct Message { |
16 | 26 | string message; |
17 | 27 | } |
18 | 28 |
|
| 29 | +struct WorldResponse { |
| 30 | + int id; |
| 31 | + int randomNumber; |
| 32 | +} |
| 33 | + |
| 34 | +enum connectionInfo = "host=tfb-database port=5432 " |
| 35 | + ~ "dbname=hello_world user=benchmarkdbuser password=benchmarkdbpass"; |
| 36 | +enum worldSize = 10000; |
| 37 | +enum poolSize = 64; |
| 38 | + |
| 39 | +shared Pool!Connection connectionPool; |
| 40 | + |
19 | 41 | class BenchmarkProcessor : HttpProcessor { |
20 | | - HttpHeader[] plainText = [HttpHeader("Content-Type", "text/plain; charset=utf-8")]; |
21 | | - HttpHeader[] json = [HttpHeader("Content-Type", "application/json")]; |
| 42 | + HttpHeader[] plainTextHeaders = [HttpHeader("Content-Type", "text/plain; charset=utf-8")]; |
| 43 | + HttpHeader[] jsonHeaders = [HttpHeader("Content-Type", "application/json")]; |
22 | 44 | Buffer!char jsonBuf; |
| 45 | + Buffer!WorldResponse worlds; |
| 46 | + UniformVariable!uint uniformVariable; |
| 47 | + Xorshift gen; |
| 48 | + |
23 | 49 | this(Socket sock) { |
24 | 50 | super(sock); |
| 51 | + gen = Xorshift(unpredictableSeed!uint); |
| 52 | + uniformVariable = UniformVariable!uint(1, worldSize); |
25 | 53 | jsonBuf = Buffer!char(256); |
| 54 | + worlds = Buffer!WorldResponse(500); |
26 | 55 | } |
27 | 56 |
|
28 | 57 | override void handle(HttpRequest req) { |
29 | 58 | if (req.uri == "/plaintext") { |
30 | | - respondWith("Hello, world!", HttpStatus.OK, plainText); |
| 59 | + plaintext(); |
31 | 60 | } else if(req.uri == "/json") { |
32 | | - jsonBuf.clear(); |
33 | | - serializeJsonPretty!""(jsonBuf, Message("Hello, World!")); |
34 | | - respondWith(jsonBuf.data, HttpStatus.OK, json); |
35 | | - } else { |
36 | | - respondWith("Not found", HttpStatus.NotFound, plainText); |
| 61 | + json(); |
| 62 | + } else if(req.uri == "/db") { |
| 63 | + db(); |
| 64 | + } else if(req.uri.startsWith("/queries")) { |
| 65 | + queries(req.uri); |
| 66 | + } else if(req.uri.startsWith("/updates")) { |
| 67 | + updates(req.uri); |
| 68 | + } |
| 69 | + else { |
| 70 | + respondWith("Not found", HttpStatus.NotFound, plainTextHeaders); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + final void plaintext() { |
| 75 | + respondWith("Hello, world!", HttpStatus.OK, plainTextHeaders); |
| 76 | + } |
| 77 | + |
| 78 | + final void json() { |
| 79 | + jsonBuf.clear(); |
| 80 | + serializeJsonPretty!""(jsonBuf, Message("Hello, World!")); |
| 81 | + respondWith(jsonBuf.data, HttpStatus.OK, jsonHeaders); |
| 82 | + } |
| 83 | + |
| 84 | + final void db() { |
| 85 | + jsonBuf.clear(); |
| 86 | + int id = uniformVariable(gen); |
| 87 | + auto c = connectionPool.acquire(); |
| 88 | + scope(exit) connectionPool.release(c); |
| 89 | + QueryParams qp; |
| 90 | + qp.preparedStatementName("db_prpq"); |
| 91 | + qp.argsVariadic(id); |
| 92 | + immutable result = c.execPrepared(qp).rangify.front; |
| 93 | + auto w = WorldResponse(id, result[0].as!PGinteger); |
| 94 | + serializeJsonPretty!""(jsonBuf, w); |
| 95 | + respondWith(jsonBuf.data, HttpStatus.OK, jsonHeaders); |
| 96 | + } |
| 97 | + |
| 98 | + // GET /queries?queries=... |
| 99 | + final void queries(const(char)[] uri) { |
| 100 | + jsonBuf.clear(); |
| 101 | + worlds.clear(); |
| 102 | + auto c = connectionPool.acquire(); |
| 103 | + scope(exit) connectionPool.release(c); |
| 104 | + |
| 105 | + int count = 1; |
| 106 | + auto i = uri.indexOf("?queries="); |
| 107 | + if (i > 0 && i + 9 <= uri.length && isDigit(uri[i+9])) |
| 108 | + { |
| 109 | + try count = min(max(uri[i+9..$].to!int, 1), 500); |
| 110 | + catch (ConvException) {} |
| 111 | + } |
| 112 | + |
| 113 | + QueryParams qp; |
| 114 | + qp.preparedStatementName("db_prpq"); |
| 115 | + foreach (_; 0..count) { |
| 116 | + int id = uniformVariable(gen); |
| 117 | + qp.argsVariadic(id); |
| 118 | + immutable result = c.execPrepared(qp).rangify.front; |
| 119 | + worlds.put(WorldResponse(id, result[0].as!PGinteger)); |
| 120 | + } |
| 121 | + serializeJsonPretty!""(jsonBuf, worlds.data); |
| 122 | + respondWith(jsonBuf.data, HttpStatus.OK, jsonHeaders); |
| 123 | + } |
| 124 | + |
| 125 | + // GET /updates?queries=... |
| 126 | + final void updates(const(char)[] uri) { |
| 127 | + jsonBuf.clear(); |
| 128 | + worlds.clear(); |
| 129 | + auto c = connectionPool.acquire(); |
| 130 | + scope(exit) connectionPool.release(c); |
| 131 | + |
| 132 | + int count = 1; |
| 133 | + auto i = uri.indexOf("?queries="); |
| 134 | + if (i > 0 && i + 9 <= uri.length && isDigit(uri[i+9])) |
| 135 | + { |
| 136 | + try count = min(max(uri[i+9..$].to!int, 1), 500); |
| 137 | + catch (ConvException) {} |
| 138 | + } |
| 139 | + |
| 140 | + QueryParams qp; |
| 141 | + qp.preparedStatementName("db_prpq"); |
| 142 | + |
| 143 | + QueryParams qp_update; |
| 144 | + qp_update.preparedStatementName("db_update_prpq"); |
| 145 | + |
| 146 | + foreach (_; 0..count) { |
| 147 | + int id = uniformVariable(gen); |
| 148 | + qp.argsVariadic(id); |
| 149 | + immutable result = c.execPrepared(qp).rangify.front; |
| 150 | + auto w = WorldResponse(id, result[0].as!PGinteger); |
| 151 | + |
| 152 | + // update random number |
| 153 | + w.randomNumber = uniformVariable(gen); |
| 154 | + qp_update.argsVariadic(w.randomNumber, id); |
| 155 | + |
| 156 | + // persist to DB |
| 157 | + c.execPrepared(qp_update); |
| 158 | + worlds.put(w); |
37 | 159 | } |
| 160 | + serializeJsonPretty!""(jsonBuf, worlds.data); |
| 161 | + respondWith(jsonBuf.data, HttpStatus.OK, jsonHeaders); |
38 | 162 | } |
39 | 163 | } |
40 | 164 |
|
@@ -75,6 +199,14 @@ void server() { |
75 | 199 |
|
76 | 200 | void main() { |
77 | 201 | initPhoton(); |
| 202 | + connectionPool = pool(poolSize, 15.seconds, () { |
| 203 | + auto c = new Connection(connectionInfo); |
| 204 | + c.prepareEx("db_prpq", "SELECT randomNumber, id FROM world WHERE id = $1"); |
| 205 | + c.prepareEx("db_update_prpq", "UPDATE world SET randomNumber = $1 WHERE id = $2"); |
| 206 | + return c; |
| 207 | + }, (ref Connection c) { |
| 208 | + destroy(c); |
| 209 | + }); |
78 | 210 | go(() => server()); |
79 | 211 | runScheduler(); |
80 | 212 | } |
0 commit comments