-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark.js
106 lines (89 loc) · 3.12 KB
/
benchmark.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var msgpack = require('msgpack'),
msgpackJs = require('msgpack-js'),
msgpack5 = require('msgpack5')(),
PSON = require('pson'),
pson = new PSON.StaticPair(),
avsc = require('avsc')
// Avro type declarations for avsc.
var typeWithoutBytes = avsc.parse({
name: 'Record',
type: 'record',
fields: [
{name: 'abcdef', type: 'int'},
{name: 'qqq', type: 'int'},
{name: 'a19', type: {type: 'array', items: 'int'}},
]
})
var typeWithBytes = avsc.parse({
name: 'Record',
type: 'record',
fields: [
{name: 'abcdef', type: 'int'},
{name: 'qqq', type: 'int'},
{name: 'a19', type: {type: 'array', items: 'int'}},
{name: 'buf', type: {name: 'Buf', type: 'fixed', size: 256}}
]
})
function benchmark(name, data) {
var testCount = 1e5,
packed
console.time(`${name} msgpack.pack`)
for (var i = 0; i < testCount; i++)
msgpack.pack(data)
console.timeEnd(`${name} msgpack.pack`)
encoded = msgpack.pack(data)
console.time(`${name} msgpack.unpack`)
for (var i = 0; i < testCount; i++)
msgpack.unpack(encoded)
console.timeEnd(`${name} msgpack.unpack`)
console.time(`${name} msgpack-js.encode`)
for (var i = 0; i < testCount; i++)
msgpackJs.encode(data)
console.timeEnd(`${name} msgpack-js.encode`)
encoded = msgpackJs.encode(data)
console.time(`${name} msgpack-js.decode`)
for (var i = 0; i < testCount; i++)
msgpackJs.decode(encoded)
console.timeEnd(`${name} msgpack-js.decode`)
console.time(`${name} pson.encode`)
for (var i = 0; i < testCount; i++)
pson.encode(data)
console.timeEnd(`${name} pson.encode`)
encoded = pson.toBuffer(data) // encoded data results in a "Error: Truncated"
console.time(`${name} pson.decode`)
for (var i = 0; i < testCount; i++)
pson.decode(encoded)
console.timeEnd(`${name} pson.decode`)
var type = data.buf ? typeWithBytes : typeWithoutBytes
console.time(`${name} avsc.toBuffer`)
for (var i = 0; i < testCount; i++)
type.toBuffer(data)
console.timeEnd(`${name} avsc.toBuffer`)
encoded = type.toBuffer(data)
console.time(`${name} avsc.fromBuffer`)
for (var i = 0; i < testCount; i++)
type.fromBuffer(encoded)
console.timeEnd(`${name} avsc.fromBuffer`)
console.time(`${name} JSON.stringify`)
for (var i = 0; i < testCount; i++)
JSON.stringify(data)
console.timeEnd(`${name} JSON.stringify`)
encoded = JSON.stringify(data)
console.time(`${name} JSON.parse`)
for (var i = 0; i < testCount; i++)
JSON.parse(encoded)
console.timeEnd(`${name} JSON.parse`)
console.time(`${name} msgpack5.encode`)
for (var i = 0; i < testCount; i++)
msgpack5.encode(data)
console.timeEnd(`${name} msgpack5.encode`)
encoded = msgpack5.encode(data)
console.time(`${name} msgpack5.decode`)
for (var i = 0; i < testCount; i++)
msgpack5.decode(encoded)
console.timeEnd(`${name} msgpack5.decode`)
}
var dataWithBinary = { 'abcdef' : 1, 'qqq' : 13, 'a19' : [1, 2, 3, 4], buf: require('crypto').randomBytes(256) },
dataPlain = { 'abcdef' : 1, 'qqq' : 13, 'a19' : [1, 2, 3, 4] }
benchmark('no binary buffers', dataPlain)
benchmark('with binary buffers', dataWithBinary)