Repository navigation
Expand file tree
/
Copy pathping.js
More file actions
151 lines (128 loc) · 3.56 KB
/
Copy pathping.js
File metadata and controls
151 lines (128 loc) · 3.56 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* global BigInt */
/* USAGE
* node ping [HOSTNAME] [PORT]
*
* Both HOSTNAME and PORT are optional arguments,
* a random list of connection test hosts will be
* chosen to connect on port 80 (HTTP)
*/
const net = require('net')
const defaultAddresses = [
'connectivitycheck.gstatic.com',
'www.msftconnecttest.com',
'cloudflare.com',
'google.com',
'microsoft.com',
'1.1.1.1'
]
const maxStore = Number.MAX_VALUE
const options = {
address: process.argv[2] != null
// true: Uses 2nd argument for `node ping.js ABC.XYZ`
? String(process.argv[2]).trim().toLowerCase()
// false: Selects random address from list of defaults
: (() => defaultAddresses[Math.random() * defaultAddresses.length])(),
port: process.argv[3] != null
? Number(String(process.argv[3]).trim())
: 80,
timeout: 1000
}
function throttle (ms, fn) {
var lastCallTime
return function () {
var now = Date.now()
if (!lastCallTime || now - lastCallTime > ms) {
lastCallTime = now
fn.apply(this, arguments)
}
}
}
let pings = BigInt(0)
let pongs = BigInt(0)
const GetPing = () => new Promise(resolve => {
const socket = new net.Socket()
const start = process.hrtime.bigint()
socket.connect(options.port, options.address)
socket.on('connect', () => {
const end = process.hrtime.bigint()
socket.destroy()
pings++; pongs++
resolve(end - start)
})
socket.on('error', e => {
socket.destroy()
pings++
resolve(null)
})
socket.setTimeout(options.timeout, () => {
socket.destroy()
pings++
resolve(null)
})
})
const jitterValues = []
const pingValues = []
const addToJitterValues = value => {
if (jitterValues.length > maxStore) jitterValues.shift()
if (value != null) jitterValues.push(value)
}
const addToPingValues = (value) => {
if (pingValues.length > maxStore) pingValues.shift()
if (value != null) pingValues.push(value)
}
const countAverage = values => {
let sum = 0
let counted = 0
for (let i = 0; i < values.length; i++) {
if (values[i] == null) continue
sum += Number(nanoToMs(values[i]))
counted += 1
}
const avg = sum / Math.max(counted, 1)
return avg
}
let prevMessage = process.hrtime.bigint()
let thisMessage = process.hrtime.bigint()
const timeSinceLast = () => {
const newValue = thisMessage - prevMessage
prevMessage = thisMessage
thisMessage = process.hrtime.bigint()
return newValue
}
const nanoToMs = bigInt => {
// eslint-disable-next-line
if (typeof bigInt !== 'bigint') return String(bigInt)
const ms = bigInt / BigInt(1000000)
return String(ms)
}
const rd = n => Math.round(Number(nanoToMs(n)))
const PrintResults = throttle(200, function () {
const averageJitter = rd(countAverage(jitterValues))
const currentJitter = rd(jitterValues[jitterValues.length - 1])
const averagePing = rd(countAverage(pingValues))
const currentPing = rd(pingValues[pingValues.length - 1])
const packetLoss = Math.abs(((
Number(
String(pongs)
) / Math.max(1, Number(
String(pings)
))
) * 100) - 100).toFixed(2)
console.log('\x1Bc')
console.log(` Ping-Pong!\n`)
console.log(` \t\t[Realtime]\t[Average]`)
console.log(` Ping: \t${currentPing}ms\t\t${averagePing}ms`)
console.log(` Jitter:\t${currentJitter}ms\t\t${averageJitter}ms`)
console.log(`\n Pings: ${pings}\n Pongs: ${pongs}\n`)
console.log(` Packet Loss: ${packetLoss}%\n\n`)
})
const Runner = () => {
GetPing().then(time => {
const difference = timeSinceLast()
addToJitterValues(difference)
addToPingValues(time)
PrintResults()
Runner()
})
}
Runner()