javascript - Timing Ajax Requests - negative values -
we have small javascript test designed test clients network before use our software. works making ajax calls our servers , gradually increasing number of calls made per second , timing how long takes response give idea of latency network load increases.
in order time requests attaching senttime
jqxhr object on jquerys beforesend
event calculating difference on complete. simulate load of multiple users creating list of times (just array of ints), setting ajax request fire using settimeout, so:
function configureajaxrequesttimings() { var time = 0; for(var = 0; < requestmax; i++) { ajaxrequesttimings.push(time += getrandomtime()); } that.totalrequesttime = time; } function getrandomtime() { return (math.random() * 4000) + 3000; // generate new time (between 3 secs , 7 secs) } function startrequests() { (var j = 0, thistime; thistime = ajaxrequesttimings[j++];) { doajaxrequest(thistime); } }; function doajaxrequest(timedelay) { settimeout(function () { $.ajax({ type: 'get', contenttype: 'application/json', url: url, timeout: 60000, beforesend: function (jqxhr) { jqxhr.senttime = new date().gettime(); }, complete: function (response) { var = new date().gettime(); var requesttime = - response.senttime; requesttimes.push(requesttime); } }); }, timedelay) }
however, see negative time pushed requesttimes
, cannot see how can happen (unless complete
called before beforesend
??). unable recreate issue on dev environment. there perhaps kind of clever inlining or similar performance tricks browsers may cause issue? (it has happened twice far both on chrome, last 1 on chrome 45)
note: aware method not give precise millisecond timings (or close) due single threaded nature of js, idea see general 'shape' of response curve under increasing load
Comments
Post a Comment