简体   繁体   English

如何测试Node.js并发请求

[英]How to test nodejs concurrent Request

I want to write a simple code to test nodejs Concurrency when it receives many requests. 我想编写一个简单的代码来在收到许多请求时测试nodejs并发性。

And I use loadtest module for simulate send many requests to server with this command : 我使用loadtest模块来模拟使用此命令向服务器发送许多请求:

loadtest -c 10 --rps 200 http://localhost:3000/

my simple code that write in server.js : 我写在server.js中的简单代码:

var http = require('http');
var server = http.createServer();
var cun=0;
var step=0;       

function handleRequest(req,res) {

console.log(step++);
        while(100000000>cun){
            cun++;
        }
        cun=0;
}

server.on('request', handleRequest);
server.listen(3000);

but requests is wait until while loop is down 但是请求一直等到while循环关闭

and know to solve this problem I should use callback function , but don't know How do write it. 并且知道要解决此问题,我应该使用回调函数,但不知道如何编写它。

var http = require('http');
var url = require("url");
var server = http.createServer();

function handleRequest(req, res) {
  res.writeHead(200, { 'content-type': 'text/plain'});

var params = url.parse(request.url, true).query;
var input = params.number;

/*
1- Use input param in a algorithm and process it
2- Insert data in to the db
3- select on db and return to the client
*/  

  res.end();
}
server.on('request', handleRequest);
server.listen(3000);

PROBLEM : All requests wait in a queue until last request process(process-insert-select) is down 问题:所有请求都在队列中等待,直到最后一个请求进程(process-insert-select)关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NodeJS如何处理对MySQL的并发请求 - NodeJS How To Handle Concurrent Request To MySQL 带有Restify的Node.js如何处理并发请求? - How Nodejs with Restify Handle Concurrent Request? Node.js是单线程的,但如何在1秒内处理相同的并发请求 - Nodejs is single threaded but how it can process same concurrent request in 1 sec 并发请求无法正确处理mongoDB和nodeJs - Concurrent request can not handle properly with mongoDB and nodeJs nodejs 服务器如何处理来自同一客户端和不同客户端的多个并发请求? - How does nodejs server handles multiple concurrent request from same client and different client? 如何模拟nodejs中的请求和响应来测试中间件/控制器? - How to mock request and response in nodejs to test middleware/controllers? 如何使用 NodeJS 的超级测试测试 GET 请求的路径参数? - How to test path parameters of a GET request with NodeJS's supertest? 可以模拟多个并发连接以测试Node.js应用 - Possible to simulate several concurrent connections to test a nodejs app Node.js如何处理更多并发请求? - How is nodejs able to handle more concurrent requests? 如何使用cheerio和nodejs并发下载文件? - How to concurrent download files using cheerio and nodejs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM