msgpackr 1.5.0 → 1.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/index.js +271 -113
- package/dist/index.min.js +66 -100
- package/dist/node.cjs +273 -113
- package/dist/str.cjs +100 -0
- package/dist/test.js +33 -2
- package/pack.js +206 -105
- package/package.json +7 -1
- package/unpack.d.ts +2 -0
- package/unpack.js +62 -2
- package/.vscode/launch.json +0 -23
- package/tests/benchmark-stream.cjs +0 -282
- package/tests/benchmark.cjs +0 -199
- package/tests/example.json +0 -52
- package/tests/example2.json +0 -26
- package/tests/example3.json +0 -22
- package/tests/example4.json +0 -1
- package/tests/example5.json +0 -12
- package/tests/floats.json +0 -1
- package/tests/index.html +0 -28
- package/tests/sample-large.json +0 -231
- package/tests/strings2.json +0 -1
- package/tests/test-compatibility.cjs +0 -64
- package/tests/test-incomplete.js +0 -41
- package/tests/test-node-iterators.js +0 -72
- package/tests/test-node-stream.js +0 -76
- package/tests/test.js +0 -650
package/unpack.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export interface Options {
|
|
|
13
13
|
mapsAsObjects?: boolean
|
|
14
14
|
variableMapSize?: boolean
|
|
15
15
|
copyBuffers?: boolean
|
|
16
|
+
bundleStrings?: boolean
|
|
16
17
|
useTimestamp32?: boolean
|
|
17
18
|
largeBigIntToFloat?: boolean
|
|
18
19
|
encodeUndefinedAsNil?: boolean
|
|
@@ -21,6 +22,7 @@ export interface Options {
|
|
|
21
22
|
shouldShareStructure?: (keys: string[]) => boolean
|
|
22
23
|
getStructures?(): {}[]
|
|
23
24
|
saveStructures?(structures: {}[]): boolean | void
|
|
25
|
+
onInvalidDate?: () => any
|
|
24
26
|
}
|
|
25
27
|
interface Extension {
|
|
26
28
|
Class: Function
|
package/unpack.js
CHANGED
|
@@ -15,6 +15,7 @@ var currentStructures
|
|
|
15
15
|
var srcString
|
|
16
16
|
var srcStringStart = 0
|
|
17
17
|
var srcStringEnd = 0
|
|
18
|
+
var bundledStrings
|
|
18
19
|
var referenceMap
|
|
19
20
|
var currentExtensions = []
|
|
20
21
|
var dataView
|
|
@@ -55,6 +56,7 @@ export class Unpackr {
|
|
|
55
56
|
srcStringEnd = 0
|
|
56
57
|
srcString = null
|
|
57
58
|
strings = EMPTY_ARRAY
|
|
59
|
+
bundledStrings = null
|
|
58
60
|
src = source
|
|
59
61
|
// this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
|
|
60
62
|
// technique for getting data from a database where it can be copied into an existing buffer instead of creating
|
|
@@ -154,6 +156,9 @@ export function checkedRead() {
|
|
|
154
156
|
currentStructures.length = sharedLength
|
|
155
157
|
}
|
|
156
158
|
let result = read()
|
|
159
|
+
if (bundledStrings) // bundled strings to skip past
|
|
160
|
+
position = bundledStrings.postBundlePosition
|
|
161
|
+
|
|
157
162
|
if (position == srcEnd) {
|
|
158
163
|
// finished reading this source, cleanup references
|
|
159
164
|
if (currentStructures.restoreStructures)
|
|
@@ -248,7 +253,15 @@ export function read() {
|
|
|
248
253
|
let value
|
|
249
254
|
switch (token) {
|
|
250
255
|
case 0xc0: return null
|
|
251
|
-
case 0xc1:
|
|
256
|
+
case 0xc1:
|
|
257
|
+
if (bundledStrings) {
|
|
258
|
+
value = read() // followed by the length of the string in characters (not bytes!)
|
|
259
|
+
if (value > 0)
|
|
260
|
+
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
|
|
261
|
+
else
|
|
262
|
+
return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
|
|
263
|
+
}
|
|
264
|
+
return C1; // "never-used", return special object to denote that
|
|
252
265
|
case 0xc2: return false
|
|
253
266
|
case 0xc3: return true
|
|
254
267
|
case 0xc4:
|
|
@@ -488,6 +501,8 @@ export function setExtractor(extractStrings) {
|
|
|
488
501
|
return function readString(length) {
|
|
489
502
|
let string = strings[stringPosition++]
|
|
490
503
|
if (string == null) {
|
|
504
|
+
if (bundledStrings)
|
|
505
|
+
return readStringJS(length)
|
|
491
506
|
let extraction = extractStrings(position - headerLength, srcEnd, src)
|
|
492
507
|
if (typeof extraction == 'string') {
|
|
493
508
|
string = extraction
|
|
@@ -746,6 +761,36 @@ function shortStringInJS(length) {
|
|
|
746
761
|
}
|
|
747
762
|
}
|
|
748
763
|
|
|
764
|
+
function readOnlyJSString() {
|
|
765
|
+
let token = src[position++]
|
|
766
|
+
let length
|
|
767
|
+
if (token < 0xc0) {
|
|
768
|
+
// fixstr
|
|
769
|
+
length = token - 0xa0
|
|
770
|
+
} else {
|
|
771
|
+
switch(token) {
|
|
772
|
+
case 0xd9:
|
|
773
|
+
// str 8
|
|
774
|
+
length = src[position++]
|
|
775
|
+
break
|
|
776
|
+
case 0xda:
|
|
777
|
+
// str 16
|
|
778
|
+
length = dataView.getUint16(position)
|
|
779
|
+
position += 2
|
|
780
|
+
break
|
|
781
|
+
case 0xdb:
|
|
782
|
+
// str 32
|
|
783
|
+
length = dataView.getUint32(position)
|
|
784
|
+
position += 4
|
|
785
|
+
break
|
|
786
|
+
default:
|
|
787
|
+
throw new Error('Expected string')
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
return readStringJS(length)
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
|
|
749
794
|
function readBin(length) {
|
|
750
795
|
return currentUnpackr.copyBuffers ?
|
|
751
796
|
// specifically use the copying slice (not the node one)
|
|
@@ -897,6 +942,19 @@ currentExtensions[0x78] = () => {
|
|
|
897
942
|
let data = read()
|
|
898
943
|
return new RegExp(data[0], data[1])
|
|
899
944
|
}
|
|
945
|
+
const TEMP_BUNDLE = []
|
|
946
|
+
currentExtensions[0x62] = (data) => {
|
|
947
|
+
let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]
|
|
948
|
+
let dataPosition = position
|
|
949
|
+
position += dataSize - data.length
|
|
950
|
+
bundledStrings = TEMP_BUNDLE
|
|
951
|
+
bundledStrings = [readOnlyJSString(), readOnlyJSString()]
|
|
952
|
+
bundledStrings.position0 = 0
|
|
953
|
+
bundledStrings.position1 = 0
|
|
954
|
+
bundledStrings.postBundlePosition = position
|
|
955
|
+
position = dataPosition
|
|
956
|
+
return read()
|
|
957
|
+
}
|
|
900
958
|
|
|
901
959
|
currentExtensions[0xff] = (data) => {
|
|
902
960
|
// 32-bit date extension
|
|
@@ -911,7 +969,7 @@ currentExtensions[0xff] = (data) => {
|
|
|
911
969
|
((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
|
|
912
970
|
(((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
|
|
913
971
|
else
|
|
914
|
-
|
|
972
|
+
return new Date('invalid')
|
|
915
973
|
} // notepack defines extension 0 to mean undefined, so use that as the default here
|
|
916
974
|
// registration of bulk record definition?
|
|
917
975
|
// currentExtensions[0x52] = () =>
|
|
@@ -925,6 +983,7 @@ function saveState(callback) {
|
|
|
925
983
|
let savedSrcString = srcString
|
|
926
984
|
let savedStrings = strings
|
|
927
985
|
let savedReferenceMap = referenceMap
|
|
986
|
+
let savedBundledStrings = bundledStrings
|
|
928
987
|
|
|
929
988
|
// TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
|
|
930
989
|
let savedSrc = new Uint8Array(src.slice(0, srcEnd)) // we copy the data in case it changes while external data is processed
|
|
@@ -941,6 +1000,7 @@ function saveState(callback) {
|
|
|
941
1000
|
srcString = savedSrcString
|
|
942
1001
|
strings = savedStrings
|
|
943
1002
|
referenceMap = savedReferenceMap
|
|
1003
|
+
bundledStrings = savedBundledStrings
|
|
944
1004
|
src = savedSrc
|
|
945
1005
|
sequentialMode = savedSequentialMode
|
|
946
1006
|
currentStructures = savedStructures
|
package/.vscode/launch.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Use IntelliSense to learn about possible attributes.
|
|
3
|
-
// Hover to view descriptions of existing attributes.
|
|
4
|
-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
-
"version": "0.2.0",
|
|
6
|
-
"configurations": [
|
|
7
|
-
{
|
|
8
|
-
"type": "pwa-node",
|
|
9
|
-
"request": "launch",
|
|
10
|
-
"name": "Run msgpackr tests",
|
|
11
|
-
"runtimeArgs": [
|
|
12
|
-
"test"
|
|
13
|
-
],
|
|
14
|
-
"runtimeExecutable": "npm",
|
|
15
|
-
"skipFiles": [
|
|
16
|
-
"<node_internals>/**"
|
|
17
|
-
],
|
|
18
|
-
"type": "pwa-node",
|
|
19
|
-
"console": "internalConsole",
|
|
20
|
-
"outputCapture": "std"
|
|
21
|
-
}
|
|
22
|
-
]
|
|
23
|
-
}
|
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
var PassThrough = require("stream").PassThrough;
|
|
4
|
-
var async = require("async");
|
|
5
|
-
|
|
6
|
-
let { PackrStream, UnpackrStream } = require("..");
|
|
7
|
-
var msgpack = require("msgpack-lite");
|
|
8
|
-
var Encoder = require("msgpack-lite/lib/encoder").Encoder;
|
|
9
|
-
var Decoder = require("msgpack-lite/lib/decoder").Decoder;
|
|
10
|
-
var notepack = require("notepack");
|
|
11
|
-
|
|
12
|
-
var pkg = require("../package.json");
|
|
13
|
-
|
|
14
|
-
// a sample fluentd message
|
|
15
|
-
var data = ["tag", [[1440949922, {"message": "hi there"}]]];
|
|
16
|
-
var packed = msgpack.encode(data); // 30 bytes per message
|
|
17
|
-
var packsize = packed.length;
|
|
18
|
-
var opcount = 1000000;
|
|
19
|
-
var joincount = 100;
|
|
20
|
-
var packjoin = repeatbuf(packed, joincount); // 3KB per chunk
|
|
21
|
-
var limit = 2;
|
|
22
|
-
|
|
23
|
-
var blocksToJoin = []
|
|
24
|
-
var streamForJoin = new PackrStream();
|
|
25
|
-
streamForJoin.on("data", data => blocksToJoin.push(data));
|
|
26
|
-
for (var j = 0; j < joincount; j++) {
|
|
27
|
-
streamForJoin.write(data);
|
|
28
|
-
}
|
|
29
|
-
var packjoinWithRecords = Buffer.concat(blocksToJoin)
|
|
30
|
-
|
|
31
|
-
var argv = Array.prototype.slice.call(process.argv, 2);
|
|
32
|
-
|
|
33
|
-
if (argv[0] === "-v") {
|
|
34
|
-
console.warn(pkg.name + " " + pkg.version);
|
|
35
|
-
process.exit(0);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (argv[0] - 0) limit = argv.shift() - 0;
|
|
39
|
-
|
|
40
|
-
var list = [
|
|
41
|
-
['new PackrStream().write(obj);', encode5],
|
|
42
|
-
['new UnpackrStream().write(buf);', decode5],
|
|
43
|
-
['stream.write(msgpack.encode(obj));', encode1],
|
|
44
|
-
['stream.write(msgpack.decode(buf));', decode1],
|
|
45
|
-
['stream.write(notepack.encode(obj));', encode4],
|
|
46
|
-
['stream.write(notepack.decode(buf));', decode4],
|
|
47
|
-
['msgpack.Encoder().on("data",ondata).encode(obj);', encode2],
|
|
48
|
-
['msgpack.createDecodeStream().write(buf);', decode3],
|
|
49
|
-
['msgpack.createEncodeStream().write(obj);', encode3],
|
|
50
|
-
['msgpack.Decoder().on("data",ondata).decode(buf);', decode2],
|
|
51
|
-
// ['stream.write(Buffer.from(JSON.stringify(obj)));', stringify],
|
|
52
|
-
// ['stream.write(JSON.parse(buf));', parse]
|
|
53
|
-
];
|
|
54
|
-
|
|
55
|
-
function encode5(callback) {
|
|
56
|
-
var stream = new PackrStream();
|
|
57
|
-
var cnt = counter(callback);
|
|
58
|
-
stream.on("data", cnt.inc);
|
|
59
|
-
stream.on("end", cnt.end);
|
|
60
|
-
for (var j = 0; j < opcount; j++) {
|
|
61
|
-
stream.write(data);
|
|
62
|
-
}
|
|
63
|
-
stream.end();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function encode1(callback) {
|
|
67
|
-
var stream = new PassThrough();
|
|
68
|
-
var cnt = counter(callback);
|
|
69
|
-
stream.on("data", cnt.buf);
|
|
70
|
-
stream.on("end", cnt.end);
|
|
71
|
-
for (var j = 0; j < opcount; j++) {
|
|
72
|
-
stream.write(msgpack.encode(data));
|
|
73
|
-
}
|
|
74
|
-
stream.end();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function encode2(callback) {
|
|
78
|
-
var stream = new PassThrough();
|
|
79
|
-
var cnt = counter(callback);
|
|
80
|
-
stream.on("data", cnt.buf);
|
|
81
|
-
stream.on("end", cnt.end);
|
|
82
|
-
var encoder = Encoder();
|
|
83
|
-
encoder.on("data", function(chunk) {
|
|
84
|
-
stream.write(chunk);
|
|
85
|
-
});
|
|
86
|
-
encoder.on("end", function() {
|
|
87
|
-
stream.end();
|
|
88
|
-
});
|
|
89
|
-
for (var j = 0; j < opcount; j++) {
|
|
90
|
-
encoder.encode(data);
|
|
91
|
-
}
|
|
92
|
-
encoder.end();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function encode3(callback) {
|
|
96
|
-
var stream = msgpack.createEncodeStream();
|
|
97
|
-
var cnt = counter(callback);
|
|
98
|
-
stream.on("data", cnt.buf);
|
|
99
|
-
stream.on("end", cnt.end);
|
|
100
|
-
for (var j = 0; j < opcount; j++) {
|
|
101
|
-
stream.write(data);
|
|
102
|
-
}
|
|
103
|
-
stream.end();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function encode4(callback) {
|
|
107
|
-
var stream = new PassThrough();
|
|
108
|
-
var cnt = counter(callback);
|
|
109
|
-
stream.on("data", cnt.buf);
|
|
110
|
-
stream.on("end", cnt.end);
|
|
111
|
-
for (var j = 0; j < opcount; j++) {
|
|
112
|
-
stream.write(notepack.encode(data));
|
|
113
|
-
}
|
|
114
|
-
stream.end();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function decode5(callback) {
|
|
118
|
-
var stream = new UnpackrStream();
|
|
119
|
-
var cnt = counter(callback);
|
|
120
|
-
stream.on("data", cnt.inc);
|
|
121
|
-
stream.on("end", cnt.end);
|
|
122
|
-
for (var j = 0; j < opcount / joincount; j++) {
|
|
123
|
-
stream.write(packjoinWithRecords);
|
|
124
|
-
}
|
|
125
|
-
stream.end();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function decode1(callback) {
|
|
129
|
-
var stream = new PassThrough({objectMode: true});
|
|
130
|
-
var cnt = counter(callback);
|
|
131
|
-
stream.on("data", cnt.inc);
|
|
132
|
-
stream.on("end", cnt.end);
|
|
133
|
-
for (var j = 0; j < opcount; j++) {
|
|
134
|
-
stream.write(msgpack.decode(packed));
|
|
135
|
-
}
|
|
136
|
-
stream.end();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function decode2(callback) {
|
|
140
|
-
var stream = new PassThrough({objectMode: true});
|
|
141
|
-
var cnt = counter(callback);
|
|
142
|
-
stream.on("data", cnt.inc);
|
|
143
|
-
stream.on("end", cnt.end);
|
|
144
|
-
var decoder = Decoder();
|
|
145
|
-
decoder.on("data", function(chunk) {
|
|
146
|
-
stream.write(chunk);
|
|
147
|
-
});
|
|
148
|
-
decoder.on("end", function() {
|
|
149
|
-
stream.end();
|
|
150
|
-
});
|
|
151
|
-
for (var j = 0; j < opcount / joincount; j++) {
|
|
152
|
-
decoder.decode(packjoin);
|
|
153
|
-
}
|
|
154
|
-
decoder.end();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function decode3(callback) {
|
|
158
|
-
var stream = msgpack.createDecodeStream();
|
|
159
|
-
var cnt = counter(callback);
|
|
160
|
-
stream.on("data", cnt.inc);
|
|
161
|
-
stream.on("end", cnt.end);
|
|
162
|
-
for (var j = 0; j < opcount / joincount; j++) {
|
|
163
|
-
stream.write(packjoin);
|
|
164
|
-
}
|
|
165
|
-
stream.end();
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function decode4(callback) {
|
|
169
|
-
var stream = new PassThrough({objectMode: true});
|
|
170
|
-
var cnt = counter(callback);
|
|
171
|
-
stream.on("data", cnt.inc);
|
|
172
|
-
stream.on("end", cnt.end);
|
|
173
|
-
for (var j = 0; j < opcount; j++) {
|
|
174
|
-
stream.write(notepack.decode(packed));
|
|
175
|
-
}
|
|
176
|
-
stream.end();
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function rpad(str, len, chr) {
|
|
180
|
-
if (!chr) chr = " ";
|
|
181
|
-
str += "";
|
|
182
|
-
while (str.length < len) str += chr;
|
|
183
|
-
return str;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function lpad(str, len, chr) {
|
|
187
|
-
if (!chr) chr = " ";
|
|
188
|
-
str += "";
|
|
189
|
-
while (str.length < len) str = chr + str;
|
|
190
|
-
return str;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function repeatbuf(buf, cnt) {
|
|
194
|
-
var array = [];
|
|
195
|
-
for (var i = 0; i < cnt; i++) {
|
|
196
|
-
array.push(buf);
|
|
197
|
-
}
|
|
198
|
-
return Buffer.concat(array);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function counter(callback) {
|
|
202
|
-
var cnt = 0;
|
|
203
|
-
return {buf: b, inc: i, end: e};
|
|
204
|
-
|
|
205
|
-
function b(buf) {
|
|
206
|
-
cnt += buf.length / packsize;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function i() {
|
|
210
|
-
cnt++;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function e() {
|
|
214
|
-
cnt = Math.round(cnt);
|
|
215
|
-
callback(null, cnt);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function run() {
|
|
220
|
-
// task filter
|
|
221
|
-
if (argv.length) {
|
|
222
|
-
list = list.filter(function(pair) {
|
|
223
|
-
var name = pair[0];
|
|
224
|
-
var match = argv.filter(function(grep) {
|
|
225
|
-
return (name.indexOf(grep) > -1);
|
|
226
|
-
});
|
|
227
|
-
return match.length;
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// run tasks repeatedly
|
|
232
|
-
var tasks = [];
|
|
233
|
-
for (var i = 0; i < limit; i++) {
|
|
234
|
-
tasks.push(oneset);
|
|
235
|
-
}
|
|
236
|
-
async.series(tasks, end);
|
|
237
|
-
|
|
238
|
-
// run a series of tasks
|
|
239
|
-
function oneset(callback) {
|
|
240
|
-
async.eachSeries(list, bench, callback);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// run a single benchmark
|
|
244
|
-
function bench(pair, callback) {
|
|
245
|
-
process.stdout.write(".");
|
|
246
|
-
var func = pair[1];
|
|
247
|
-
var start = new Date() - 0;
|
|
248
|
-
func(function(err, cnt) {
|
|
249
|
-
var end = new Date() - 0;
|
|
250
|
-
var array = pair[2] || (pair[2] = []);
|
|
251
|
-
array.push(end - start);
|
|
252
|
-
pair[3] = cnt;
|
|
253
|
-
setTimeout(callback, 100);
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// show result
|
|
258
|
-
function end() {
|
|
259
|
-
var title = "operation (" + opcount + " x " + limit + ")";
|
|
260
|
-
process.stdout.write("\n");
|
|
261
|
-
|
|
262
|
-
// table header
|
|
263
|
-
var COL1 = 48;
|
|
264
|
-
console.log(rpad(title, COL1), "|", " op ", "|", " ms ", "|", " op/s ");
|
|
265
|
-
console.log(rpad("", COL1, "-"), "|", "------:", "|", "----:", "|", "-----:");
|
|
266
|
-
|
|
267
|
-
// table body
|
|
268
|
-
list.forEach(function(pair) {
|
|
269
|
-
var name = pair[0];
|
|
270
|
-
var op = pair[3];
|
|
271
|
-
var array = pair[2];
|
|
272
|
-
array = array.sort(function(a, b) {
|
|
273
|
-
return a > b;
|
|
274
|
-
});
|
|
275
|
-
var fastest = array[0];
|
|
276
|
-
var score = Math.floor(opcount / fastest * 1000);
|
|
277
|
-
console.log(rpad(name, COL1), "|", lpad(op, 7), "|", lpad(fastest, 5), "|", lpad(score, 6));
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
run();
|
package/tests/benchmark.cjs
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
var msgpackr = tryRequire("../dist/node.cjs");
|
|
2
|
-
var msgpack_node = tryRequire("msgpack");
|
|
3
|
-
var msgpack_msgpack = tryRequire("@msgpack/msgpack");
|
|
4
|
-
var msgpack_lite = tryRequire("msgpack-lite");
|
|
5
|
-
var msgpack_js = tryRequire("msgpack-js");
|
|
6
|
-
var msgpack_js_v5 = tryRequire("msgpack-js-v5");
|
|
7
|
-
var msgpack5 = tryRequire("msgpack5");
|
|
8
|
-
var msgpack_unpack = tryRequire("msgpack-unpack");
|
|
9
|
-
var msgpack_codec = tryRequire("msgpack.codec");
|
|
10
|
-
var notepack = tryRequire("notepack");
|
|
11
|
-
var what_the_pack = tryRequire("what-the-pack");
|
|
12
|
-
var avro = tryRequire('avsc')
|
|
13
|
-
var cbor = tryRequire('cbor')
|
|
14
|
-
|
|
15
|
-
msgpack5 = msgpack5 && msgpack5();
|
|
16
|
-
msgpack_codec = msgpack_codec && msgpack_codec.msgpack;
|
|
17
|
-
what_the_pack = what_the_pack && what_the_pack.initialize(2**20);
|
|
18
|
-
|
|
19
|
-
var pkg = require("../package.json");
|
|
20
|
-
var data = require("./example4.json");
|
|
21
|
-
var packed = msgpack_lite && msgpack_lite.encode(data);
|
|
22
|
-
var expected = JSON.stringify(data);
|
|
23
|
-
|
|
24
|
-
var argv = Array.prototype.slice.call(process.argv, 2);
|
|
25
|
-
|
|
26
|
-
if (argv[0] === "-v") {
|
|
27
|
-
console.warn(pkg.name + " " + pkg.version);
|
|
28
|
-
process.exit(0);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
var limit = 5;
|
|
32
|
-
if (argv[0] - 0) limit = argv.shift() - 0;
|
|
33
|
-
limit *= 1000;
|
|
34
|
-
|
|
35
|
-
var COL1 = 58;
|
|
36
|
-
var COL2 = 7;
|
|
37
|
-
var COL3 = 5;
|
|
38
|
-
var COL4 = 6;
|
|
39
|
-
|
|
40
|
-
console.log(rpad("operation", COL1), "|", " op ", "|", " ms ", "|", " op/s ");
|
|
41
|
-
console.log(rpad("", COL1, "-"), "|", lpad(":", COL2, "-"), "|", lpad(":", COL3, "-"), "|", lpad(":", COL4, "-"));
|
|
42
|
-
|
|
43
|
-
var buf, obj;
|
|
44
|
-
|
|
45
|
-
if (msgpackr) {
|
|
46
|
-
let packr = new msgpackr.Packr({ useRecords: false })
|
|
47
|
-
buf = bench('require("msgpackr").pack(obj);', msgpackr.pack, data);
|
|
48
|
-
//buf = bench('require("msgpackr").pack(obj);', data => {let result = packr.pack(data); packr.resetMemory(); return result;}, data);
|
|
49
|
-
|
|
50
|
-
obj = bench('require("msgpackr").unpack(buf);', msgpackr.unpack, buf);
|
|
51
|
-
test(obj);
|
|
52
|
-
|
|
53
|
-
packr = new msgpackr.Packr({ structures: [] })
|
|
54
|
-
buf = bench('msgpackr w/ shared structures: packr.pack(obj);', packr.pack.bind(packr), data);
|
|
55
|
-
//buf = bench('msgpackr w/ shared structures: packr.pack(obj);', data => {let result = packr.pack(data); packr.resetMemory(); return result;}, data);
|
|
56
|
-
|
|
57
|
-
obj = bench('msgpackr w/ shared structures: packr.unpack(buf);', packr.unpack.bind(packr), buf);
|
|
58
|
-
test(obj);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (JSON) {
|
|
62
|
-
buf = bench('buf = Buffer(JSON.stringify(obj));', JSON_stringify, data);
|
|
63
|
-
obj = bench('obj = JSON.parse(buf);', JSON.parse, buf);
|
|
64
|
-
test(obj);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (msgpack_lite) {
|
|
68
|
-
buf = bench('buf = require("msgpack-lite").encode(obj);', msgpack_lite.encode, data);
|
|
69
|
-
obj = bench('obj = require("msgpack-lite").decode(buf);', msgpack_lite.decode, packed);
|
|
70
|
-
test(obj);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (msgpack_msgpack) {
|
|
74
|
-
buf = bench('buf = require("@msgpack/msgpack").encode(obj);', msgpack_msgpack.encode, data);
|
|
75
|
-
obj = bench('obj = require("@msgpack/msgpack").decode(buf);', msgpack_msgpack.decode, buf);
|
|
76
|
-
test(obj);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (msgpack_node) {
|
|
80
|
-
buf = bench('buf = require("msgpack").pack(obj);', msgpack_node.pack, data);
|
|
81
|
-
obj = bench('obj = require("msgpack").unpack(buf);', msgpack_node.unpack, buf);
|
|
82
|
-
test(obj);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (msgpack_codec) {
|
|
86
|
-
buf = bench('buf = Buffer(require("msgpack.codec").msgpack.pack(obj));', msgpack_codec_pack, data);
|
|
87
|
-
obj = bench('obj = require("msgpack.codec").msgpack.unpack(buf);', msgpack_codec.unpack, buf);
|
|
88
|
-
test(obj);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (msgpack_js_v5) {
|
|
92
|
-
buf = bench('buf = require("msgpack-js-v5").encode(obj);', msgpack_js_v5.encode, data);
|
|
93
|
-
obj = bench('obj = require("msgpack-js-v5").decode(buf);', msgpack_js_v5.decode, buf);
|
|
94
|
-
test(obj);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (msgpack_js) {
|
|
98
|
-
buf = bench('buf = require("msgpack-js").encode(obj);', msgpack_js.encode, data);
|
|
99
|
-
obj = bench('obj = require("msgpack-js").decode(buf);', msgpack_js.decode, buf);
|
|
100
|
-
test(obj);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (msgpack5) {
|
|
104
|
-
buf = bench('buf = require("msgpack5")().encode(obj);', msgpack5.encode, data);
|
|
105
|
-
obj = bench('obj = require("msgpack5")().decode(buf);', msgpack5.decode, buf);
|
|
106
|
-
test(obj);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (notepack) {
|
|
110
|
-
buf = bench('buf = require("notepack").encode(obj);', notepack.encode, data);
|
|
111
|
-
obj = bench('obj = require("notepack").decode(buf);', notepack.decode, buf);
|
|
112
|
-
test(obj);
|
|
113
|
-
}
|
|
114
|
-
if (what_the_pack) {
|
|
115
|
-
buf = bench('require("what-the-pack")... encoder.encode(obj);', what_the_pack.encode, data);
|
|
116
|
-
obj = bench('require("what-the-pack")... encoder.decode(buf);', what_the_pack.decode, buf);
|
|
117
|
-
test(obj);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (msgpack_unpack) {
|
|
121
|
-
obj = bench('obj = require("msgpack-unpack").decode(buf);', msgpack_unpack, packed);
|
|
122
|
-
test(obj);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (avro) {
|
|
126
|
-
const type = avro.Type.forValue(data);
|
|
127
|
-
buf = bench('require("avsc")...make schema/type...type.toBuffer(obj);', type.toBuffer.bind(type), data);
|
|
128
|
-
obj = bench('require("avsc")...make schema/type...type.fromBuffer(obj);', type.fromBuffer.bind(type), buf);
|
|
129
|
-
}
|
|
130
|
-
if (cbor) {
|
|
131
|
-
buf = bench('buf = require("cbor").encode(obj);', cbor.encode, data);
|
|
132
|
-
obj = bench('obj = require("cbor").decode(buf);', cbor.decode, buf);
|
|
133
|
-
test(obj);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function JSON_stringify(src) {
|
|
137
|
-
return Buffer(JSON.stringify(src));
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function msgpack_codec_pack(data) {
|
|
141
|
-
return Buffer(msgpack_codec.pack(data));
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function bench(name, func, src) {
|
|
145
|
-
if (argv.length) {
|
|
146
|
-
var match = argv.filter(function(grep) {
|
|
147
|
-
return (name.indexOf(grep) > -1);
|
|
148
|
-
});
|
|
149
|
-
if (!match.length) return SKIP;
|
|
150
|
-
}
|
|
151
|
-
var ret, duration;
|
|
152
|
-
var start = new Date() - 0;
|
|
153
|
-
var count = 0;
|
|
154
|
-
while (1) {
|
|
155
|
-
var end = new Date() - 0;
|
|
156
|
-
duration = end - start;
|
|
157
|
-
if (duration >= limit) break;
|
|
158
|
-
while ((++count) % 100) ret = func(src);
|
|
159
|
-
}
|
|
160
|
-
name = rpad(name, COL1);
|
|
161
|
-
var score = Math.floor(count / duration * 1000);
|
|
162
|
-
count = lpad(count, COL2);
|
|
163
|
-
duration = lpad(duration, COL3);
|
|
164
|
-
score = lpad(score, COL4);
|
|
165
|
-
console.log(name, "|", count, "|", duration, "|", score);
|
|
166
|
-
return ret;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function rpad(str, len, chr) {
|
|
170
|
-
if (!chr) chr = " ";
|
|
171
|
-
while (str.length < len) str += chr;
|
|
172
|
-
return str;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function lpad(str, len, chr) {
|
|
176
|
-
if (!chr) chr = " ";
|
|
177
|
-
str += "";
|
|
178
|
-
while (str.length < len) str = chr + str;
|
|
179
|
-
return str;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function test(actual) {
|
|
183
|
-
if (actual === SKIP) return;
|
|
184
|
-
actual = JSON.stringify(actual);
|
|
185
|
-
if (actual === expected) return;
|
|
186
|
-
console.warn("expected: " + expected);
|
|
187
|
-
console.warn("actual: " + actual);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function SKIP() {
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function tryRequire(name) {
|
|
194
|
-
try {
|
|
195
|
-
return require(name);
|
|
196
|
-
} catch (e) {
|
|
197
|
-
// ignore
|
|
198
|
-
}
|
|
199
|
-
}
|
package/tests/example.json
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"int0": 0,
|
|
3
|
-
"int1": 1,
|
|
4
|
-
"int1-": -1,
|
|
5
|
-
"int8": 255,
|
|
6
|
-
"int8-": -255,
|
|
7
|
-
"int16": 256,
|
|
8
|
-
"int16-": -256,
|
|
9
|
-
"int32": 65536,
|
|
10
|
-
"int32-": -65536,
|
|
11
|
-
"nil": null,
|
|
12
|
-
"true": true,
|
|
13
|
-
"false": false,
|
|
14
|
-
"float": 0.5,
|
|
15
|
-
"float-": -0.5,
|
|
16
|
-
"string0": "",
|
|
17
|
-
"string1": "A",
|
|
18
|
-
"string4": "foobarbaz",
|
|
19
|
-
"string8": "Omnes viae Romam ducunt.",
|
|
20
|
-
"string16": "L’homme n’est qu’un roseau, le plus faible de la nature ; mais c’est un roseau pensant. Il ne faut pas que l’univers entier s’arme pour l’écraser : une vapeur, une goutte d’eau, suffit pour le tuer. Mais, quand l’univers l’écraserait, l’homme serait encore plus noble que ce qui le tue, puisqu’il sait qu’il meurt, et l’avantage que l’univers a sur lui, l’univers n’en sait rien. Toute notre dignité consiste donc en la pensée. C’est de là qu’il faut nous relever et non de l’espace et de la durée, que nous ne saurions remplir. Travaillons donc à bien penser : voilà le principe de la morale.",
|
|
21
|
-
"array0": [],
|
|
22
|
-
"array1": [
|
|
23
|
-
"foo"
|
|
24
|
-
],
|
|
25
|
-
"array8": [
|
|
26
|
-
1,
|
|
27
|
-
2,
|
|
28
|
-
4,
|
|
29
|
-
8,
|
|
30
|
-
16,
|
|
31
|
-
32,
|
|
32
|
-
64,
|
|
33
|
-
128,
|
|
34
|
-
256,
|
|
35
|
-
512,
|
|
36
|
-
1024,
|
|
37
|
-
2048,
|
|
38
|
-
4096,
|
|
39
|
-
8192,
|
|
40
|
-
16384,
|
|
41
|
-
32768,
|
|
42
|
-
65536,
|
|
43
|
-
131072,
|
|
44
|
-
262144,
|
|
45
|
-
524288,
|
|
46
|
-
1048576
|
|
47
|
-
],
|
|
48
|
-
"map0": {},
|
|
49
|
-
"map1": {
|
|
50
|
-
"foo": "bar"
|
|
51
|
-
}
|
|
52
|
-
}
|