-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
170 lines (131 loc) · 4.3 KB
/
test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
const assert = require('assert');
const path = require('path');
const Benchmark = require('benchmark');
const prettyBytes = require('pretty-bytes');
const Table = require('cli-table2');
const chalk = require('chalk');
const generate = require('./generate');
const schemas = {
avro: path.resolve('./schemas/avro.json'),
proto: path.resolve('./schemas/proto.proto')
};
const encoders = [
require('./encoders/json'),
require('./encoders/pson'),
require('./encoders/avsc')(schemas.avro),
require('./encoders/msgpack5'),
require('./encoders/msgpack-lite'),
require('./encoders/protobufjs')(schemas.proto)
];
const compressors = [
require('./compressors/none'),
require('./compressors/gzip')(6),
require('./compressors/deflate')(6),
require('./compressors/lz4'),
require('./compressors/snappy')
];
function main(n) {
const data = generate(n);
const suite = new Benchmark.Suite;
const info = [];
let minSizeTitle = null;
let [minSize, maxSize] = [Infinity, 0];
let [minPerf, maxPerf] = [Infinity, 0];
for (const encoder of encoders) {
const items = [];
for (const compressor of compressors) {
const title = `${encoder.name} + ${compressor.name}`;
const to = data => compressor.compress(encoder.encode(data));
const from = data => encoder.decode(compressor.decompress(data));
console.log(`Checking ${title}...`);
const encoded = to(data);
const actual = unify(from(encoded));
assert.deepEqual(actual, data);
suite.add(title, () => from(to(data)));
const size = encoded.length;
items.push({
compressor: compressor.name,
size: size
});
if (size < minSize) {
minSizeTitle = title;
minSize = size;
}
maxSize = Math.max(size, maxSize);
}
info.push({
encoder: encoder.name,
items,
});
}
console.log('Benchmarking...');
suite
.on('cycle', event => {
const [encoder, compressor] = event.target.name.split(' + ');
const item = info
.find(item => item.encoder === encoder)
.items
.find(item => item.compressor === compressor);
const perf = item.perf = event.target.hz;
minPerf = Math.min(perf, minPerf);
maxPerf = Math.max(perf, maxPerf);
console.log(String(event.target), '\t', `${prettyBytes(item.size)}`);
})
.on('complete', () => {
const fastest = suite.filter('fastest').map('name');
const smallest = minSizeTitle;
console.log(`Fastest is ${fastest}.`);
console.log(`Smallest is ${smallest}.`)
})
.run();
const table = new Table({
head: [''].concat(compressors.map(c => ({
hAlign: 'center',
content: c.name
})))
});
for (const item of info) {
table.push({
[item.encoder]: item.items.map(i => {
const perf = i.perf.toFixed(0) + ' ops/s';
const size = prettyBytes(i.size);
const cPerf = colorize(i.perf, minPerf, maxPerf, false)(perf);
const cSize = colorize(i.size, minSize, maxSize, true)(size);
return {
hAlign: 'right',
content: `${cPerf}\n${cSize}`
};
})
});
}
console.log();
console.log(String(table));
}
function unify(obj) {
return JSON.parse(JSON.stringify(clean(obj)));
}
function clean(obj) {
for (const name in obj) {
if (!obj.hasOwnProperty(name)) {
continue;
}
if (obj[name] === null) {
delete obj[name];
} else if (typeof obj[name] === 'object') {
clean(obj[name]);
}
}
return obj;
}
const colors = (
'red orange ' +
'white '.repeat(5) +
'limegreen green'
).split(' ');
function colorize(val, min, max, invert) {
let i = Math.floor((val - min) / (max - min + 1) * colors.length);
i = invert ? colors.length - i - 1 : i;
return chalk.keyword(colors[i]);
}
main(parseInt(process.argv[2], 10) || 20);