如何测试nodejs并发Request

时间:2015-08-19 12:20:22

标签: node.js concurrency

我想编写一个简单的代码来测试nodejs并发收到很多请求。

我使用 loadtest 模块模拟使用此命令向服务器发送许多请求:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form id='form-id'>
    <input value='1' name='test' type='radio' />radio1
    <input value='2' name='test' type='radio' />radio2
    <input value='3' name='test' type='radio' />radio3
    <input value='4' name='test' type='radio' />radio4
    <input value='5' name='test' type='radio' />radio5<br />
    <input value='6' name='test' type='radio' />radio6
    <input value='7' name='test' type='radio' />radio7
    <input value='8' name='test' type='radio' />radio8
    <input value='9' name='test' type='radio' />radio9
    <input value='10' name='test' type='radio' />radio10
    
</form>
    <div class='show-me' style='display:none'>Div 1</div>
    <div class='show-me' style='display:none'>Div 2</div>
    <div class='show-me' style='display:none'>Div 3</div>
    <div class='show-me' style='display:none'>Div 4</div>
    <div class='show-me' style='display:none'>Div 5</div>
    <div class='show-me' style='display:none'>Div 6</div>
    <div class='show-me' style='display:none'>Div 7</div>
    <div class='show-me' style='display:none'>Div 8</div>
    <div class='show-me' style='display:none'>Div 9</div>
    <div class='show-me' style='display:none'>Div 10</div>

我在server.js中编写的简单代码:

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

但请求是等到while循环停止时

并知道要解决这个问题我应该使用回调函数,但不知道如何写它。

1 个答案:

答案 0 :(得分:0)

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);

问题: 所有请求都在队列中等待,直到最后一个请求进程(process-insert-select)关闭