-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamparse_2.ts
41 lines (34 loc) · 983 Bytes
/
streamparse_2.ts
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
let time = Date.now();
let json_sax = require('./json_sax'); //for JSON streaming parser
let stream = process.stdin;
let parse_data = { sum: 0, count: 0 };
let parser = new json_sax.SaxParser(callbacks(parse_data));
stream.on('error', function (error) {
console.log("Caught", error);
});
stream.on('data', data => {
parser.parse(data);
});
stream.on('end', () => {
time = Date.now() - time;
let mem = Number(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2);
if(process.argv[2] != '-s')
console.log("Method,Rows,Sum,Time,Memory");
console.log("stream2, %s, %s, %s, %s", parse_data.count, parse_data.sum, time, mem);
});
/////JSON Streaming parser test begin/////
function callbacks(data) {
return {
onEndArray: function() {
if(parser.stack.length == 1)
data.count++;
},
onValue: function(v) {
data.sum += v;
},
onError: function(e) {
console.log(e)
}
};
}
/////JSON Streaming parser test end//////