forked from kenigbolo/network-speed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (61 loc) · 1.97 KB
/
app.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
const http = require('http');
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+`-=[]{}|;':,./<>?";
class NetworkSpeedCheck {
constructor() {}
checkDownloadSpeed(baseUrl, fileSize) {
let startTime;
return new Promise((resolve, _) => {
return http.get(baseUrl, response => {
response.once('data', () => {
startTime = new Date().getTime();
});
response.once('end', () => {
const endTime = new Date().getTime();
const duration = (endTime - startTime) / 1000;
const bitsLoaded = fileSize * 8;
const bps = (bitsLoaded / duration).toFixed(2);
const kbps = (bps / 1024).toFixed(2);
const mbps = (kbps / 1024).toFixed(2);
resolve({bps, kbps, mbps});
});
});
}).catch(error => {
throw new Error(error);
});
}
checkUploadSpeed(options) {
let startTime;
const data = '{"data": "' + this.generateTestData(20) + '"}';
return new Promise((resolve, _) => {
var req = http.request(options, res => {
res.setEncoding('utf8');
res.on('data', () => {
startTime = new Date().getTime();
});
res.on('end', () => {
const endTime = new Date().getTime();
const duration = (endTime - startTime) / 1000;
const bitsLoaded = 20 * 8;
const bps = (bitsLoaded / duration).toFixed(2);
const kbps = (bps / 1024).toFixed(2);
const mbps = (kbps / 1024).toFixed(2);
resolve({bps, kbps, mbps});
});
});
req.write(data);
req.end();
}).catch(error => {
throw new Error(error);
});
}
generateTestData(sizeInKmb) {
const iterations = sizeInKmb * 1024; //get byte count
let result = '';
for (var index = 0; index < iterations; index++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
}
module.exports = NetworkSpeedCheck;