forked from jeffbyrnes/internet-speed-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-speedtest.js
76 lines (68 loc) · 2.02 KB
/
run-speedtest.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
/* eslint-disable no-console */
const config = require('config');
const { exec } = require('child_process');
const dbInit = require('./db');
// Get the command to execute
const cmd = config.get('speedtest.commandString');
// Timing related constants
const minimumIntervalS = 1800;
const intervalS = Math.max(config.get('speedtest.intervalSec'), minimumIntervalS);
const intervalMS = intervalS * 1000;
const isDaemon = process.argv[2] === 'daemon';
function getDelay(interval) {
return Math.floor(interval * (Math.random() * 0.5 + 0.75));
}
function insertData(result) {
dbInit().then((dbs) => {
const byteToMbit = 0.000008;
const { timestamp } = result;
const ping = result.ping.latency;
const { jitter } = result.ping;
const download = result.download.bandwidth * byteToMbit;
const upload = result.upload.bandwidth * byteToMbit;
const speedtestResult = {
date: new Date(timestamp), ping, download, upload, jitter,
};
dbs.insertOne(speedtestResult, (err) => {
if (err) {
console.error(err);
}
if (!isDaemon) {
process.exit();
}
});
}).catch((err) => {
console.error('Failed to connect to mongo');
console.error(err);
process.exit(1);
});
}
function processOutput(error, stdout, stderr) {
if (error) {
console.error('Error executing Speedtest');
console.error(error);
}
if (stderr) {
console.error(stderr);
}
try {
const data = JSON.parse(stdout);
insertData(data);
} catch (err) {
console.error('Failed to connect to parse output');
console.error(err);
} finally {
if (isDaemon) {
// No matter if there is an error, re-schedule.
// eslint-disable-next-line no-use-before-define
const delay = getDelay(intervalMS);
console.log(`Sleeping for ${Math.floor(delay / 1000)} seconds before next run...`);
// eslint-disable-next-line no-use-before-define
setTimeout(executeSpeedtest, delay);
}
}
}
function executeSpeedtest() {
exec(cmd, processOutput);
}
executeSpeedtest();