forked from kriszyp/msgpackr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cjs
239 lines (204 loc) · 7.58 KB
/
benchmark.cjs
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
var msgpackr = require("../dist/node.cjs");
console.log('isNativeAccelerationEnabled', msgpackr.isNativeAccelerationEnabled)
var msgpack_node = tryRequire("msgpack");
var msgpack_msgpack = tryRequire("@msgpack/msgpack");
var msgpack_lite = tryRequire("msgpack-lite");
var msgpack_js = tryRequire("msgpack-js");
var msgpack_js_v5 = tryRequire("msgpack-js-v5");
var msgpack5 = tryRequire("msgpack5");
var msgpack_unpack = tryRequire("msgpack-unpack");
var msgpack_codec = tryRequire("msgpack.codec");
var notepack = tryRequire("notepack");
var what_the_pack = tryRequire("what-the-pack");
var avro = tryRequire('avsc')
var cbor = tryRequire('cbor')
var inspector = require('inspector');
//inspector.open(9229, null, true); debugger
const LAZY = { lazy: true };
msgpack5 = msgpack5 && msgpack5();
msgpack_codec = msgpack_codec && msgpack_codec.msgpack;
what_the_pack = what_the_pack && what_the_pack.initialize(2**20);
var pkg = require("../package.json");
var data = require("./example5.json");
var packed = msgpack_lite && msgpack_lite.encode(data);
var expected = JSON.stringify(data);
var argv = Array.prototype.slice.call(process.argv, 2);
if (argv[0] === "-v") {
console.warn(pkg.name + " " + pkg.version);
process.exit(0);
}
var limit = 5;
if (argv[0] - 0) limit = argv.shift() - 0;
limit *= 1000;
var COL1 = 58;
var COL2 = 7;
var COL3 = 5;
var COL4 = 6;
console.log(rpad("operation", COL1), "|", " op ", "|", " ms ", "|", " op/s ", "|", "size");
console.log(rpad("", COL1, "-"), "|", lpad(":", COL2, "-"), "|", lpad(":", COL3, "-"), "|", lpad(":", COL4, "-"), "|", lpad(":", COL4, "-"));
var buf, obj;
if (msgpackr) {
let packr, last
let keys = ['littleNum'];//Object.keys(data);
packr = new msgpackr.Packr({ structures: [] })
buf = bench('msgpackr w/ shared structures: packr.pack(obj);', packr.pack.bind(packr), data);
console.log('buffer size', buf.length);
//buf = bench('msgpackr w/ shared structures: packr.pack(obj);', data => {let result = packr.pack(data); packr.resetMemory(); return result;}, data);
obj = bench('msgpackr w/ shared structures: packr.unpack(buf);', value => {
let o = packr.unpack(value);
return o.biggerNum;
/* for (let i of keys) {
last = o[i];
}
return last;*/
}, buf);
test(obj);
packr = new msgpackr.Packr({ structures: [],randomAccessStructure: true, saveStructures(structures) {
console.log('saved',{structures});
} })
console.log('starting')
buf = bench('msgpackr w/ random access structures: packr.pack(obj);', value => packr.pack(value), data);
//buf = bench('msgpackr w/ shared structures: packr.pack(obj);', data => {let result = packr.pack(data); packr.resetMemory(); return result;}, data);
console.log('buffer size', buf.length);
obj = bench('msgpackr w/ random access structures: packr.unpack(buf);', value => {
let o = packr.unpack(value);
return o.biggerNum;
/* for (let i of keys) {
last = o[i];
}
return last;*/
}, buf);
test(obj);
packr = new msgpackr.Packr({ useRecords: false })
buf = bench('require("msgpackr").pack(obj);', msgpackr.pack, data);
//buf = bench('require("msgpackr").pack(obj);', data => {let result = packr.pack(data); packr.resetMemory(); return result;}, data);
obj = bench('require("msgpackr").unpack(buf);', msgpackr.unpack, buf);
test(obj);
packr = new msgpackr.Packr({ bundleStrings: true, structures: [] })
buf = bench('bundled strings packr.pack(obj);', packr.pack.bind(packr), data);
//buf = bench('require("msgpackr").pack(obj);', data => {let result = packr.pack(data); packr.resetMemory(); return result;}, data);
obj = bench('bundled strings packr.unpack(buf);', packr.unpack.bind(packr), buf);
test(obj);
}
if (JSON) {
console.log('JSON')
buf = bench('buf = Buffer(JSON.stringify(obj));', JSON_stringify, data);
obj = bench('obj = JSON.parse(buf);', JSON.parse, buf);
test(obj);
}
if (msgpack_lite) {
buf = bench('buf = require("msgpack-lite").encode(obj);', msgpack_lite.encode, data);
obj = bench('obj = require("msgpack-lite").decode(buf);', msgpack_lite.decode, packed);
test(obj);
}
if (msgpack_msgpack) {
buf = bench('buf = require("@msgpack/msgpack").encode(obj);', msgpack_msgpack.encode, data);
obj = bench('obj = require("@msgpack/msgpack").decode(buf);', msgpack_msgpack.decode, buf);
test(obj);
}
if (msgpack_node) {
buf = bench('buf = require("msgpack").pack(obj);', msgpack_node.pack, data);
obj = bench('obj = require("msgpack").unpack(buf);', msgpack_node.unpack, buf);
test(obj);
}
if (msgpack_codec) {
buf = bench('buf = Buffer(require("msgpack.codec").msgpack.pack(obj));', msgpack_codec_pack, data);
obj = bench('obj = require("msgpack.codec").msgpack.unpack(buf);', msgpack_codec.unpack, buf);
test(obj);
}
if (msgpack_js_v5) {
buf = bench('buf = require("msgpack-js-v5").encode(obj);', msgpack_js_v5.encode, data);
obj = bench('obj = require("msgpack-js-v5").decode(buf);', msgpack_js_v5.decode, buf);
test(obj);
}
if (msgpack_js) {
buf = bench('buf = require("msgpack-js").encode(obj);', msgpack_js.encode, data);
obj = bench('obj = require("msgpack-js").decode(buf);', msgpack_js.decode, buf);
test(obj);
}
if (msgpack5) {
buf = bench('buf = require("msgpack5")().encode(obj);', msgpack5.encode, data);
obj = bench('obj = require("msgpack5")().decode(buf);', msgpack5.decode, buf);
test(obj);
}
if (notepack) {
buf = bench('buf = require("notepack").encode(obj);', notepack.encode, data);
obj = bench('obj = require("notepack").decode(buf);', notepack.decode, buf);
test(obj);
}
if (what_the_pack) {
buf = bench('require("what-the-pack")... encoder.encode(obj);', what_the_pack.encode, data);
obj = bench('require("what-the-pack")... encoder.decode(buf);', what_the_pack.decode, buf);
test(obj);
}
if (msgpack_unpack) {
obj = bench('obj = require("msgpack-unpack").decode(buf);', msgpack_unpack, packed);
test(obj);
}
if (avro) {
const type = avro.Type.forValue(data);
buf = bench('require("avsc")...make schema/type...type.toBuffer(obj);', type.toBuffer.bind(type), data);
obj = bench('require("avsc")...make schema/type...type.fromBuffer(obj);', type.fromBuffer.bind(type), buf);
}
if (cbor) {
buf = bench('buf = require("cbor").encode(obj);', cbor.encode, data);
obj = bench('obj = require("cbor").decode(buf);', cbor.decode, buf);
test(obj);
}
function JSON_stringify(src) {
return Buffer.from(JSON.stringify(src));
}
function msgpack_codec_pack(data) {
return Buffer.from(msgpack_codec.pack(data));
}
function bench(name, func, src) {
if (argv.length) {
var match = argv.filter(function(grep) {
return (name.indexOf(grep) > -1);
});
if (!match.length) return SKIP;
}
var ret, duration;
var start = new Date() - 0;
var count = 0;
while (1) {
var end = new Date() - 0;
duration = end - start;
if (duration >= limit) break;
while ((++count) % 100) ret = func(src);
}
name = rpad(name, COL1);
var score = Math.floor(count / duration * 1000);
count = lpad(count, COL2);
duration = lpad(duration, COL3);
score = lpad(score, COL4);
console.log(name, "|", count, "|", duration, "|", score, "|", src.length);
return ret;
}
function rpad(str, len, chr) {
if (!chr) chr = " ";
while (str.length < len) str += chr;
return str;
}
function lpad(str, len, chr) {
if (!chr) chr = " ";
str += "";
while (str.length < len) str = chr + str;
return str;
}
function test(actual) {
if (actual === SKIP) return;
actual = JSON.stringify(actual);
if (actual === expected) return;
console.warn("expected: " + expected);
console.warn("actual: " + actual);
}
function SKIP() {
}
function tryRequire(name) {
try {
return require(name);
} catch (e) {
// ignore
}
}