Skip to content

Instantly share code, notes, and snippets.

@yyx990803
Last active March 23, 2018 08:12
Show Gist options
  • Save yyx990803/196b828f370faa4b6edb6ccb443cb64f to your computer and use it in GitHub Desktop.
Save yyx990803/196b828f370faa4b6edb6ccb443cb64f to your computer and use it in GitHub Desktop.
const { URL } = require('url')
const https = require('https')
const pingCount = 5
const paths = {
npm: `https://registry.npmjs.org/vue/latest`,
yarn: `https://registry.yarnpkg.com/vue/latest`,
tb: `https://registry.npm.taobao.org/vue/latest`
}
const ping = url => {
return new Promise((resolve, reject) => {
const start = Date.now()
const { hostname, pathname } = new URL(url)
const req = https.request({
hostname,
path: pathname
}, () => {
process.stdout.write('.')
resolve(Date.now() - start)
})
req.on('error', reject)
req.end()
})
}
const pingX = (registry, times) => {
return ping(paths[registry]).then(latency => {
return times > 1
? pingX(registry, times - 1).then(results => [latency, ...results])
: [latency]
})
}
const pings = Object.keys(paths).map(registry => {
return pingX(registry, pingCount).then(results => ({
registry,
results
}))
})
Promise.all(pings)
.then(results => {
console.log()
console.log(results)
})
.catch(err => {
console.log(err)
})
@jasonwwl
Copy link

地点:陕西,西安,电信

[ 
  { registry: 'npm', results: [ 2477, 1784, 381, 378, 345 ] },
  { registry: 'yarn', results: [ 1232, 514, 526, 745, 577 ] },
  { registry: 'tb', results: [ 1675, 1817, 1733, 2056, 1844 ] } 
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment