-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.ts
73 lines (64 loc) · 2.09 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* eslint-disable no-console */
/*
$ 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'
import { Test as ProtobufjsTest } from './protobufjs/bench.js'
import { Test as ProtobufTsTest } from './protobuf-ts/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])
}
function expectDecodedCorrectly (result: any) {
expect(result).to.have.nested.property('meh.lol', message.meh.lol)
expect(result).to.have.nested.property('meh.b.tmp.baz', message.meh.b.tmp.baz)
expect(result).to.have.property('hello', message.hello)
expect(result).to.have.property('foo', message.foo)
expect(result).to.have.property('payload').that.equalBytes(message.payload)
}
new Benchmark.Suite()
.add('pbjs', () => {
const buf = pbjsEncodeTest(message)
const result = pbjsDecodeTest(buf)
expectDecodedCorrectly(result)
})
.add('protons', () => {
const buf = ProtonsTest.encode(message)
const result = ProtonsTest.decode(buf)
expectDecodedCorrectly(result)
})
.add('protobuf.js', () => {
const buf = ProtobufjsTest.encode(message).finish()
const result = ProtobufjsTest.decode(buf)
expectDecodedCorrectly(result)
})
.add('@protobuf-ts', () => {
const buf = ProtobufTsTest.toBinary(message)
const result = ProtobufTsTest.fromBinary(buf)
expectDecodedCorrectly(result)
})
.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 })