-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark.js
176 lines (165 loc) · 4.49 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
var cpus = require('os').cpus();
var cpu = cpus[0].model;
var cores = cpus.length;
var concurrency = 1;
var QuotedPrintable = require('./index.js');
var Queue = require('@ronomon/queue');
var RNG = function(seed) {
var self = this;
if (seed === undefined) seed = Date.now();
if (typeof seed !== 'number' || Math.round(seed) !== seed || seed < 0) {
throw new Error('bad seed');
}
self.seed = seed % Math.pow(2, 31);
self.hash = self.seed;
};
RNG.prototype.random = function() {
var self = this;
self.hash = ((self.hash + 0x7ED55D16) + (self.hash << 12)) & 0xFFFFFFF;
self.hash = ((self.hash ^ 0xC761C23C) ^ (self.hash >>> 19)) & 0xFFFFFFF;
self.hash = ((self.hash + 0x165667B1) + (self.hash << 5)) & 0xFFFFFFF;
self.hash = ((self.hash + 0xD3A2646C) ^ (self.hash << 9)) & 0xFFFFFFF;
self.hash = ((self.hash + 0xFD7046C5) + (self.hash << 3)) & 0xFFFFFFF;
self.hash = ((self.hash ^ 0xB55A4F09) ^ (self.hash >>> 16)) & 0xFFFFFFF;
return (self.hash & 0xFFFFFFF) / 0x10000000;
};
var rng = new RNG();
var random = rng.random.bind(rng);
function generateBuffer(size) {
var buffer = Buffer.alloc(size);
while (size--) buffer[size] = Math.floor(random() * 256);
return buffer;
}
var bindings = [
{
name: 'Javascript',
decode: function(buffer, end) {
var options = { binding: QuotedPrintable.binding.javascript };
QuotedPrintable.decode(buffer, options);
end();
},
encode: function(buffer, end) {
var options = { binding: QuotedPrintable.binding.javascript };
QuotedPrintable.encode(buffer, options);
end();
}
},
{
name: 'Native',
decode: function(buffer, end) {
var options = { binding: QuotedPrintable.binding.native };
QuotedPrintable.decode(buffer, options);
end();
},
encode: function(buffer, end) {
var options = { binding: QuotedPrintable.binding.native };
QuotedPrintable.encode(buffer, options);
end();
}
}
];
var vectors = {};
var sizes = [
32,
128,
512,
2048,
8192,
32768
];
sizes.forEach(
function(size) {
var encode = [];
var decode = [];
var count = Math.ceil(1 * 1024 * 1024 / size);
while (count-- > 0) {
var buffer = generateBuffer(size);
encode.push(buffer);
decode.push(QuotedPrintable.encode(buffer));
}
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'
]);
// Rest between benchmarks to leave room for GC:
setTimeout(end, 100);
};
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();