Analog jmeter on nodejs

41 views
Skip to first unread message

Viktor Sidorenko

unread,
Mar 26, 2015, 3:04:07 PM3/26/15
to nod...@googlegroups.com
Hello all.
I'm create simple client for burst check web server:
var http = require("http");

var req_total = 10,
   req_per_sec = 30,
   i = 0;

var intervalID = setInterval(function timerik()  {
   i++;
   console.log(i); //write to console  task number
   console.time(i + ' latency');

    if(i == req_total){
       clearInterval(intervalID); //end of Life
   }

    http.get("http://www.yandex.ru", function (response){
       console.log(i +" result = " + response.statusCode); //write to console task number and statusCode
       console.timeEnd(i +' latency'); //write to console operation timeout
   });

}, 1000/req_per_sec);

Result:
"C:\Program Files\nodejs\node.exe" run4.js
1
2
3
3 result = 200
3 latency: 24ms
4
4 result = 200
4 latency: 11ms
4 result = 200
4 latency: 43ms
5
6
6 result = 200
6 latency: 12ms
7
7 result = 200
7 latency: 8ms
8
9
10

Process finished with exit code 0

In this try i have two problems:
1) I lose few queries, In result you can see only 5 of 10 queries.
2) Few queries have not valid id. You can see them in result line 4.

How i can fix this problems?


Ruben Rodriguez (cha0s)

unread,
Mar 26, 2015, 4:03:22 PM3/26/15
to nod...@googlegroups.com
It's due to the way you're closing over the variable `i`. `i` is being incremented even if a request takes longer than the interval, so when the request comes back, `i` will be out of sync.

Here's a simple fix, you could approach it many other ways:


var http = require("http");

var req_total = 10,
   req_per_sec = 30,
   i = 0;

var intervalID = setInterval(function timerik()  {

   // Create a new local variable bound to this closure's invocation's scope.
   var j = ++i;

   if(j == req_total){
       clearInterval(intervalID); //end of Life
   }

   console.log(j); //write to console  task number
   console.time(j + ' latency');


   http.get("http://www.yandex.ru", function (response){
      console.log(j +" result = " + response.statusCode); //write to console task number and statusCode
      console.timeEnd(j +' latency'); //write to console operation timeout
   });

}, 1000/req_per_sec);

Notice all instances of `i` are replaced with a new local variable `j` that is bound to each invocation of the closure's scope.
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/fbbb0d28-b52a-4261-b1de-49ea3774f7ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Viktor Sidorenko

unread,
Mar 27, 2015, 1:01:53 PM3/27/15
to nod...@googlegroups.com
You all right! first problem fixed.
What do you think about second problem? loss results
Now when i run script i have result only few queries.
"C:\Program Files\nodejs\node.exe" run4.js
1
2
3
1 result = 200
1 latency: 111ms
4
2 result = 200
2 latency: 95ms
5
3 result = 200
3 latency: 99ms
6
4 result = 200
4 latency: 109ms
7
5 result = 200
5 latency: 105ms
8
9
10

Process finished with exit code 0
Reply all
Reply to author
Forward
0 new messages