-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·149 lines (136 loc) · 3.45 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
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
#!/usr/bin/env node
'use strict'
const { extend, keys, map, reduce, sortBy, uniq, values } = require('lodash')
const axios = require('axios')
const Listr = require('listr')
const table = require('tty-table')
const ASIAN_SERVERS = {
'sg': ['Singapore', 'sgp'],
'jp': ['Tokyo, Japan', 'hnd-jp']
}
const EUROPEAN_SERVERS = {
'de': ['Frankfurt, DE', 'fra-de'],
'fr': ['Paris, FR', 'par-fr'],
'nl': ['Amsterdam, NL', 'ams-nl'],
'uk': ['London, UK', 'lon-gb'],
'au': ['Sydney, Australia', 'syd-au']
}
const AMERICAN_SERVERS = {
'ny': ['New York (New Jersey)', 'nj-us'],
'il': ['Chicago, IL', 'il-us'],
'fl': ['Miami, FL', 'fl-us'],
'wa': ['Seattle, WA', 'wa-us'],
'tx': ['Dallas, TX', 'tx-us'],
'sf': ['San Francisco, CA', 'sjo-ca-us'],
'la': ['Los Angeles, CA', 'lax-ca-us']
}
const ALL_SERVERS = extend({},
ASIAN_SERVERS,
EUROPEAN_SERVERS,
AMERICAN_SERVERS,
{
'as': null,
'eu': null,
'us': null
})
const { argv } = require('yargs')
.usage('Usage: $0 [options]')
.option('h', {
alias: 'host',
description: 'Hostname to ping',
default: 'google.com',
type: 'string'
})
.option('l', {
alias: 'locations',
description: 'Locations to test',
default: keys(ALL_SERVERS),
defaultDescription: 'All locations',
type: 'array',
choices: keys(ALL_SERVERS)
})
.help()
const selectedLocations = uniq(reduce(argv.locations, (accum, l) => {
switch (l) {
case 'as':
return accum.concat(values(ASIAN_SERVERS))
case 'eu':
return accum.concat(values(EUROPEAN_SERVERS))
case 'us':
return accum.concat(values(AMERICAN_SERVERS))
default:
return accum.concat([ALL_SERVERS[l]])
}
}, []))
function createPingTask([title, subdomain]) {
return {
title,
task: (ctx) => axios
.get(`http://${subdomain}-ping.vultr.com/ajax.php?cmd=ping&host=${argv.host}`)
.then(({ data }) => {
const pingSummaryParserRegex = /rtt min\/avg\/max\/mdev = ([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+) ms/
const [, min, avg, max, mdev] = map(data.match(pingSummaryParserRegex), parseFloat)
if (!ctx.results) {
ctx.results = []
}
ctx.results.push({ title, min, avg, max, mdev })
})
}
}
function printResults(results) {
const header = [
{
value: 'rank',
headerColor: 'cyan',
color: 'white',
align: 'left',
paddingLeft: 1,
width: 8
},
{
value: 'location',
headerColor: 'cyan',
color: 'white',
align: 'left',
paddingLeft: 1,
width: 25
},
{
value: 'min',
headerColor: 'green',
color: 'white',
align: 'right',
paddingLeft: 1,
width: 10
},
{
value: 'avg',
headerColor: 'yellow',
color: 'white',
align: 'right',
paddingLeft: 1,
width: 10
},
{
value: 'max',
headerColor: 'red',
color: 'white',
align: 'right',
paddingLeft: 1,
width: 10
},
{
value: 'mdev',
headerColor: 'cyan',
color: 'white',
align: 'right',
paddingLeft: 1,
width: 10
}
]
const rows = map(sortBy(results, ({ avg }) => avg), (r, i) => [i + 1, ...values(r)])
console.log(table(header, rows).render())
}
new Listr(map(selectedLocations, createPingTask), { concurrent: true })
.run()
.then((ctx) => printResults(ctx.results))