[英]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.