This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
benchmark.js
196 lines (185 loc) · 5.08 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
process['UV_THREADPOOL_SIZE'] = 64;
var Cores = require('os').cpus().length;
var Concurrency = Math.max(2, Math.round(Cores / 2));
var Common = require('./common.js');
var Binding = {
'node': Common.independent,
'sync @ronomon': require('.'),
'async @ronomon': require('.')
};
var Queue = require('@ronomon/queue');
function Args(method, algorithm, sourceSize) {
var signatures = Common[method].signatures;
var signature = signatures[signatures.length - 1];
var array = [];
for (var index = 0; index < signature.length; index++) {
var key = signature[index];
switch (key) {
case 'algorithm':
array.push(algorithm.name);
break;
case 'encrypt':
array.push(1);
break;
case 'key':
if (!Slice(algorithm.keySize || 32, array)) return;
break;
case 'iv':
if (!Slice(algorithm.ivSize, array)) return;
break;
case 'source':
if (!Slice(sourceSize, array)) return;
break;
case 'aad':
if (!Slice(0, array)) return;
break;
case 'tag':
if (!Slice(algorithm.tagSize, array)) return;
break;
case 'keyOffset':
case 'ivOffset':
case 'sourceOffset':
case 'targetOffset':
case 'aadOffset':
case 'tagOffset':
array.push(0);
break;
case 'keySize':
case 'ivSize':
case 'sourceSize':
case 'aadSize':
case 'tagSize':
array.push(array[index - 2].length);
break;
case 'target':
if (method === 'cipher') {
if (!Slice(sourceSize + 32, array)) return;
} else {
if (!Slice(64, array)) return;
}
break;
default:
throw new Error('unsupported key: ' + key);
}
}
return array;
}
function Bench(binding, method, batch, sourceSize, end) {
var queue = new Queue(Concurrency);
var start = process.hrtime();
var latencies = 0;
if (binding === 'sync @ronomon') {
queue.onData = function(args, end) {
var hrtime = process.hrtime();
Binding[binding][method].apply(Binding[binding], args);
var elapsed = process.hrtime(hrtime);
latencies += (elapsed[0] * 1e9) + elapsed[1];
end();
};
} else {
queue.onData = function(args, end) {
var hrtime = process.hrtime();
Binding[binding][method].call(Binding[binding], ...args,
function(error) {
if (error) return end(error);
var elapsed = process.hrtime(hrtime);
latencies += (elapsed[0] * 1e9) + elapsed[1];
end();
}
);
};
}
queue.onEnd = function(error) {
if (error) return end(error);
var elapsed = process.hrtime(start);
var seconds = ((elapsed[0] * 1e9) + elapsed[1]) / 1000000 / 1000;
var latency = latencies / batch.length / 1000000;
var throughput = batch.length * sourceSize / 1000000 / (seconds || 1);
Print([
binding + ':',
'Latency:',
latency.toFixed(3) + 'ms',
'Throughput:',
throughput.toFixed(2) + ' MB/s'
]);
// Leave room for GC:
setTimeout(end, 100);
};
queue.concat(batch);
queue.end();
}
function Print(columns) {
console.log(columns[0].padStart(20, ' ') + ' ' + columns.slice(1).join(' '));
}
var Slab = Buffer.alloc(160 * 1024 * 1024, 255);
var SlabOffset = 0;
function Slice(size, array) {
if (SlabOffset + size > Slab.length) return false;
array.push(Slab.slice(SlabOffset, SlabOffset += size));
return true;
}
console.log('');
Print([ 'CPU:', require('os').cpus()[0].model ]);
Print([ 'Cores:', Cores ]);
Print([ 'Threads:', Concurrency ]);
var queue = new Queue();
queue.onData = function(method, end) {
var queue = new Queue();
queue.onData = function(algorithm, end) {
console.log('');
console.log(new Array(72 + 1).join('='));
var queue = new Queue();
queue.onData = function(sourceSize, end) {
SlabOffset = 0;
var batch = [];
var length = 16384;
while (length--) {
var args = Args(method, algorithm, sourceSize);
if (args) {
batch.push(args);
} else {
break;
}
}
batch = batch.slice(0, Math.pow(2, Math.floor(Math.log2(batch.length))));
console.log('');
Print([
(method === 'hmac' ? 'hmac-' : '') + algorithm.name + ':',
batch.length + ' x ' + sourceSize + ' Bytes'
]);
var queue = new Queue();
queue.onData = function(binding, end) {
Bench(binding, method, batch, sourceSize, end);
};
queue.onEnd = end;
queue.push('node');
queue.push('sync @ronomon');
queue.push('async @ronomon');
queue.end();
};
queue.onEnd = end;
queue.push(256);
queue.push(1024);
queue.push(4096);
queue.push(65536);
queue.push(1048576);
queue.end();
};
queue.onEnd = end;
queue.concat(Common[method].algorithm.filter(
function(algorithm) {
if (/^(md5|sha1)$/i.test(algorithm.name)) return false;
if (/-(128|192)-/i.test(algorithm.name)) return false;
return true;
}
));
queue.end();
};
queue.onEnd = function(error) {
if (error) throw error;
console.log('');
};
queue.push('cipher');
queue.push('hash');
queue.push('hmac');
queue.end();