Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每秒发送数千个http请求_Javascript_Node.js_Performance_Http_Tcp - Fatal编程技术网

Javascript 每秒发送数千个http请求

Javascript 每秒发送数千个http请求,javascript,node.js,performance,http,tcp,Javascript,Node.js,Performance,Http,Tcp,为了对服务器进行负载测试,我想发送很多http请求。每个请求都必须创建一个新的连接,并应等到收到响应后再关闭连接。 此外,对于每个生成新URL的请求,我不能将请求发送到同一URL 起初我认为使用http.request可能是最聪明的方法,因为它是一个本机模块。所以我试了一下: for(let i=0; i<100; i++) { setInterval(()=>{ if (reqCount >= maxConcurrentRequests)

为了对服务器进行负载测试,我想发送很多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多个请求。节点进程的RAM使用率似乎保持在50.000k和100.000k之间,CPU使用率约为1-3%


我尝试了10个、100个甚至1000个并发连接,但在100个之后,这并没有多大区别。

我无法添加注释。但我认为由于并发请求的限制。你可以在这个答案中读到更多


可能是异步问题吗?您发送了很多请求,但这是一个接一个的顺序方式,因此我认为发送600个请求是很困难的。您可能希望使用节点工作线程(其他编程语言中的线程等效)。通过这种方式,您可以并行发送多个请求。我认为您不需要setInterval,这可能会导致一些问题。只需尝试
for(让我…我有一些特定的工具可以实现这一点,比如apachebenchmark(ab),你考虑过使用它们吗?@LuisMuñoz不,我需要生成URL。我认为这不能用其他工具来完成。@HMR它绝对不能是一个for循环。@anlijudavid 600个请求应该一点都不成问题。正如我说的,我的带宽甚至远没有耗尽,我的CPU和RAM也没有耗尽。发送这些请求需要一个second会导致DOS。注意。不,它不是。首先,不再有默认限制,其次,正如你们所看到的,我已经将代理设置为false。
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);
}