Guest User

Untitled

a guest
Jun 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. function doClientBWCheck() {
  2. nc.call("onClientBWCheck", res, null);
  3. trace("testing CtoS performance...");
  4.  
  5. }
  6.  
  7. var nc:NetConnection = new NetConnection();
  8. nc.onStatus = function(info) {
  9. trace (info.code);
  10. if (info.code == "NetConnection.Connect.Success") {
  11. doClientBWCheck();
  12. }
  13. }
  14. nc.connect("rtmp://localhost/bwcheck");
  15.  
  16. var payload = new Array();
  17. for (var i=0; i<1200; i++){
  18. payload[i] = Math.random(); //16K approx
  19. }
  20.  
  21. var res = new Object();
  22. res.latency = 0;
  23. res.cumLatency = 1;
  24. res.bwTime = 0;
  25. res.count = 0;
  26. res.sent = 0;
  27. res.kbitUp = 0;
  28. res.deltaUp = 0;
  29. res.deltaTime = 0;
  30. //res.client = p_client;
  31. //var stats = p_client.getStats();
  32. res.pakSent = new Array();
  33. res.pakRecv = new Array();
  34. res.beginningValues = {};
  35.  
  36. res.onResult = function(p_res) {
  37. trace ("ClientBWResult: ");
  38. var now = (new Date()).getTime()/1;
  39. if(this.sent == 0) {
  40. this.beginningValues = p_res;
  41. this.beginningValues.time = now;
  42. this.pakSent[res.sent++] = now;
  43. nc.call("onClientBWCheck", this, now);
  44. } else {
  45. this.pakRecv[this.count] = now;
  46. trace( "Packet interval = " + (this.pakRecv[this.count] - this.pakSent[this.count])*1 );
  47. this.count++;
  48. var timePassed = (now - this.beginningValues.time);
  49.  
  50. if (this.count == 1) {
  51. this.latency = Math.min(timePassed, 800);
  52. this.latency = Math.max(this.latency, 10);
  53. this.overhead = p_res.cOutBytes - this.beginningValues.cOutBytes;
  54. trace("overhead: "+this.overhead);
  55. this.pakSent[res.sent++] = now;
  56. nc.call("onClientBWCheck", res, now, payload);
  57. }
  58. trace("count: "+this.count+ " sent: "+this.sent+" timePassed: "+timePassed+" latency: "+this.latency);
  59.  
  60. // If we have a hi-speed network with low latency send more to determine
  61. // better bandwidth numbers, send no more than 6 packets
  62. if ( (this.count >= 1) && (timePassed<1000))
  63. {
  64. this.pakSent[res.sent++] = now;
  65. this.cumLatency++;
  66. nc.call("onClientBWCheck", res, now, payload);
  67. } else if ( this.sent == this.count ) {
  68. // See if we need to normalize latency
  69. if ( this.latency >= 100 )
  70. { // make sure we detect sattelite and modem correctly
  71. if ( this.pakRecv[1] - this.pakRecv[0] > 1000 )
  72. {
  73. this.latency = 100;
  74. }
  75. }
  76. delete payload;
  77. // Got back responses for all the packets compute the bandwidth.
  78. var stats = p_res;
  79. deltaUp = (stats.cOutBytes - this.beginningValues.cOutBytes)*8/1000;
  80. deltaTime = ((now - this.beginningValues.time) - (this.latency * this.cumLatency) )/1000;
  81. if ( deltaTime <= 0 )
  82. deltaTime = (now - this.beginningValues.time)/1000;
  83.  
  84. kbitUp = Math.round(deltaUp/deltaTime);
  85.  
  86. trace("onBWDone: kbitUp = " + kbitUp + ", deltaUp= " + deltaUp + ", deltaTime = " + deltaTime + ", latency = " + this.latency + " KBytes " + (stats.cOutBytes - this.beginningValues.cOutBytes)/1024) ;
  87. }
  88. }
  89. }
Add Comment
Please, Sign In to add comment