-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
136 lines (98 loc) · 2.85 KB
/
client.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const net = require("net");
const { WebSocket } = require("ws");
const zero = require("dev-zero-stream");
let serverAddress = process.env.HOST;
const parameters = process.argv.slice(2);
let upload = false;
let download = false;
let downloadMbpsToSend = 0;
if (parameters.length > 0) {
parameters.find((param) => {
if (param.includes("-D")) {
download = true;
console.log("Download test");
} else if (param.includes("-U")) {
upload = true;
console.log("Upload test");
}
});
}
function start() {
console.log(`SERVER: ${serverAddress}`);
const ws = new WebSocket(`ws://${serverAddress}:9075/`);
const client = new net.Socket();
client.connect(9074, serverAddress);
let pbi = 0;
const pingBuffer = Array(5).fill(0);
const totalWeight =
(pingBuffer.length / 2) *
(2 * pingBuffer.length + (pingBuffer.length - 1) * -1);
let lastPingTime = 0;
function sendPing() {
lastPingTime = Date.now();
ws.send("I");
}
ws.on("open", () => {
sendPing();
});
function runUploadTest() {
let sinceLastUpload = 0;
const stream = zero();
stream.on("data", (data) => {
sinceLastUpload += data.length;
});
stream.pipe(client);
}
if (upload) {
runUploadTest();
} else {
console.log("No upload test issued");
}
function runDownloadSpeedTest() {
let last = Date.now();
let sinceLastDownload = 0;
client.on("data", (data) => {
sinceLastDownload += data.length;
});
client.setMaxListeners(0);
setInterval(() => {
const sinceLastDownloadMB = (sinceLastDownload * 8) / (1024 * 1024);
const diff = (Date.now() - last) / 1000;
const downloadMbps = Math.round((sinceLastDownloadMB / diff) * 100) / 100;
downloadMbpsToSend = downloadMbps;
console.log(`Download rate: ${downloadMbps} Mb/s`);
sinceLastDownload = 0;
last = Date.now();
}, 1000);
}
if (download) {
runDownloadSpeedTest();
} else {
console.log("No download test issued");
}
ws.on("message", (message) => {
const msg = message.toString();
if (msg[0] === "O") {
const diff = Date.now() - lastPingTime;
pingBuffer[pbi] = diff / 3;
let sum = 0;
for (let i = 0; i < pingBuffer.length; i++) {
const idx = (pbi + i * -1 + pingBuffer.length) % pingBuffer.length;
const pingValue = pingBuffer[idx] * (pingBuffer.length - i);
sum += pingValue;
}
const averagePing = sum / totalWeight;
const d = downloadMbpsToSend;
ws.send(`D ${d} ${averagePing}`);
// ws.send(
// `P:${Math.round(
// averagePing
// )} ms ${downloadMbpsToSend} ${uploadMbpsToSend}`
// );
console.log(`${Math.round(averagePing)} ms`, downloadMbpsToSend);
pbi = (pbi + 1) % pingBuffer.length;
setTimeout(sendPing, 500);
}
});
}
start();