Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function doClientBWCheck() {
- nc.call("onClientBWCheck", res, null);
- trace("testing CtoS performance...");
- }
- var nc:NetConnection = new NetConnection();
- nc.onStatus = function(info) {
- trace (info.code);
- if (info.code == "NetConnection.Connect.Success") {
- doClientBWCheck();
- }
- }
- nc.connect("rtmp://localhost/bwcheck");
- var payload = new Array();
- for (var i=0; i<1200; i++){
- payload[i] = Math.random(); //16K approx
- }
- var res = new Object();
- res.latency = 0;
- res.cumLatency = 1;
- res.bwTime = 0;
- res.count = 0;
- res.sent = 0;
- res.kbitUp = 0;
- res.deltaUp = 0;
- res.deltaTime = 0;
- //res.client = p_client;
- //var stats = p_client.getStats();
- res.pakSent = new Array();
- res.pakRecv = new Array();
- res.beginningValues = {};
- res.onResult = function(p_res) {
- trace ("ClientBWResult: ");
- var now = (new Date()).getTime()/1;
- if(this.sent == 0) {
- this.beginningValues = p_res;
- this.beginningValues.time = now;
- this.pakSent[res.sent++] = now;
- nc.call("onClientBWCheck", this, now);
- } else {
- this.pakRecv[this.count] = now;
- trace( "Packet interval = " + (this.pakRecv[this.count] - this.pakSent[this.count])*1 );
- this.count++;
- var timePassed = (now - this.beginningValues.time);
- if (this.count == 1) {
- this.latency = Math.min(timePassed, 800);
- this.latency = Math.max(this.latency, 10);
- this.overhead = p_res.cOutBytes - this.beginningValues.cOutBytes;
- trace("overhead: "+this.overhead);
- this.pakSent[res.sent++] = now;
- nc.call("onClientBWCheck", res, now, payload);
- }
- trace("count: "+this.count+ " sent: "+this.sent+" timePassed: "+timePassed+" latency: "+this.latency);
- // If we have a hi-speed network with low latency send more to determine
- // better bandwidth numbers, send no more than 6 packets
- if ( (this.count >= 1) && (timePassed<1000))
- {
- this.pakSent[res.sent++] = now;
- this.cumLatency++;
- nc.call("onClientBWCheck", res, now, payload);
- } else if ( this.sent == this.count ) {
- // See if we need to normalize latency
- if ( this.latency >= 100 )
- { // make sure we detect sattelite and modem correctly
- if ( this.pakRecv[1] - this.pakRecv[0] > 1000 )
- {
- this.latency = 100;
- }
- }
- delete payload;
- // Got back responses for all the packets compute the bandwidth.
- var stats = p_res;
- deltaUp = (stats.cOutBytes - this.beginningValues.cOutBytes)*8/1000;
- deltaTime = ((now - this.beginningValues.time) - (this.latency * this.cumLatency) )/1000;
- if ( deltaTime <= 0 )
- deltaTime = (now - this.beginningValues.time)/1000;
- kbitUp = Math.round(deltaUp/deltaTime);
- trace("onBWDone: kbitUp = " + kbitUp + ", deltaUp= " + deltaUp + ", deltaTime = " + deltaTime + ", latency = " + this.latency + " KBytes " + (stats.cOutBytes - this.beginningValues.cOutBytes)/1024) ;
- }
- }
- }
Add Comment
Please, Sign In to add comment