forked from ronomon/base64
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.js
164 lines (156 loc) · 3.87 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
var cpus = require('os').cpus();
var cpu = cpus[0].model;
var cores = cpus.length;
var concurrency = 1;
var crypto = require('crypto');
var Base64 = require('./index.js');
var Queue = require('@ronomon/queue');
var bindings = [
{
name: 'Javascript',
decode: function(buffer, end) {
var options = { binding: Base64.binding.javascript };
Base64.decode(buffer, options);
end();
},
encode: function(buffer, end) {
var options = { binding: Base64.binding.javascript };
Base64.encode(buffer, options);
end();
}
},
{
name: 'Node',
decode: function(buffer, end) {
Buffer.from(buffer.toString('ascii'), 'base64');
end();
},
encode: function(buffer, end) {
Buffer.from(buffer.toString('base64'), 'ascii');
end();
}
}
];
if (Base64.binding.native) {
bindings.splice(1, 0, {
name: 'Native',
decode: function(buffer, end) {
var options = { binding: Base64.binding.native };
Base64.decode(buffer, options);
end();
},
encode: function(buffer, end) {
var options = { binding: Base64.binding.native };
Base64.encode(buffer, options);
end();
}
});
}
var vectors = {};
var sizes = [
128,
1024,
32768,
1 * 1024 * 1024,
4 * 1024 * 1024
];
sizes.forEach(
function(size) {
var encode = [];
var decode = [];
var count = Math.max(10, Math.ceil(1024 * 1024 / size));
while (count-- > 0) {
var decoded = crypto.randomBytes(size);
encode.push(decoded);
// Decoder should not switch to slow-mode permanently on any white space:
var encoded = Buffer.concat([
Buffer.from(' ', 'ascii'),
Base64.encode(decoded)
]);
decode.push(encoded);
}
vectors[size] = {
encode: encode,
decode: decode
};
}
);
function benchmark(binding, method, buffers, end) {
var now = Date.now();
var sum = 0;
var time = 0;
var count = 0;
var queue = new Queue(1);
queue.onData = function(buffer, end) {
var hrtime = process.hrtime();
binding[method](buffer,
function(error) {
if (error) return end(error);
var difference = process.hrtime(hrtime);
var ns = (difference[0] * 1e9) + difference[1];
// Count the number of data bytes that can be processed per second:
sum += buffer.length;
time += ns;
count++;
end();
}
);
};
queue.onEnd = function(error) {
if (error) return end(error);
var elapsed = Date.now() - now;
var latency = (time / count) / 1000000;
var throughput = sum / elapsed / 1000;
display([
binding.name + ':',
'Latency:',
latency.toFixed(3) + 'ms',
'Throughput:',
throughput.toFixed(2) + ' MB/s'
]);
end();
};
queue.concat(buffers);
queue.end();
}
function display(columns) {
var string = columns[0];
while (string.length < 15) string = ' ' + string;
string += ' ' + columns.slice(1).join(' ');
console.log(string);
}
console.log('');
display([ 'CPU:', cpu ]);
display([ 'Cores:', cores ]);
display([ 'Threads:', concurrency ]);
var queue = new Queue();
queue.onData = function(method, end) {
console.log('');
console.log('============================================================');
var queue = new Queue();
queue.onData = function(size, end) {
var buffers = vectors[size][method];
console.log('');
display([
method.slice(0, 1).toUpperCase() + method.slice(1) + ':',
buffers.length + ' x ' + size + ' Bytes'
]);
var queue = new Queue();
queue.onData = function(binding, end) {
benchmark(binding, method, buffers, end);
};
queue.onEnd = end;
queue.concat(bindings);
queue.end();
};
queue.onEnd = end;
queue.concat(sizes);
queue.end();
};
queue.onEnd = function(error) {
if (error) throw error;
console.log('');
};
queue.push('encode');
queue.push('decode');
queue.end();