Last active
March 23, 2018 08:12
-
-
Save yyx990803/196b828f370faa4b6edb6ccb443cb64f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
node ping.js
...............
[ { registry: 'npm', results: [ 989, 271, 278, 249, 249 ] },
{ registry: 'yarn', results: [ 996, 124, 117, 111, 117 ] },
{ registry: 'tb', results: [ 40, 35, 26, 27, 26 ] } ]
浙江杭州
[ { registry: 'npm', results: [ 1574, 569, 631, 597, 592 ] },
{ registry: 'yarn', results: [ 1250, 854, 891, 920, 1720 ] },
{ registry: 'tb', results: [ 170, 122, 118, 104, 108 ] } ]
广东中山 -- 中国电信
[ { registry: 'npm', results: [ 4901, 2117, 957, 1724, 5073 ] },
{ registry: 'yarn', results: [ 4688, 887, 878, 855, 1930 ] },
{ registry: 'tb', results: [ 96, 79, 63, 75, 62 ] } ]
上海 长城宽带 node v8.6.0
[ { registry: 'npm', results: [ 3153, 1498, 856, 821, 601 ] },
{ registry: 'yarn', results: [ 2623, 2866, 1907, 921, 750 ] },
{ registry: 'tb', results: [ 88, 46, 40, 41, 41 ] } ]
浙江杭州
四川成都
1、开代理
[ { registry: 'npm', results: [ 5488, 811, 1973, 1098, 1536 ] },
{ registry: 'yarn', results: [ 1683, 855, 779, 921, 1537 ] },
{ registry: 'tb', results: [ 207, 161, 165, 165, 155 ] } ]
2、未开代理
[ { registry: 'npm', results: [ 2933, 2873, 2150, 820, 2457 ] },
{ registry: 'yarn', results: [ 972, 968, 720, 879, 819 ] },
{ registry: 'tb', results: [ 213, 153, 176, 164, 279 ] } ]
Node Version: node v9.0.0
...............
[ { registry: 'npm', results: [ 2640, 671, 2733, 3893, 4287 ] },
{ registry: 'yarn', results: [ 1335, 1277, 695, 700, 933 ] },
{ registry: 'tb', results: [ 129, 99, 92, 92, 95 ] } ]
武汉电信
四川成都 电信
[ { registry: 'npm', results: [ 3705, 783, 5505, 726, 737 ] },
{ registry: 'yarn', results: [ 2145, 1829, 1964, 3055, 1865 ] },
{ registry: 'tb', results: [ 353, 256, 177, 254, 521 ] } ]
[
{ registry: 'npm', results: [ 1423, 231, 260, 226, 257 ] },
{ registry: 'yarn', results: [ 1923, 693, 527, 543, 1679 ] },
{ registry: 'tb', results: [ 1281, 162, 191, 185, 162 ] }
]
[
{ registry: 'npm', results: [ 348, 260, 237, 229, 246] },
{ registry: 'yarn', results: [ 690, 1821, 1513, 677, 591] },
{ registry: 'tb', results: [ 200, 123, 140, 121, 185] }
]
地点:广东,深圳 ,宝安 电信
{ registry: 'npm', results: [ 1888, 981, 925, 963, 940 ] },
{ registry: 'yarn', results: [ 1106, 742, 841, 826, 627 ] },
{ registry: 'tb', results: [ 161, 123, 116, 117, 123 ] }
地点:陕西,西安,电信
[
{ 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
[ { registry: 'npm', results: [ 1068, 1181, 613, 604, 619 ] },
{ registry: 'yarn', results: [ 999, 821, 856, 911, 581 ] },
{ registry: 'tb', results: [ 169, 106, 113, 118, 108 ] } ]
深圳宝安