I want to write a simple code to test nodejs Concurrency when it receives many requests.
And I use loadtest module for simulate send many requests to server with this command :
loadtest -c 10 --rps 200 http://localhost:3000/
my simple code that write in 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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.