-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.Node.js
55 lines (52 loc) · 1.37 KB
/
test.Node.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
const Node = require('.').Node;
const counter = {
_DELAY: 1e3,
_DURATION: 6e3,
_count: 0,
_loop: null,
_ts: 0,
_loopCB() {
let ts = Date.now();
console.log("freq:", Math.round(counter._count / (ts - counter._ts)), "KHz");
},
_finishCB() {
console.log("count:", counter._count.toLocaleString());
process.exit();
},
increase() {
counter._count++;
},
start() {
if (counter._loop) counter.end();
counter._loop = setInterval(counter._loopCB, counter._DELAY);
counter._count = 0;
counter._ts = Date.now();
setTimeout(counter.end, counter._DURATION);
},
end() {
clearInterval(counter._loop);
counter._loop = null;
counter._finishCB();
}
};
const SIZE = 1e3;
const nodes = [];
console.log("type:", "chain");
console.log("size:", SIZE.toLocaleString());
console.time("build network");
for (let i = 0; i < SIZE; i++) {
nodes.push(
new Node({ id: "n" + i })
.on('spread', function (prevID) {
counter.increase();
this.emit('spread', this.data.id);
})
);
if (i > 0)
nodes[i].attachTo(nodes[i - 1]);
}
nodes[0].attachTo(nodes[SIZE - 1]);
// nodes[0].attachTo(nodes[1]); // interesting
console.timeEnd("build network");
counter.start();
nodes[0].trigger('spread');