-
Notifications
You must be signed in to change notification settings - Fork 75
/
index.js
245 lines (205 loc) Β· 7.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/// <reference types="es4x" />
// @ts-check
import {Router} from '@vertx/web';
import {PgClient, Tuple} from '@reactiverse/reactive-pg-client';
import {PgPoolOptions} from '@reactiverse/reactive-pg-client/options';
import {HandlebarsTemplateEngine} from '@vertx/web-templ-handlebars'
const util = require('./util');
const SERVER = 'vertx.js';
const app = Router.router(vertx);
const template = HandlebarsTemplateEngine.create(vertx);
let date = new Date().toUTCString();
vertx.setPeriodic(1000, t => date = new Date().toUTCString());
/*
* This test exercises the framework fundamentals including keep-alive support, request routing, request header
* parsing, object instantiation, JSON serialization, response header generation, and request count throughput.
*/
app.get("/json").handler(ctx => {
ctx.response()
.putHeader("Server", SERVER)
.putHeader("Date", date)
.putHeader("Content-Type", "application/json")
.end(JSON.stringify({message: 'Hello, World!'}));
});
const UPDATE_WORLD = "UPDATE world SET randomnumber=$1 WHERE id=$2";
const SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
const SELECT_FORTUNE = "SELECT id, message from FORTUNE";
let client = PgClient.pool(
vertx,
new PgPoolOptions()
.setCachePreparedStatements(true)
.setMaxSize(1)
.setHost('localhost')
.setUser('benchmarkdbuser')
.setPassword('benchmarkdbpass')
.setDatabase('hello_world'));
/*
* This test exercises the framework's object-relational mapper (ORM), random number generator, database driver,
* and database connection pool.
*/
app.get("/db").handler(ctx => {
client.preparedQuery(SELECT_WORLD, Tuple.of(util.randomWorld()), res => {
if (res.succeeded()) {
let resultSet = res.result().iterator();
if (!resultSet.hasNext()) {
ctx.fail(404);
return;
}
let row = resultSet.next();
ctx.response()
.putHeader("Server", SERVER)
.putHeader("Date", date)
.putHeader("Content-Type", "application/json")
.end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)}));
} else {
ctx.fail(res.cause());
}
})
});
/*
* This test is a variation of Test #2 and also uses the World table. Multiple rows are fetched to more dramatically
* punish the database driver and connection pool. At the highest queries-per-request tested (20), this test
* demonstrates all frameworks' convergence toward zero requests-per-second as database activity increases.
*/
app.get("/queries").handler(ctx => {
let failed = false;
let worlds = [];
const queries = util.getQueries(ctx.request());
for (let i = 0; i < queries; i++) {
client.preparedQuery(SELECT_WORLD, Tuple.of(util.randomWorld()), ar => {
if (!failed) {
if (ar.failed()) {
failed = true;
ctx.fail(ar.cause());
return;
}
// we need a final reference
const row = ar.result().iterator().next();
worlds.push({id: row.getInteger(0), randomNumber: row.getInteger(1)});
// stop condition
if (worlds.length === queries) {
ctx.response()
.putHeader("Server", SERVER)
.putHeader("Date", date)
.putHeader("Content-Type", "application/json")
.end(JSON.stringify(worlds));
}
}
});
}
});
/*
* This test exercises the ORM, database connectivity, dynamic-size collections, sorting, server-side templates,
* XSS countermeasures, and character encoding.
*/
app.get("/fortunes").handler(ctx => {
client.preparedQuery(SELECT_FORTUNE, ar => {
if (ar.failed()) {
ctx.fail(ar.cause());
return;
}
let fortunes = [];
let resultSet = ar.result().iterator();
if (!resultSet.hasNext()) {
ctx.fail(404);
return;
}
while (resultSet.hasNext()) {
let row = resultSet.next();
fortunes.push({id: row.getInteger(0), message: row.getString(1)});
}
fortunes.push({id: 0, message: "Additional fortune added at request time."});
fortunes.sort((a, b) => {
let messageA = a.message;
let messageB = b.message;
if (messageA < messageB) {
return -1;
}
if (messageA > messageB) {
return 1;
}
return 0;
});
// and now delegate to the engine to render it.
template.render({fortunes: fortunes}, "templates/fortunes.hbs", res => {
if (res.succeeded()) {
ctx.response()
.putHeader("Server", SERVER)
.putHeader("Date", date)
.putHeader("Content-Type", "text/html; charset=UTF-8")
.end(res.result());
} else {
ctx.fail(res.cause());
}
});
});
});
/*
* This test is a variation of Test #3 that exercises the ORM's persistence of objects and the database driver's
* performance at running UPDATE statements or similar. The spirit of this test is to exercise a variable number of
* read-then-write style database operations.
*/
app.route("/updates").handler(ctx => {
let failed = false;
let queryCount = 0;
let worlds = [];
const queries = util.getQueries(ctx.request());
for (let i = 0; i < queries; i++) {
const id = util.randomWorld();
const index = i;
client.preparedQuery(SELECT_WORLD, Tuple.of(id), ar => {
if (!failed) {
if (ar.failed()) {
failed = true;
ctx.fail(ar.cause());
return;
}
const row = ar.result().iterator().next();
worlds[index] = {id: row.getInteger(0), randomNumber: row.getInteger(1)};
worlds[index].randomNumber = util.randomWorld();
if (++queryCount === queries) {
worlds.sort((a, b) => {
return a.id - b.id;
});
let batch = [];
worlds.forEach(world => {
batch.push(Tuple.of(world.randomNumber, world.id));
});
client.preparedBatch(UPDATE_WORLD, batch, ar => {
if (ar.failed()) {
ctx.fail(ar.cause());
return;
}
let json = [];
worlds.forEach(world => {
json.push({id: world.id, randomNumber: world.randomNumber});
});
ctx.response()
.putHeader("Server", SERVER)
.putHeader("Date", date)
.putHeader("Content-Type", "application/json")
.end(JSON.stringify(json));
});
}
}
});
}
});
/*
* This test is an exercise of the request-routing fundamentals only, designed to demonstrate the capacity of
* high-performance platforms in particular. Requests will be sent using HTTP pipelining. The response payload is
* still small, meaning good performance is still necessary in order to saturate the gigabit Ethernet of the test
* environment.
*/
app.get("/plaintext").handler(ctx => {
ctx.response()
.putHeader("Server", SERVER)
.putHeader("Date", date)
.putHeader("Content-Type", "text/plain")
.end('Hello, World!');
});
vertx
.createHttpServer()
.requestHandler(app)
.listen(8080);
console.log('Server listening at: http://localhost:8080/');