Skip to content

Commit 4976850

Browse files
committed
Add a benchmark for TypedArray with zero-copy extension
1 parent fbcd36b commit 4976850

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

Diff for: benchmark/msgpack-benchmark.js

+61-15
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,45 @@
33
"use strict";
44
require("ts-node/register");
55
const Benchmark = require("benchmark");
6-
const fs = require("fs");
7-
const msgpack = require("../src");
6+
7+
const msgpackEncode = require("..").encode;
8+
const msgpackDecode = require("..").decode;
9+
const ExtensionCodec = require("..").ExtensionCodec;
10+
11+
const extensionCodec = new ExtensionCodec();
12+
extensionCodec.register({
13+
type: 0x01,
14+
encode: (object) => {
15+
if (object instanceof Float32Array) {
16+
return (pos) => {
17+
const bpe = Float32Array.BYTES_PER_ELEMENT;
18+
const padding = 1 + ((bpe - ((pos + 1) % bpe)) % bpe);
19+
const data = new Uint8Array(object.buffer);
20+
const result = new Uint8Array(padding + data.length);
21+
result[0] = padding;
22+
result.set(data, padding);
23+
return result;
24+
};
25+
}
26+
return null;
27+
},
28+
decode: (data) => {
29+
const padding = data[0];
30+
const bpe = Float32Array.BYTES_PER_ELEMENT;
31+
const offset = data.byteOffset + padding;
32+
const length = data.byteLength - padding;
33+
return new Float32Array(data.buffer, offset, length / bpe);
34+
},
35+
});
836

937
const implementations = {
1038
"@msgpack/msgpack": {
11-
encode: require("..").encode,
12-
decode: require("..").decode,
39+
encode: msgpackEncode,
40+
decode: msgpackDecode,
41+
},
42+
"@msgpack/msgpack (zero-copy extension)": {
43+
encode: (data) => msgpackEncode(data, { extensionCodec }),
44+
decode: (data) => msgpackDecode(data, { extensionCodec }),
1345
},
1446
"msgpack-lite": {
1547
encode: require("msgpack-lite").encode,
@@ -21,28 +53,42 @@ const implementations = {
2153
},
2254
};
2355

24-
// exactly the same as:
25-
// https://raw.githubusercontent.com/endel/msgpack-benchmark/master/sample-large.json
26-
const sampleFiles = ["./sample-large.json"];
56+
const samples = [
57+
// {
58+
// // exactly the same as:
59+
// // https://raw.githubusercontent.com/endel/msgpack-benchmark/master/sample-large.json
60+
// name: "./sample-large.json",
61+
// data: require("./sample-large.json"),
62+
// },
63+
{
64+
name: "Generated large Float32Array",
65+
data: [
66+
{
67+
position: new Float32Array(1e3).fill(1.14),
68+
},
69+
],
70+
},
71+
];
2772

2873
function validate(name, data, encoded) {
29-
if (JSON.stringify(data) !== JSON.stringify(implementations[name].decode(encoded))) {
30-
throw new Error("Bad implementation: " + name);
31-
}
74+
return JSON.stringify(data) === JSON.stringify(implementations[name].decode(encoded));
3275
}
3376

34-
for (const sampleFile of sampleFiles) {
35-
const data = require(sampleFile);
77+
for (const sample of samples) {
78+
const { name: sampleName, data } = sample;
3679
const encodeSuite = new Benchmark.Suite();
3780
const decodeSuite = new Benchmark.Suite();
3881

3982
console.log("");
40-
console.log("**" + sampleFile + ":** (" + JSON.stringify(data).length + " bytes in JSON)");
83+
console.log("**" + sampleName + ":** (" + JSON.stringify(data).length + " bytes in JSON)");
4184
console.log("");
4285

4386
for (const name of Object.keys(implementations)) {
4487
implementations[name].toDecode = implementations[name].encode(data);
45-
validate(name, data, implementations[name].toDecode);
88+
if (!validate(name, data, implementations[name].toDecode)) {
89+
console.log("Not supported by " + name);
90+
continue;
91+
}
4692
encodeSuite.add("(encode) " + name, () => {
4793
implementations[name].encode(data);
4894
});
@@ -60,7 +106,7 @@ for (const sampleFile of sampleFiles) {
60106

61107
console.log("");
62108

63-
decodeSuite.on("cycle", function(event) {
109+
decodeSuite.on("cycle", (event) => {
64110
console.log(String(event.target));
65111
});
66112

0 commit comments

Comments
 (0)