[英]Send thousands of http requests per second
我想发送很多http请求,以便对服务器进行负载测试。 每个请求都必须创建一个新的连接,并且应该等待直到收到响应后再关闭连接。
另外,对于每个请求,都会生成一个新的URL,我无法将请求发送到相同的URL。
起初,我认为使用http.request可能是最聪明的方法,因为它是一个本机模块。 所以我尝试了这个:
for(let i=0; i<100; i++) {
setInterval(()=>{
if (reqCount >= maxConcurrentRequests)
return
reqCount++
const req = http.request({
hostname: config.hostname,
port: config.port,
path: getRandomPath(),
method: 'GET',
agent:false
},()=>{
reqCount--
showPerformance() // is used to calculate and display performance
})
req.on('error', (e) => {
failedCount++
})
req.end()
},0);
}
最初,它每秒发送大约600个请求,然后在大约10秒后开始下降,并在10秒左右的时间内下降到每秒5-20个请求。
然后,我决定再降低一个级别,并创建自己的http数据包,并使用带有net
模块的tcp套接字直接将其发送:
for(let i=0; i<100; i++) {
setInterval(()=>{
if (reqCount >= maxConcurrentRequests)
return
reqCount++
const socket = new net.Socket()
socket.connect(config.port, config.hostname, () => {
socket.write(`GET http://${config.hostname}:${config.port}/${getRandomPath()} HTTP/1.1\r\nhostname: ${config.hostname}\r\nconnection: close\r\n\r\n`)
})
socket.on("data", data => {
reqCount--
showPerformance() // is used to calculate and display performance
})
socket.on("error", err => {
failedCount++
})
},0);
}
但是结果几乎相同。
为什么会这样呢? 运行负载测试时,我的网络带宽甚至还没有耗尽,服务器应该可以轻松地每秒处理600个以上的请求。 节点进程的RAM使用率似乎保持在50.000k和100.000k之间,CPU使用率约为1-3%。
我尝试了10个,100个甚至1000个并发连接,但100次之后并没有太大的区别。
我无法添加评论。 但是我认为由于并发请求的限制。 您可以在此答案中阅读更多内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.