Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: increase encoding/decoding performance #58

Merged
merged 14 commits into from
Aug 10, 2022
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ node_modules
package-lock.json
yarn.lock
.clinic
coverage
4 changes: 1 addition & 3 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"lerna": "4.0.0",
"packages": [
"packages/*"
],
"useWorkspaces": true,
"version": "independent",
"command": {
"run": {
Expand Down
11 changes: 7 additions & 4 deletions packages/protons-benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"sourceType": "module"
},
"ignorePatterns": [
"src/protobufjs/*.ts"
"src/pbjs/*",
"src/protobufjs/*"
]
},
"scripts": {
Expand All @@ -67,12 +68,14 @@
"start": "node dist/src/index.js"
},
"dependencies": {
"@types/benchmark": "^2.1.1",
"aegir": "^37.0.5",
"benny": "^3.7.1",
"benchmark": "^2.1.4",
"pbjs": "^0.0.14",
"protobufjs": "^6.11.2",
"protobufjs": "^7.0.0",
"protons": "^4.0.0",
"protons-runtime": "^2.0.0"
"protons-runtime": "^2.0.0",
"uint8arrays": "^3.1.0"
},
"private": true
}
1 change: 1 addition & 0 deletions packages/protons-benchmark/src/bench.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
syntax = "proto3";

message Foo {
optional uint32 baz = 1;
Expand Down
50 changes: 50 additions & 0 deletions packages/protons-benchmark/src/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable no-console */

/*
$ node dist/src/index.js
$ npx playwright-test dist/src/index.js --runner benchmark
*/

import Benchmark from 'benchmark'
import { Test as ProtonsTest } from './protons/bench.js'
import { decodeTest as pbjsDecodeTest } from './pbjs/bench.js'
import { Test as ProtobufjsTest } from './protobufjs/bench.js'

const message = {
meh: {
lol: 'sdkljfoee',
b: {
tmp: {
baz: 2309292
}
}
},
hello: 3493822,
foo: 'derp derp derp',
payload: Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
}

const buf = ProtonsTest.encode(message).subarray()

new Benchmark.Suite()
.add('pbjs', () => {
pbjsDecodeTest(buf)
})
.add('protons', () => {
ProtonsTest.decode(buf)
})
.add('protobufjs', () => {
ProtobufjsTest.decode(buf)
})
.on('error', (err: Error) => {
console.error(err)
})
.on('cycle', (event: any) => {
console.info(String(event.target))
})
.on('complete', function () {
// @ts-expect-error types are wrong
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
})
// run async
.run({ async: true })
48 changes: 48 additions & 0 deletions packages/protons-benchmark/src/encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable no-console */

/*
$ node dist/src/index.js
$ npx playwright-test dist/src/index.js --runner benchmark
*/

import Benchmark from 'benchmark'
import { Test as ProtonsTest } from './protons/bench.js'
import { encodeTest as pbjsEncodeTest } from './pbjs/bench.js'
import { Test as ProtobufjsTest } from './protobufjs/bench.js'

const message = {
meh: {
lol: 'sdkljfoee',
b: {
tmp: {
baz: 2309292
}
}
},
hello: 3493822,
foo: 'derp derp derp',
payload: Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
}

new Benchmark.Suite()
.add('pbjs', () => {
pbjsEncodeTest(message)
})
.add('protons', () => {
ProtonsTest.encode(message)
})
.add('protobufjs', () => {
ProtobufjsTest.encode(message).finish()
})
.on('error', (err: Error) => {
console.error(err)
})
.on('cycle', (event: any) => {
console.info(String(event.target))
})
.on('complete', function () {
// @ts-expect-error types are wrong
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
})
// run async
.run({ async: true })
42 changes: 26 additions & 16 deletions packages/protons-benchmark/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/* eslint-disable no-console */

import benny from 'benny'
/*
$ node dist/src/index.js
$ npx playwright-test dist/src/index.js --runner benchmark
*/

import Benchmark from 'benchmark'
import { expect } from 'aegir/chai'
import { Test as ProtonsTest } from './protons/bench.js'
import { encodeTest as pbjsEncodeTest, decodeTest as pbjsDecodeTest } from './pbjs/bench.js'
Expand Down Expand Up @@ -27,30 +33,34 @@ function expectDecodedCorrectly (result: any) {
expect(result).to.have.property('payload').that.equalBytes(message.payload)
}

void benny.suite(
'Encode/Decode',

benny.add('pbjs', () => {
new Benchmark.Suite()
.add('pbjs', () => {
const buf = pbjsEncodeTest(message)
const result = pbjsDecodeTest(buf)

expectDecodedCorrectly(result)
}),

benny.add('protons', () => {
})
.add('protons', () => {
const buf = ProtonsTest.encode(message)
const result = ProtonsTest.decode(buf)

expectDecodedCorrectly(result)
}),

benny.add('protobufjs', () => {
})
.add('protobufjs', () => {
const buf = ProtobufjsTest.encode(message).finish()
const result = ProtobufjsTest.decode(buf)

expectDecodedCorrectly(result)
}),

benny.cycle(),
benny.complete()
)
})
.on('error', (err: Error) => {
console.error(err)
})
.on('cycle', (event: any) => {
console.info(String(event.target))
})
.on('complete', function () {
// @ts-expect-error types are wrong
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
})
// run async
.run({ async: true })
Loading