From 0f21dc9eb45c5c5f8a7e79f52d00b67ebf59a625 Mon Sep 17 00:00:00 2001 From: "Pascal S. de Kloe" Date: Tue, 6 Mar 2018 23:55:50 +0100 Subject: [PATCH] JavaScript benchmark thanks to @lewisdiamond. (issue #37) --- ecma/bench/Makefile | 15 ++++++++++++ ecma/bench/bench.js | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 ecma/bench/Makefile create mode 100644 ecma/bench/bench.js diff --git a/ecma/bench/Makefile b/ecma/bench/Makefile new file mode 100644 index 0000000..cdcb7c5 --- /dev/null +++ b/ecma/bench/Makefile @@ -0,0 +1,15 @@ +include ../../common.mk + +NODE?=node + +.PHONY: test +test: build + $(NODE) bench.js + $(NODE) --version + +build: install + $(COLF) -b build -s 1024 js ../../testdata/bench/scheme.colf + +.PHONY: clean +clean: + rm -fr build diff --git a/ecma/bench/bench.js b/ecma/bench/bench.js new file mode 100644 index 0000000..4ffecea --- /dev/null +++ b/ecma/bench/bench.js @@ -0,0 +1,56 @@ +global._ = require('./lodash.js') +const Benchmark = require('./benchmark.js'); + +const Colfer = require('./build/Colfer.js') + +const testData = [ + new Colfer.bench.Colfer({key: 1234567890, host: "db003lz12", port: 389, size: 452, hash: 0x5c2428488918, ratio: 0.99, route: true}), + new Colfer.bench.Colfer({key: 1234567891, host: "localhost", port: 22, size: 4096, hash: 0x48899c24c824, ratio: 0.20, route: false}), + new Colfer.bench.Colfer({key: 1234567892, host: "kdc.local", port: 88, size: 1984, hash: 0x48891c24485c, ratio: 0.06, route: false}), + new Colfer.bench.Colfer({key: 1234567893, host: "vhost8.dmz.example.com", port: 27017, size: 59741, hash: 0x08488b9c2489, ratio: 0.0, route: true}), +]; + +// Corresponding testData Colfer serials. +var testColfer = new Array(testData.length); +testData.forEach(function(o, i) { + testColfer[i] = o.marshal(); +}); + +// Corresponding testData JSON serials. +var testJSON = new Array(testData.length); +testData.forEach(function(o, i) { + testJSON[i] = JSON.stringify(o); +}); + +var suite = new Benchmark.Suite; +suite.add('marshal Colfer', function() { + testData[0].marshal(); + testData[1].marshal(); + testData[2].marshal(); + testData[3].marshal(); + }) + .add('unmarshal Colfer', function() { + new Colfer.bench.Colfer().unmarshal(testColfer[0]); + new Colfer.bench.Colfer().unmarshal(testColfer[1]); + new Colfer.bench.Colfer().unmarshal(testColfer[2]); + new Colfer.bench.Colfer().unmarshal(testColfer[3]); + }) + .add('marshal JSON', function() { + JSON.stringify(testData[0]); + JSON.stringify(testData[1]); + JSON.stringify(testData[2]); + JSON.stringify(testData[3]); + }) + .add('unmarshal JSON', function() { + JSON.parse(testJSON[0]); + JSON.parse(testJSON[1]); + JSON.parse(testJSON[2]); + JSON.parse(testJSON[3]); + }) + .on('error', function(event) { + console.log(String(event.target.error)); + }) + .on('cycle', function(event) { + console.log(String(event.target)) + }) + .run({async: true});