-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.js
102 lines (93 loc) · 3.05 KB
/
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
const Benchmark = require('benchmark');
const bin = require('./');
const protobuf = require('protocol-buffers');
const fs = require('fs');
const proto_source = fs.readFileSync('./tests/benchmark/protobuf.proto');
const defs = protobuf(proto_source);
const MyMessage = require('./tests/benchmark/rules');
const Compiler = require('./Compiler');
const results_enc = {};
const results_dec = {};
function runTestsForN(n){
const original = {
title: 'binary-encoder',
from: {first_name: 'Karol', age: 25},
to: {first_name: 'Jon', age: 30},
content: Buffer.from('some content'.padEnd(n, '0')),
attachments: [
{link: {url: 'https://github.com/walasek/node-binary-encoder'}}
]
};
const buf = Buffer.allocUnsafe(2*n+1024);
const turboEncoder = Compiler.compileEncoder(MyMessage);
const turboDecoder = Compiler.compileDecoder(MyMessage);
const enc_protobuf = defs.MyMessage.encode(original);
const enc_json = JSON.stringify(original);
const enc_bin = MyMessage.encode(original, null, null, buf.length);
new Benchmark.Suite('Encode')
.add('protobuf (encode)', () => {
defs.MyMessage.encode(original);
})
.add('binary-encoder (encode)', () => {
MyMessage.encode(original, null, null, buf.length);
})
.add('binary-encoder-buf (encode)', () => {
MyMessage.encode(original, buf, null, buf.length);
})
.add('binary-encoder-compiled (encode)', () => {
turboEncoder(original, null, null, buf.length);
})
.add('binary-encoder-compiled-buf (encode)', () => {
turboEncoder(original, buf, null, buf.length);
})
.add('json (encode)', () => {
JSON.stringify(original);
})
.on('cycle', (ev) => {
console.log(String(ev.target));
if(!results_enc[n])
results_enc[n] = {};
results_enc[n][ev.target.name] = ev.target.hz;
})
.on('complete', function() {
console.log(`Fastest Encoding for N=${n} is ${this.filter('fastest').map('name')}\n`);
})
.run();
new Benchmark.Suite('Decode')
.add('protobuf (decode)', () => {
defs.MyMessage.decode(enc_protobuf);
})
.add('binary-encoder (decode)', () => {
MyMessage.decode(enc_bin);
})
.add('binary-encoder-compiled (decode)', () => {
turboDecoder(enc_bin);
})
.add('json (decode)', () => {
JSON.stringify(enc_json);
})
.on('cycle', (ev) => {
console.log(String(ev.target));
if(!results_dec[n])
results_dec[n] = {};
results_dec[n][ev.target.name] = ev.target.hz;
})
.on('complete', function() {
console.log(`Fastest Decoding for N=${n} is ${this.filter('fastest').map('name')}\n`);
})
.run();
console.log(`Message sizes:\nprotobuf ${enc_protobuf.length} bytes\nbinary-encoder ${enc_bin.length}\njson ${enc_json.length}\n`);
}
runTestsForN(10);
runTestsForN(100);
runTestsForN(1000);
runTestsForN(10000);
runTestsForN(100000);
/*runTestsForN(1000000);
runTestsForN(10000000);*/
console.log('Results in CSV for plots:');
console.log(','+Object.keys(results_enc[10]));
Object.keys(results_enc).map(n => console.log(`${n},${Object.values(results_enc[n])}`));
console.log('');
console.log(','+Object.keys(results_dec[10]));
Object.keys(results_dec).map(n => console.log(`${n},${Object.values(results_dec[n])}`));