Skip to content

Commit

Permalink
Revamp benchmark runner
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Dec 30, 2022
1 parent d3f5e34 commit b8e7127
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 210 deletions.
1 change: 0 additions & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ wasm-bindgen = { version = "^0.2" }
crate-type = ["cdylib", "rlib"]

[features]
default = ["serde-json", "serde-wasm-bindgen"]
serde-json = ["wasm-bindgen/serde-serialize"]
msgpack = ["rmp-serde"]

Expand Down
46 changes: 0 additions & 46 deletions benchmarks/index.js

This file was deleted.

82 changes: 82 additions & 0 deletions benchmarks/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Benchmark from 'benchmark';
import { readdirSync, readFileSync } from 'fs';
import prettyBytes from 'pretty-bytes';
import { fileSync as brotliSize } from 'brotli-size';
import { createRequire } from 'module';
import { inspect } from 'util';

const { Suite } = Benchmark;
const require = createRequire(import.meta.url);

let suiteOps = {
parse: {},
serialize: {}
};

const libs = readdirSync('pkg').map(dir => ({
name: dir,
impl: require(`./pkg/${dir}`)
}));

console.log('=== Sizes ===');
for (let { name } of libs) {
const [js, wasm] = [
'serde_wasm_bindgen_benches.js',
'serde_wasm_bindgen_benches_bg.wasm'
].map(file => prettyBytes(brotliSize(`pkg/${name}/${file}`)));

console.log(`${name}: JS = ${js}, Wasm = ${wasm}`);
}
console.log();

function loadData(name) {
return JSON.parse(readFileSync(`./data/${name}.json`, 'utf8'));
}

const datasets = {
Canada: loadData('canada'),
CitmCatalog: loadData('citm_catalog'),
Twitter: loadData('twitter')
};

let filter = new RegExp(process.argv[2] || '(?:)');

for (let { name: libName, impl } of libs) {
for (let [dataName, json] of Object.entries(datasets)) {
let { parse } = impl[dataName];

let parseBenchName = `parse ${dataName} ${libName}`;
if (filter.test(parseBenchName)) {
(suiteOps.parse[dataName] ??= new Suite(dataName)).add(libName, () =>
parse(json).free()
);
}

let serializeBenchName = `serialize ${dataName} ${libName}`;
if (filter.test(serializeBenchName)) {
let parsed = parse(json);
(suiteOps.serialize[dataName] ??= new Suite(dataName)).add(
libName,
() => parsed.serialize(),
{
onComplete: () => parsed.free()
}
);
}
}
}

console.log('=== Benchmarks ===');

for (let [op, suites] of Object.entries(suiteOps)) {
console.group(op);
for (let suite of Object.values(suites)) {
console.group(suite.name);
suite
.on('error', event => console.error(event.target.error))
.on('cycle', event => console.log(event.target.toString()))
.run();
console.groupEnd();
}
console.groupEnd();
}
175 changes: 174 additions & 1 deletion benchmarks/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
"private": true,
"dependencies": {
"@msgpack/msgpack": "^2.8.0",
"benchmark": "^2.1.4"
"benchmark": "^2.1.4",
"brotli-size": "^4.0.0",
"cross-env": "^7.0.3",
"pretty-bytes": "^6.0.0"
},
"scripts": {
"pretest": "wasm-pack build -t nodejs --profiling",
"test": "node index.js"
"build:swb": "wasm-pack build -t nodejs --out-dir pkg/serde-wasm-bindgen -- --features serde-wasm-bindgen",
"build:swb-reftypes": "cross-env RUSTFLAGS=\"-C target-feature=+reference-types\" WASM_BINDGEN_EXTERNREF=1 wasm-pack build -t nodejs --out-dir pkg/serde-wasm-bindgen-reftypes -- --features serde-wasm-bindgen",
"build:json": "wasm-pack build -t nodejs --out-dir pkg/serde-json -- --features serde-json",
"build:msgpack": "wasm-pack build -t nodejs --out-dir pkg/msgpack -- --features msgpack",
"build": "npm run build:swb && npm run build:swb-reftypes && npm run build:json",
"test": "node index.mjs"
}
}
Loading

0 comments on commit b8e7127

Please sign in to comment.