-
Notifications
You must be signed in to change notification settings - Fork 70
/
layer-check.js
92 lines (60 loc) · 1.71 KB
/
layer-check.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
var fs = require('fs');
var redis = require('redis');
var srand = require('srand');
var client = redis.createClient(6379, '127.0.0.1');
var checksource = fs.readFileSync('layer-check.lua', 'ascii');
var entries = process.argv[2] || 10000;
var precision = process.argv[3] || 0.01;
var checksha = '';
var start;
var count = process.argv[4] || 100000;
var found = [];
var added = 0;
console.log('entries = ' + entries);
console.log('precision = ' + (precision * 100) + '%');
console.log('count = ' + count);
srand.seed(2);
function check(n) {
if (n == count) {
var sec = count / ((Date.now() - start) / 1000);
console.log(sec + ' per second');
var total = 0;
for (var i = 1; i < found.length; ++i) {
total += found[i];
console.log('layer ' + i + ': ' + (found[i] / (count / 100)) + '% false positives');
}
console.log((total / (count / 100)) + '% false positives total');
console.log('done.');
process.exit();
return;
}
// Mimic the same number of srand.random() calls as layer-add.js
// so we get the same id's if we use the same seed.
while (added > 100 && srand.random() < 0.3) {
srand.random();
++added;
}
var id = Math.ceil(srand.random() * 4000000000);
client.evalsha(checksha, 0, 'test', entries, precision, id, function(err, layer) {
if (err) {
throw err;
}
if (layer) {
if (found[layer]) {
found[layer]++;
} else {
found[layer] = 1;
}
}
check(n + 1);
});
}
client.send_command('script', ['load', checksource], function(err, sha) {
if (err) {
throw err;
}
checksha = sha;
console.log('adding check function... ' + checksha);
start = Date.now();
check(0);
});