-
Notifications
You must be signed in to change notification settings - Fork 1
/
json-stream-benchmark.js
360 lines (304 loc) · 11 KB
/
json-stream-benchmark.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
var async = require('async');
var cpu = require('./cpu');
var MS_PER_TEST = 3000;
var MS_SLEEP_BETWEEN_TESTS = 1500;
var benchmarks = [
require('./streamers/json'),
require('./streamers/json-delay'),
require('./streamers/JSONStream'),
require('./streamers/JSONStream-delay')
];
var tests = [
require('./tests/numerics'),
require('./tests/mixed'),
require('./tests/heavy-nested'),
require('./tests/huge-complex'),
require('./tests/huge-buffer')
];
var eventLoop = {
count: 0,
totalLag: 0,
lastCheck: Date.now()
};
var isRunning = true;
function updateEventLoopLag() {
var now = Date.now();
var elapsed = (now - eventLoop.lastCheck);
if (elapsed === 0) return isRunning && setImmediate(updateEventLoopLag); // ignore zero deltas to avoid polluting the data
eventLoop.count++;
eventLoop.totalLag += elapsed;
eventLoop.lastCheck = now;
if (isRunning) {
setImmediate(updateEventLoopLag);
}
}
function resetEventLoop() {
eventLoop = {
count: 0,
totalLag: 0,
lastCheck: Date.now()
};
}
function getEventLoopLag() {
return Math.round(eventLoop.totalLag / eventLoop.count);
}
setImmediate(updateEventLoopLag);
function runBenchmark(benchmark, cb) {
benchmark.timeElapsed = 0;
benchmark.cpuTimeElapsed = 0;
benchmark.throughputKBsec = 0;
benchmark.results = {};
var testTasks = [];
tests.forEach(function(test) {
testTasks.push(createTestTask(benchmark, test));
});
console.log('* Benching ' + benchmark.name + '...');
async.series(testTasks, function(err) {
if (err) {
return cb(err);
}
cb();
});
}
function createTestTask(benchmark, test) {
var objs = [];
for (var i = 0; i < 10; i++) {
objs.push(test.fn());
}
var objJsons = objs.map(function(obj) {
return JSON.stringify(obj);
});
benchmark.factor = 1.0 / benchmark.multiple;
var getObjectToTest = function() {
var rnd = Math.floor(Math.random() * objs.length);
return {
obj: objs[rnd],
json: objJsons[rnd]
}
};
return function(cb) {
async.series([
function(cb) {
if (!benchmark.writer) {
return setImmediate(cb);
}
var objToTest = getObjectToTest();
// verify writer working
benchmark.writer(objToTest.obj, function(err, json) {
if (err) {
return cb(err);
}
if (json != objToTest.json) {
return cb(new Error('Writer result was NOT VALID! ' + json + ' != ' + objToTest.json));
}
cb();
});
},
function(cb) {
if (!benchmark.reader) {
return setImmediate(cb);
}
var objToTest = getObjectToTest();
// verify writer working
benchmark.reader(objToTest.json, function (err, o) {
if (err) {
return cb(err);
}
var json = JSON.stringify(o);
if (json != objToTest.json) {
return cb(new Error('Reader result was NOT VALID! ' + json + ' != ' + objToTest.json));
}
cb();
});
},
function(cb) {
if (!benchmark.writer) {
console.log('* No write test for ', benchmark.name, ' - skipping...');
return setImmediate(cb);
}
var results = benchmark.results[test.name] = benchmark.results[test.name] || {};
var name = ' ' + test.name + '.write';
results.write = {
paddedName: name + Array(30 - name.length).join(' '),
start: Date.now(),
cpuTime: cpu.getTotalCpuTimeInMs()
};
resetEventLoop();
var cycles = 0;
console.log('* Testing... ' + name);
var performNextIteration = function() {
var objToTest = getObjectToTest();
benchmark.writer(objToTest.obj, function(err) {
if (err) {
return cb(err);
}
cycles++;
var now = Date.now();
if ((now - results.write.start) > MS_PER_TEST) {
results.write.timeElapsed = now - results.write.start;
results.write.cpuTimeElapsed = Math.round(cpu.getTotalCpuTimeInMs() - results.write.cpuTime);
results.write.throughputKBsec = Math.round(cycles * objToTest.json.length * (MS_PER_TEST / results.write.timeElapsed) / 1024);
return void setTimeout(function() {
results.write.eventLoopLag = getEventLoopLag();
benchmark.timeElapsed += results.write.timeElapsed;
benchmark.cpuTimeElapsed += results.write.cpuTimeElapsed;
benchmark.throughputKBsec += results.write.throughputKBsec;
setTimeout(cb, MS_SLEEP_BETWEEN_TESTS);
}, 100);
}
process.nextTick(performNextIteration);
});
};
process.nextTick(performNextIteration);
},
function(cb) {
if (!benchmark.reader) {
console.log('* No read test for ', benchmark.name, ' - skipping...');
return setImmediate(cb);
}
var results = benchmark.results[test.name] = benchmark.results[test.name] || {};
var name = ' ' + test.name + '.read';
results.read = {
paddedName: name + Array(30 - name.length).join(' '),
start: Date.now(),
cpuTime: cpu.getTotalCpuTimeInMs()
};
resetEventLoop();
var cycles = 0;
console.log('* Testing... ' + name);
var performNextIteration = function() {
var objToTest = getObjectToTest();
benchmark.reader(objToTest.json, function(err) {
if (err) {
return cb(err);
}
cycles++;
var now = Date.now();
if ((now - results.read.start) > MS_PER_TEST) {
results.read.timeElapsed = now - results.read.start;
results.read.cpuTimeElapsed = Math.round(cpu.getTotalCpuTimeInMs() - results.read.cpuTime);
results.read.throughputKBsec = Math.round(cycles * objToTest.json.length * (MS_PER_TEST / results.read.timeElapsed) / 1024);
return void setTimeout(function() {
results.read.eventLoopLag = getEventLoopLag();
benchmark.timeElapsed += results.read.timeElapsed;
benchmark.cpuTimeElapsed += results.read.cpuTimeElapsed;
benchmark.throughputKBsec += results.read.throughputKBsec;
setTimeout(cb, MS_SLEEP_BETWEEN_TESTS);
}, 100);
}
process.nextTick(performNextIteration);
});
};
process.nextTick(performNextIteration);
}
], function(err) {
if (err) {
return cb(err);
}
cb();
});
}
}
var benchmarkTasks = [];
benchmarks.forEach(function(benchmark) {
benchmark.paddedName = benchmark.name + Array(30 - benchmark.name.length).join(' ');
benchmarkTasks.push(createBenchmarkTask(benchmark));
});
function createBenchmarkTask(benchmark) {
return function(cb) {
runBenchmark(benchmark, function() {
var lag = 0;
var lagCount = 0;
Object.keys(benchmark.results).forEach(function(k) {
var result = benchmark.results[k];
if (result.read && result.read.eventLoopLag) {
lag += result.read.eventLoopLag;
lagCount++;
}
if (result.write && result.write.eventLoopLag) {
lag += result.write.eventLoopLag;
lagCount++;
}
});
benchmark.eventLoopLag = Math.round(lag / lagCount);
cb();
});
};
}
// run it
async.series(benchmarkTasks, function(err) {
if (err) {
return console.log('* ERROR:', err);
}
console.log('* ALL BENCHMARKS COMPLETE');
printResults();
// done
});
function printResults() {
isRunning = false;
console.log('* RESULTS:');
console.log('\r\n* Throughput (KByte/sec):');
benchmarks.forEach(function(benchmark) {
printThroughput(benchmark, benchmark, benchmarks[0]);
Object.keys(benchmark.results).forEach(function(k) {
printThroughput(benchmark, benchmark.results[k].read, benchmarks[0].results[k].read);
printThroughput(benchmark, benchmark.results[k].write, benchmarks[0].results[k].write);
});
});
/*console.log('\r\n* Wall Clock (ms):');
benchmarks.forEach(function(benchmark) {
printWallClock(benchmark, benchmark, benchmarks[0]);
Object.keys(benchmark.results).forEach(function(k) {
printWallClock(benchmark, benchmark.results[k].read, benchmarks[0].results[k].read);
printWallClock(benchmark, benchmark.results[k].write, benchmarks[0].results[k].write);
});
});*/
console.log('\r\n* CPU Clock (ms): *** NOTE: Includes all cpu usage -- run only on idle system');
benchmarks.forEach(function(benchmark) {
printCPUClock(benchmark, benchmark, benchmarks[0]);
Object.keys(benchmark.results).forEach(function(k) {
printCPUClock(benchmark, benchmark.results[k].read, benchmarks[0].results[k].read);
printCPUClock(benchmark, benchmark.results[k].write, benchmarks[0].results[k].write);
});
});
console.log('\r\n* Avg Event Lag (ms):');
benchmarks.forEach(function(benchmark) {
printEventLoopLag(benchmark, benchmark, benchmarks[0]);
Object.keys(benchmark.results).forEach(function(k) {
printEventLoopLag(benchmark, benchmark.results[k].read, benchmarks[0].results[k].read);
printEventLoopLag(benchmark, benchmark.results[k].write, benchmarks[0].results[k].write);
});
});
}
function printWallClock(benchmark, test, compare) {
var compareToJson = ' (base)';
if (benchmark.name != 'JSON') {
var diff = Math.round(test.timeElapsed / compare.timeElapsed * 100);
compareToJson = ' (' + diff + '%)';
}
console.log(' ' + test.paddedName + '\t' + test.timeElapsed + compareToJson);
}
function printCPUClock(benchmark, test, compare) {
var compareToJson = ' (base)';
if (benchmark.name != 'JSON') {
var diff = Math.round(test.cpuTimeElapsed / compare.cpuTimeElapsed * 100);
compareToJson = ' (' + diff + '%)';
}
console.log(' ' + test.paddedName + '\t' + test.cpuTimeElapsed + compareToJson);
}
function printThroughput(benchmark, test, compare) {
var compareToJson = ' (base)';
if (benchmark.name != 'JSON') {
var diff = Math.round(test.throughputKBsec / compare.throughputKBsec * 100);
compareToJson = ' (' + diff + '%)';
}
console.log(' ' + test.paddedName + '\t' + test.throughputKBsec + compareToJson);
}
function printEventLoopLag(benchmark, test, compare) {
var compareToJson = ' (base)';
if (benchmark.name != 'JSON') {
var diff = Math.round(test.eventLoopLag / compare.eventLoopLag * 100);
compareToJson = ' (' + diff + '%)';
}
console.log(' ' + test.paddedName + '\t' + test.eventLoopLag + compareToJson);
}