motor.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <!--
  2. var start_time = 0;
  3. function Timer()
  4. {
  5. this.startTime = 0;
  6. this.endTime = 0;
  7. Timer.prototype.Reset = function()
  8. {
  9. this.startTime = new Date().getTime();
  10. this.endTime = 0;
  11. }
  12. Timer.prototype.Stop = function()
  13. {
  14. this.endTime = new Date().getTime();
  15. return this.endTime - this.startTime;
  16. }
  17. Timer.prototype.Value = function()
  18. {
  19. var end = this.endTime;
  20. if( end == 0 && this.startTime != 0 )
  21. end = new Date().getTime();
  22. return end - this.startTime;
  23. }
  24. this.Reset();
  25. }
  26. Number.prototype.timeString = function()
  27. {
  28. if( this >= 1000 )
  29. return roundNumber( this / 1000, 2 ).toString() + " s";
  30. return this.toString() + " ms";
  31. }
  32. Number.prototype.speedString = function()
  33. {
  34. //if( this >= 1000 )
  35. // return roundNumber( this / 1000, 1 ).toString() + " Mbit/s";
  36. return this.toString() + " kbit/s";
  37. }
  38. function WebClientRequest( method, url )
  39. {
  40. this.timer = new Timer;
  41. this.url = url;
  42. this.data = "";
  43. this.method = method;
  44. this.GetResponse = function( readData )
  45. {
  46. var response = new WebClientResponse( this, readData );
  47. this.timer.Reset();
  48. $.ajax( {
  49. url: this.url,
  50. cache: false,
  51. async: false,
  52. processData: false,
  53. data: this.data,
  54. type: this.method,
  55. dataType: "text",
  56. contentType: "text/plain; charset=x-user-defined",
  57. complete: response.InternalRequestCompleted(),
  58. error: function( e )
  59. {
  60. if( e.status == 200 )
  61. return; // We do get here in IE7 on Vista, even though the request performed well..
  62. if( e.status == 405 )
  63. alert( "Su servidor no permite el uso de HTTP POST hacia este archivo HTML.\nPor favor, revise su configuración." );
  64. }
  65. } );
  66. return response;
  67. };
  68. }
  69. function WebClientResponse( request, readData )
  70. {
  71. this.request = request;
  72. this.readData = readData;
  73. this.data = "";
  74. this.status = "";
  75. this.statusText = "";
  76. this.contentLength = 0;
  77. this.bytesTransferred = 0;
  78. this.GetTime = function() { return this.request.timer.Value(); }
  79. this.GetSpeed = function() { return roundNumber( ( this.bytesTransferred * 0.008 ) / ( this.GetTime() / 1000 ), 0 ); }
  80. this.InternalRequestCompleted = function()
  81. {
  82. var response = this;
  83. return function( xhr )
  84. {
  85. response.status = xhr.status;
  86. response.statusText = xhr.statusText;
  87. var contentLengthHeader = xhr.getResponseHeader( "Content-Length" );
  88. if( null != contentLengthHeader && contentLengthHeader.length > 0 )
  89. response.contentLength = parseInt( contentLengthHeader );
  90. else if( null != xhr.responseText )
  91. response.contentLength = xhr.responseText.length;
  92. else
  93. response.contentLength = 0;
  94. if( response.request.method == "POST" )
  95. response.contentLength = request.data.length;
  96. if( response.request.method != "HEAD" )
  97. response.bytesTransferred = response.contentLength;
  98. if( response.readData )
  99. response.data = xhr.responseText;
  100. response.request.timer.Stop();
  101. }
  102. };
  103. }
  104. function WebClient()
  105. {
  106. WebClient.prototype.Download = function( url, readData )
  107. {
  108. return new WebClientRequest( "GET", url ).GetResponse( readData );
  109. };
  110. WebClient.prototype.Upload = function( url, data )
  111. {
  112. if( url.indexOf( "?" ) == -1 )
  113. qs = '?sid=' + Math.random();
  114. else
  115. qs = '&sid=' + Math.random();
  116. var request = new WebClientRequest( "POST", url + qs );
  117. request.data = data;
  118. return request.GetResponse();
  119. };
  120. WebClient.prototype.Ping = function( url )
  121. {
  122. return new WebClientRequest( "HEAD", url ).GetResponse();
  123. };
  124. }
  125. Test.SmallFilePath = "data_100k.txt";
  126. Test.LargeFilePath = "data_1600k.txt";
  127. Test.Download = "DOWNLOAD";
  128. Test.Upload = "UPLOAD";
  129. function Test()
  130. {
  131. this.timer = new Timer;
  132. this.webClient = new WebClient;
  133. this.progressCallback = function() {};
  134. this.completionCallback = function() {};
  135. this.progressValue = 0;
  136. this.maxProgressValue = 0;
  137. this.path = "";
  138. this.last = null;
  139. this.ping = null;
  140. Test.prototype.Start = function()
  141. {
  142. var test = this;
  143. setTimeout( function()
  144. {
  145. test.timer.Reset();
  146. test.Run( function()
  147. {
  148. test.progressValue++;
  149. if( null != test.progressCallback )
  150. {
  151. test.progressCallback();
  152. }
  153. },
  154. function()
  155. {
  156. test.timer.Stop();
  157. if( null != test.completionCallback )
  158. test.completionCallback();
  159. } );
  160. }, 10 );
  161. }
  162. Test.prototype.Run = function( updateProgress, completed ) {}
  163. Test.prototype.MakeRequest = function()
  164. {
  165. if( this.path == null || this.path.length == 0 )
  166. return;
  167. if( this.method == Test.Upload )
  168. {
  169. if( this.payload == null )
  170. {
  171. var dl = this.webClient.Ping( this.path );
  172. this.payload = this.GenerateData( dl.contentLength );
  173. }
  174. this.last = this.webClient.Upload( location.search, this.payload );
  175. }
  176. else if( this.method == Test.Download )
  177. {
  178. this.last = this.webClient.Download( this.path );
  179. }
  180. this.ping = this.webClient.Ping( "?" );
  181. }
  182. Test.prototype.GenerateData = function( length )
  183. {
  184. var data = "";
  185. while( data.length < length )
  186. {
  187. data = data + Math.random();
  188. }
  189. return data;
  190. }
  191. Test.prototype.OnProgress = function( progressCallback )
  192. {
  193. this.progressCallback = progressCallback;
  194. return this;
  195. }
  196. Test.prototype.OnComplete = function( completionCallback )
  197. {
  198. this.completionCallback = completionCallback;
  199. return this;
  200. }
  201. Test.prototype.Path = function( path )
  202. {
  203. this.path = path;
  204. return this;
  205. }
  206. Test.prototype.Method = function( method )
  207. {
  208. this.method = method;
  209. return this;
  210. }
  211. }
  212. AverageTest.prototype = new Test;
  213. function AverageTest()
  214. {
  215. this.iterations = 0;
  216. this.iteration = 0;
  217. this.totalBytes = 0;
  218. this.totalSpeed = 0;
  219. this.totalTime = 0;
  220. this.totalPing = 0;
  221. this.iterationCallback = function() {};
  222. Test.apply( this, arguments );
  223. AverageTest.prototype.Run = function( incrementProgress, completed )
  224. {
  225. this.maxProgressValue = this.iterations;
  226. if( !this.IsCompleted() )
  227. {
  228. this.Iterate();
  229. if( this.last == null )
  230. return;
  231. this.iteration++;
  232. incrementProgress();
  233. var test = this;
  234. setTimeout( function() { test.Run( incrementProgress, completed ); }, 1 );
  235. }
  236. else
  237. completed();
  238. }
  239. AverageTest.prototype.IsCompleted = function()
  240. {
  241. return this.iterations == this.iteration;
  242. }
  243. AverageTest.prototype.GetAverageTime = function()
  244. {
  245. return roundNumber( this.totalTime / this.iteration, 2 );
  246. }
  247. AverageTest.prototype.GetAveragePing = function()
  248. {
  249. return roundNumber( this.totalPing / this.iteration, 2 );
  250. }
  251. AverageTest.prototype.GetAverageSpeed = function()
  252. {
  253. return roundNumber( this.totalSpeed / this.iteration, 1 );
  254. }
  255. AverageTest.prototype.GetTotalTime = function()
  256. {
  257. return this.totalTime;
  258. }
  259. AverageTest.prototype.Iterate = function()
  260. {
  261. this.MakeRequest();
  262. if( this.last != null )
  263. {
  264. this.totalBytes += this.last.contentLength;
  265. this.totalSpeed += this.last.GetSpeed();
  266. this.totalTime += this.last.GetTime();
  267. this.totalPing += this.ping.GetTime();
  268. }
  269. }
  270. AverageTest.prototype.Iterations = function( number )
  271. {
  272. this.iterations = number;
  273. return this;
  274. }
  275. AverageTest.prototype.AfterIteration = function( iterationCallback )
  276. {
  277. this.iterationCallback = iterationCallback;
  278. return this;
  279. }
  280. }
  281. function roundNumber(num, dec)
  282. {
  283. var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  284. return result;
  285. }
  286. function EndTest( upload, download )
  287. {
  288. var totalTime = upload.GetTotalTime() + download.GetTotalTime();
  289. var avgTime = roundNumber( ( upload.GetAveragePing() + download.GetAveragePing() ) / 2, 2 );
  290. var tbl = $( "#tblSample" );
  291. var rows = $( "tr", tbl );
  292. row = $( "<tr/>" ).appendTo( tbl ).css( "background-color", "#cfcfcf" );
  293. $( row ).append( "<td colspan=8>Total time " + totalTime.timeString() + ", average response time " + avgTime.timeString() + "</td>" );
  294. $( "#startTestButton" ).removeAttr( "disabled" ).val( Strings.StartTest );
  295. $( "#testInProgress" ).hide( "normal" );
  296. var avgUlSpeed = upload.GetAverageSpeed();
  297. var avgDlSpeed = download.GetAverageSpeed();
  298. $('#dwn_speed_input').val(avgDlSpeed);
  299. $('#up_speed_input').val(avgUlSpeed);
  300. $('#submit_url_button').removeClass('hidden');
  301. SetInfo( avgUlSpeed.speedString(), avgTime.timeString(), upload.method );
  302. SetInfo( avgDlSpeed.speedString(), avgTime.timeString(), download.method );
  303. var title = "";
  304. var message = "";
  305. if( avgUlSpeed < Settings.MinimumUlSpeed || isNaN( avgUlSpeed ) ||
  306. avgDlSpeed < Settings.MinimumDlSpeed || isNaN( avgDlSpeed ) ||
  307. avgTime > Settings.MaximumTime || isNaN( avgTime ) )
  308. {
  309. title = Strings.TestFailed.Title;
  310. message = Strings.TestFailed.Message;
  311. }
  312. else
  313. {
  314. title = Strings.TestPassed.Title;
  315. message = Strings.TestPassed.Message;
  316. }
  317. var res = $( "#resultMessage" );
  318. res.children( ".result-title" ).text( title );
  319. res.children( ".result-message" ).text( message );
  320. res.show( "fast" );
  321. }
  322. function StartTest( count )
  323. {
  324. $( "#resultMessage" ).hide( "fast" );
  325. $( "#startTestButton" ).attr( "disabled", "disabled" );
  326. UpdateProgress( 0 );
  327. $( "#testInProgress" ).show( "normal", function() { TestStarted( count ); } );
  328. }
  329. function TestStarted( count )
  330. {
  331. if( "" != $.query.get("details" ))
  332. Settings.Debug = true;
  333. if( Settings.Debug )
  334. $( "#technicalDetails" ).show( "fast" );
  335. var tbl = $( "#tblSample" );
  336. $( "tr", tbl ).remove();
  337. var upload = new AverageTest()
  338. .Iterations( count / 2 )
  339. .Path( Test.SmallFilePath )
  340. .Method( Test.Upload )
  341. .OnProgress( function()
  342. {
  343. var progressPercentage = this.progressValue / ( this.maxProgressValue / 100 ) / 2;
  344. UpdateProgress( progressPercentage );
  345. AppendDetailsRow( this );
  346. } );
  347. var download = new AverageTest()
  348. .Iterations( count / 2 )
  349. .Path( Test.SmallFilePath )
  350. .Method( Test.Download )
  351. .OnProgress( function()
  352. {
  353. var progressPercentage = this.progressValue / (this.maxProgressValue / 100 ) / 2 + 50;
  354. UpdateProgress( progressPercentage );
  355. AppendDetailsRow( this );
  356. } );
  357. download.OnComplete( function() { EndTest( upload, download ); } );
  358. upload.OnComplete( function() { download.Start(); } ).Start();
  359. }
  360. function UpdateProgress( value )
  361. {
  362. $( "#progressbar" ).children( ".ui-progressbar-value" ).css( "width", value + "%" );
  363. }
  364. function AppendDetailsRow( test )
  365. {
  366. var tbl = $( "#tblSample" );
  367. var rows = $( "tr", tbl );
  368. row = $( "<tr/>" ).appendTo( tbl ).css( "background-color", ( test.iteration % 2 ) ? "#eeeeee" : "#ffffff" );
  369. var speed = test.last.GetSpeed().speedString();
  370. var time = test.last.GetTime().timeString();
  371. var addCol = function( text ) { $("<td/>" ).append( text ).appendTo( row ); };
  372. addCol( test.method + " " + test.iteration )
  373. addCol( time );
  374. addCol( test.ping.GetTime().timeString() );
  375. addCol( window.location.host );
  376. addCol( test.last.request.url );
  377. addCol( test.last.contentLength );
  378. addCol( test.last.status );
  379. addCol( speed );
  380. SetInfo( speed, test.ping.GetTime(), test.method );
  381. }
  382. var ulSpeed = $.query.get( "ul" );
  383. if( "" != ulSpeed )
  384. {
  385. Settings.MinimumUlSpeed = parseInt( ulSpeed );
  386. }
  387. var dlSpeed = $.query.get( "dl" );
  388. if( "" != dlSpeed )
  389. {
  390. Settings.MinimumDlSpeed = parseInt( dlSpeed );
  391. }
  392. var qsTime = $.query.get( "time" );
  393. if( "" != qsTime )
  394. {
  395. Settings.MaximumTime = parseInt( qsTime );
  396. }
  397. function SetInfo( speedtext, timetext, testMethod )
  398. {
  399. $( ( testMethod == Test.Download ) ? "#currentdlspeed" : "#currentulspeed" ).text( speedtext );
  400. $( "#currenttime" ).text( timetext );
  401. $( "#delay_input" ).val(timetext);
  402. }
  403. var count = $.query.get( "count" );
  404. if( count == "" || count > 100 )
  405. count = Settings.RequestCount;
  406. $( function()
  407. {
  408. $( "#startTestButton" ).removeAttr( "disabled" ).val( Strings.StartTest ).click( function() { StartTest( count ) } );
  409. $( "#requiredulspeed" ).text( Settings.MinimumUlSpeed.speedString() );
  410. $( "#requireddlspeed" ).text( Settings.MinimumDlSpeed.speedString() );
  411. $( "#requiredtime" ).text( Settings.MaximumTime.timeString() );
  412. $( "#progressbar" ).progressbar();
  413. document.title = Strings.Title;
  414. $( "#title" ).text( Strings.Title );
  415. $( "#subtitle" ).text( Strings.SubTitle );
  416. $( "#testInProgressLabel" ).text( Strings.TestInProgress );
  417. $( "#logo" ).attr( "src", Settings.Logo );
  418. });
  419. -->