大概是这样的,我有个需求,需要向某个api 不断的请求,而且数量不小,有点像压力测试吧
const fetch = require('node-fetch');
const funs = async () => {
const now = Date.now();
await fetch('https://www.baidu.com/');
console.log(i, `start:${now}`, `end:${Date.now()}`, `use:${Date.now() - now}`);
};
在我的机器上跑,发现 同时跑 10、100、1000得到的结果时间相差太多
for (let i = 0; i < 10; i += 1) {
funs(i);
}
// funs log的时间大概是 80-100ms 递增
for (let i = 0; i < 100; i += 1) {
funs(i);
}
// funs log的时间大概是 500-800ms 递增
for (let i = 0; i < 1000; i += 1) {
funs(i);
}
// funs log的时间大概是 1000-5000ms 递增
部分评论的尝试
//01,尝试设置maxSockets
https.globalAgent.maxSockets = 1000;
https.globalAgent.maxFreeSockets = 1000;
//02,用原生https尝试设置new Agent
const https = require('https');
const $get = (i) => {
const now = Date.now();
const agent = new https.Agent();
const options = {
hostname: 'www.baidu.com',
port: 443,
method: 'GET',
agent: false, // agent
};
https.get(options, () => {
console.log(i, `start:${now}`, `end:${Date.now()}`, `use:${Date.now() - now}`);
}).on('error', () => {
console.log(i, `start:${now}`, `end:${Date.now()}`, `use:${Date.now() - now}`);
});
};
for (let i = 0; i < 1000; i += 1) {
$get(i);
}
//结果还是没变化
不明白造成请求的时间变长的具体原因是啥?
瞎猜1 node有类似浏览器一样有请求数限制?
瞎猜2 TCP链接数限制?