-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (42 loc) · 1.34 KB
/
index.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
var WebSocket = require('ws');
var NanoTimer = require('nanotimer'); // We use nanotimer for better timeInverval accuracy
// /~https://github.com/Krb686/nanotimer#setintervaltask-args-interval-callback
var TIME_INTERVAL_BETWEEN_UPDATES = '250m';
var NB_USERS = 5;
var wss = new WebSocket.Server({ port: 4000 });
function random(min, max) {
return Math.random() * (max - min) + min;
}
function randomPosition(id) {
// Most of the cubes will be in the default benchmark user field of view
return JSON.stringify({
id: id,
x: random(-10, 10),
y: 0.5,
z: random(-2, -22),
})
}
var benchmarkNo = 0;
wss.on('connection', function connection(ws) {
ws.id = ++benchmarkNo;
console.log(`Benchmark user n°${ws.id} connected`);
console.log(`${NB_USERS} concurrent simulated users`);
console.log(`Time interval per simulated user of ${TIME_INTERVAL_BETWEEN_UPDATES}`);
function task() {
for(var id = 0; id < NB_USERS; id++) {
ws.send(randomPosition(id))
}
}
var timer = new NanoTimer();
timer.setInterval(task, '', TIME_INTERVAL_BETWEEN_UPDATES, function(err) {
if(err) {
//error
console.log(err);
}
});
ws.on('close', function close() {
timer.clearInterval();
console.log(`Test user n°${ws.id} disconnected`);
});
});
console.log('Benchmark is waiting for a benchmark user...');