From ac52f1a6c0438c85361e0073a7685846a27faa50 Mon Sep 17 00:00:00 2001 From: Karang Date: Wed, 20 May 2020 01:36:59 +0200 Subject: [PATCH] Add compiled benchmarks --- benchmark/benchmark_all_types.js | 43 +++++++++++++++++++++++++++++-- benchmark/benchmark_by_kind.js | 37 ++++++++++++++++++++++++-- benchmark/benchmark_by_subtype.js | 29 +++++++++++++++++++-- benchmark/benchmark_by_test.js | 24 +++++++++++++++-- benchmark/benchmark_by_type.js | 33 ++++++++++++++++++++++-- benchmark/benchmark_unified.js | 24 +++++++++++++++-- src/datatypes/compiler-utils.js | 4 +-- test/dataTypes/prepareTests.js | 8 +++--- 8 files changed, 184 insertions(+), 18 deletions(-) diff --git a/benchmark/benchmark_all_types.js b/benchmark/benchmark_all_types.js index e540f48..a73f4d3 100644 --- a/benchmark/benchmark_all_types.js +++ b/benchmark/benchmark_all_types.js @@ -1,7 +1,6 @@ /* eslint-env mocha */ -const testData = require('../test/dataTypes/prepareTests').testData -const proto = require('../test/dataTypes/prepareTests').proto +const { testData, proto, compiledProto } = require('../test/dataTypes/prepareTests') const Benchmark = require('benchmark') it('reads', function () { @@ -43,3 +42,43 @@ it('writes', function () { }) .run({ 'async': false }) }) + +it('reads (compiled)', function () { + this.timeout(1000 * 60 * 10) + const readSuite = new Benchmark.Suite() + readSuite.add('read (compiled)', function () { + testData.forEach(tests => { + tests.data.forEach(test => { + test.subtypes.forEach(subType => { + subType.values.forEach((value) => { + compiledProto.parsePacketBuffer(subType.type, value.buffer) + }) + }) + }) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) +}) + +it('writes (compiled)', function () { + this.timeout(1000 * 60 * 10) + const writeSuite = new Benchmark.Suite() + writeSuite.add('write (compiled)', function () { + testData.forEach(tests => { + tests.data.forEach(test => { + test.subtypes.forEach(subType => { + subType.values.forEach((value) => { + compiledProto.createPacketBuffer(subType.type, value.value) + }) + }) + }) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) +}) diff --git a/benchmark/benchmark_by_kind.js b/benchmark/benchmark_by_kind.js index 92ca811..3bf4ce6 100644 --- a/benchmark/benchmark_by_kind.js +++ b/benchmark/benchmark_by_kind.js @@ -1,7 +1,6 @@ /* eslint-env mocha */ -const testData = require('../test/dataTypes/prepareTests').testData -const proto = require('../test/dataTypes/prepareTests').proto +const { testData, proto, compiledProto } = require('../test/dataTypes/prepareTests') const Benchmark = require('benchmark') testData.forEach(tests => { @@ -41,5 +40,39 @@ testData.forEach(tests => { }) .run({ 'async': false }) }) + + it('reads (compiled)', function () { + const readSuite = new Benchmark.Suite() + readSuite.add('read (compiled)', function () { + tests.data.forEach(test => { + test.subtypes.forEach(subType => { + subType.values.forEach((value) => { + compiledProto.parsePacketBuffer(subType.type, value.buffer) + }) + }) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) + }) + + it('writes (compiled)', function () { + const writeSuite = new Benchmark.Suite() + writeSuite.add('write (compiled)', function () { + tests.data.forEach(test => { + test.subtypes.forEach(subType => { + subType.values.forEach((value) => { + compiledProto.createPacketBuffer(subType.type, value.value) + }) + }) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) + }) }) }) diff --git a/benchmark/benchmark_by_subtype.js b/benchmark/benchmark_by_subtype.js index 8c6c393..bf531aa 100644 --- a/benchmark/benchmark_by_subtype.js +++ b/benchmark/benchmark_by_subtype.js @@ -1,7 +1,6 @@ /* eslint-env mocha */ -const testData = require('../test/dataTypes/prepareTests').testData -const proto = require('../test/dataTypes/prepareTests').proto +const { testData, proto, compiledProto } = require('../test/dataTypes/prepareTests') const Benchmark = require('benchmark') function testType (type, values) { @@ -30,6 +29,32 @@ function testType (type, values) { }) .run({ 'async': false }) }) + + it('reads (compiled)', function () { + const readSuite = new Benchmark.Suite() + readSuite.add('read (compiled)', function () { + values.forEach((value) => { + compiledProto.parsePacketBuffer(type, value.buffer) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) + }) + + it('writes (compiled)', function () { + const writeSuite = new Benchmark.Suite() + writeSuite.add('write (compiled)', function () { + values.forEach((value) => { + compiledProto.createPacketBuffer(type, value.value) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) + }) } testData.forEach(tests => { diff --git a/benchmark/benchmark_by_test.js b/benchmark/benchmark_by_test.js index 50a7e24..f13bc21 100644 --- a/benchmark/benchmark_by_test.js +++ b/benchmark/benchmark_by_test.js @@ -1,7 +1,6 @@ /* eslint-env mocha */ -const testData = require('../test/dataTypes/prepareTests').testData -const proto = require('../test/dataTypes/prepareTests').proto +const { testData, proto, compiledProto } = require('../test/dataTypes/prepareTests') const Benchmark = require('benchmark') function testValue (type, value, buffer) { @@ -25,6 +24,27 @@ function testValue (type, value, buffer) { }) .run({ 'async': false }) }) + + it('writes (compiled)', function () { + const suite = new Benchmark.Suite() + suite.add('writes (compiled)', function () { + compiledProto.createPacketBuffer(type, value) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) + }) + it('reads (compiled)', function () { + const suite = new Benchmark.Suite() + suite.add('read (compiled)', function () { + compiledProto.parsePacketBuffer(type, buffer) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) + }) } function testType (type, values) { diff --git a/benchmark/benchmark_by_type.js b/benchmark/benchmark_by_type.js index 6cb5a64..60d72e9 100644 --- a/benchmark/benchmark_by_type.js +++ b/benchmark/benchmark_by_type.js @@ -1,7 +1,6 @@ /* eslint-env mocha */ -const testData = require('../test/dataTypes/prepareTests').testData -const proto = require('../test/dataTypes/prepareTests').proto +const { testData, proto, compiledProto } = require('../test/dataTypes/prepareTests') const Benchmark = require('benchmark') testData.forEach(tests => { @@ -39,6 +38,36 @@ testData.forEach(tests => { }) .run({ 'async': false }) }) + + it('reads (compiled)', function () { + const readSuite = new Benchmark.Suite() + readSuite.add('read (compiled)', function () { + test.subtypes.forEach(subType => { + subType.values.forEach((value) => { + compiledProto.parsePacketBuffer(subType.type, value.buffer) + }) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) + }) + + it('writes (compiled)', function () { + const writeSuite = new Benchmark.Suite() + writeSuite.add('write (compiled)', function () { + test.subtypes.forEach(subType => { + subType.values.forEach((value) => { + compiledProto.createPacketBuffer(subType.type, value.value) + }) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) + }) }) }) }) diff --git a/benchmark/benchmark_unified.js b/benchmark/benchmark_unified.js index fb7036d..15aa1d4 100644 --- a/benchmark/benchmark_unified.js +++ b/benchmark/benchmark_unified.js @@ -1,7 +1,6 @@ /* eslint-env mocha */ -const testData = require('../test/dataTypes/prepareTests').testData -const proto = require('../test/dataTypes/prepareTests').proto +const { testData, proto, compiledProto } = require('../test/dataTypes/prepareTests') const Benchmark = require('benchmark') it('read/write', function () { @@ -24,3 +23,24 @@ it('read/write', function () { }) .run({ 'async': false }) }) + +it('read/write (compiled)', function () { + this.timeout(1000 * 60 * 10) + const suite = new Benchmark.Suite() + suite.add('read/write (compiled)', function () { + testData.forEach(tests => { + tests.data.forEach(test => { + test.subtypes.forEach(subType => { + subType.values.forEach((value) => { + compiledProto.parsePacketBuffer(subType.type, value.buffer) + compiledProto.createPacketBuffer(subType.type, value.value) + }) + }) + }) + }) + }) + .on('cycle', function (event) { + console.log(String(event.target)) + }) + .run({ 'async': false }) +}) diff --git a/src/datatypes/compiler-utils.js b/src/datatypes/compiler-utils.js index c411869..740c6ca 100644 --- a/src/datatypes/compiler-utils.js +++ b/src/datatypes/compiler-utils.js @@ -111,9 +111,7 @@ module.exports = { } } if (bits !== 0) { - code += 'buffer[offset++] = ' + toWrite + ' << ' + (8 - bits) + '\n' - bits = 0 - toWrite = '' + code += 'buffer[offset++] = (' + toWrite + ') << ' + (8 - bits) + '\n' } code += 'return offset' return compiler.wrapCode(code) diff --git a/test/dataTypes/prepareTests.js b/test/dataTypes/prepareTests.js index cf43b2d..093c110 100644 --- a/test/dataTypes/prepareTests.js +++ b/test/dataTypes/prepareTests.js @@ -59,6 +59,8 @@ testData.forEach(tests => { }) }) -module.exports.compiledProto = compiler.compileProtoDefSync() -module.exports.testData = testData -module.exports.proto = proto +module.exports = { + testData, + proto, + compiledProto: compiler.compileProtoDefSync() +}