forked from bnoguchi/node-hash-ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_distribution.js
58 lines (49 loc) · 1.37 KB
/
test_distribution.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
var should = require('should');
var HashRing = require("../index");
var nodes = {
"127.0.0.1:8080": 1,
"127.0.0.2:8080": 1,
"127.0.0.3:8080": 1
};
var hasher = process.argv[3] || "md5";
console.log("testing with hasher", hasher);
var ring = new HashRing(nodes, hasher);
var iterations = 100000;
var genCode = function (length) {
length = length || 10;
var chars = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890",
numChars = chars.length;
var ret = "";
for (var i = 0; i < length; i++) {
ret += chars[parseInt(Math.random() * numChars, 10)];
}
return ret;
};
var randomDistribution = function () {
};
randomDistribution();
module.exports = {
'should be randomly distributed': function () {
var counts = {},
node, i, len, word;
for (i = 0, len = nodes.length; i < len; i++) {
node = nodes[i];
counts[node] = 0;
}
for (i = 0, len = iterations; i < len; i++) {
word = genCode(10);
node = ring.getNode(word);
counts[node] = counts[node] || 0;
counts[node]++;
}
var total = Object.keys(counts).reduce( function (sum, node) {
return sum += counts[node];
}, 0.0);
var delta = 0.05
, lower = 1.0 / 3 - 0.05
, upper = 1.0 / 3 + 0.05;
for (node in counts) {
(counts[node] / total).should.be.within(lower, upper);
}
}
};