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.
On 03/26/2015 10:48 AM, Viktor Sidorenko wrote:
Hello all.
I'm create simple client for burst check web server:
|
varhttp =require("http");
varreq_total =10,
req_per_sec =30,
i =0;
varintervalID =setInterval(functiontimerik(){
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?
--
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 [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/fbbb0d28-b52a-4261-b1de-49ea3774f7ff%40googlegroups.com
<https://groups.google.com/d/msgid/nodejs/fbbb0d28-b52a-4261-b1de-49ea3774f7ff%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/55145F2A.7030509%40gmail.com.
For more options, visit https://groups.google.com/d/optout.