-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.js
82 lines (75 loc) · 1.85 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
var Crypto = require('crypto');
var Quorum = require('./index.js');
var objects = 1000;
var vectorOffset = 0;
var objectSize = Quorum.VECTOR;
var sourceOffset = 0;
var sourceSize = objects * objectSize;
var quorum = Buffer.alloc(objects * Quorum.SIZE);
var quorumOffset = 0;
var target = Buffer.alloc(sourceSize);
var targetOffset = 0;
var a = Crypto.randomBytes(sourceSize);
var b = Crypto.randomBytes(sourceSize);
function ns(time, runs) {
var elapsed = process.hrtime(time);
return Math.round(
((elapsed[0] * 1000 * 1000000) + elapsed[1]) / objects / runs
);
}
console.log('');
console.log(' ' + require('os').cpus()[0].model);
console.log('');
console.log(' NS PER OBJECT');
console.log('');
for (var length = 1; length <= Quorum.SOURCES_MAX; length *= 2) {
// Fast path:
var sources = [];
for (var index = 0; index < length; index++) {
sources.push(index % 3 ? a : b);
}
var time = process.hrtime();
var runs = 100;
for (var index = 0; index < runs; index++) {
Quorum.calculate(
vectorOffset,
objectSize,
sourceOffset,
sourceSize,
sources,
quorum,
quorumOffset,
target,
targetOffset
);
}
var fast = ns(time, runs);
// Slow path:
var sources = [];
for (var index = 0; index < length; index++) {
sources.push(Crypto.randomBytes(sourceSize));
}
var time = process.hrtime();
var runs = 10;
for (var index = 0; index < runs; index++) {
Quorum.calculate(
vectorOffset,
objectSize,
sourceOffset,
sourceSize,
sources,
quorum,
quorumOffset,
target,
targetOffset
);
}
var slow = ns(time, runs);
if (slow < fast) slow = fast;
console.log(
' REPLICAS=' + length.toString().padEnd(4, ' ') +
' FAST=' + (fast + 'ns').padEnd(7, ' ') +
' SLOW=' + (slow + 'ns')
);
}
console.log('');