Pythorian

Run Speedtest from command line

Oct 5th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.32 KB | None | 0 0
  1. <?php
  2. /*
  3.  *  Speedtest.net linux terminal client.
  4.  *  This is free and open source software by Alex based on a script from Janhouse
  5.  *  Script uses curl, executes ifconfig commands in shell and writes temporary files in temp_down folder. Make sure you have everything set up before using it.
  6.  */
  7. header("content-type: text/plain");
  8. /* * *              Configuration                   * * */
  9. $iface="eth0";
  10. $maxrounds=4;
  11. $downloads="temp_down/";
  12. $uploads="upload/";
  13. $datadir="data/";
  14. /* * *              Speedtest servers               * * */
  15. $do_server['seattle'] = "http://speedtest.rainierconnect.com";
  16. $do_server['east oregon'] = "http://speedtest.eotnet.net";
  17. $do_server['portland'] = "http://speed.opusnet.com";
  18. $do_server['estacada,or'] = "http://speed.rconnects.com/";
  19.  
  20. /* Variables */
  21. $randoms=rand(100000000000, 9999999999999);
  22. $time=time();
  23. $day=date("d-m-Y");
  24. $do_size[1]=500;
  25. $do_size[2]=1000;
  26. $do_size[3]=1500;
  27. $do_size[4]=2000;
  28. $do_size[5]=2500;
  29. $do_size[6]=3000;
  30. $do_size[7]=3500;
  31. $do_size[8]=4000;
  32.  
  33. /* * *              The rest                        * * */
  34. function latency($round){
  35.     global $server, $downloads, $do_server, $server, $iface, $randoms, $do_size,$globallatency,$maxrounds;
  36.  
  37.     $file=$downloads."latency.txt";
  38.     $fp = fopen ($file, 'w+');
  39.     $ch = curl_init($do_server[$server]."/speedtest/latency.txt?x=".$randoms);
  40.     curl_setopt($ch, CURLOPT_HEADER, true);
  41.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  42.     curl_setopt($ch, CURLOPT_TIMEOUT, 50);
  43.     curl_setopt($ch, CURLOPT_FILE, $fp);
  44.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  45.  
  46.     $starttime=microtime(true);
  47.     $response=curl_exec($ch);
  48.     $endtme=microtime(true);
  49.  
  50.     $duration=$endtme-$starttime;
  51.  
  52.     curl_close($ch);
  53.     fclose($fp);
  54.     unlink($file);
  55.  
  56.     print round($duration, 2)."sec.";
  57.  
  58.     $globallatency+=$duration;
  59.     if ($round > 1){
  60.         latency(--$round);
  61.     } else {
  62.         print "\tAverage:".round($globallatency/$maxrounds, 2)."sec.\n";
  63.     }
  64. }
  65.  
  66. function download($size,$round){
  67.     global $server, $downloads, $do_server, $server, $iface, $randoms, $do_size,$globaldownloadspeed,$maxrounds;
  68.  
  69.     $file=$downloads."fails_".$size.".jpg";
  70.  
  71.     print ".";
  72.     $fp = fopen ($file, 'w+');
  73.     $ln = $do_server[$server]."/speedtest/random".$do_size[$size]."x".$do_size[$size].".jpg?x=".$randoms."-".$size;
  74.     $ch = curl_init();
  75.     curl_setopt($ch, CURLOPT_URL,$ln);
  76.     curl_setopt($ch, CURLOPT_HEADER, true);
  77.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  78.     curl_setopt($ch, CURLOPT_TIMEOUT, 50);
  79.     curl_setopt($ch, CURLOPT_FILE, $fp);
  80.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  81.  
  82.     $starttime=microtime(true);
  83.     $sakuma_rx=shell_exec("sudo ifconfig ".$iface."|grep RX\ bytes|awk '{ print $2 }'|cut -d : -f 2");
  84.     $response=curl_exec($ch);
  85.     $beigu_rx=shell_exec("sudo ifconfig ".$iface."|grep RX\ bytes|awk '{ print $2 }'|cut -d : -f 2");
  86.     $endtme=microtime(true);
  87.  
  88.     if ($response === false){
  89.        print "Request failed:".curl_error($ch);
  90.     }
  91.     curl_close($ch);
  92.     fclose($fp);
  93.  
  94.     #unlink($file);
  95.  
  96.     $duration=$endtme-$starttime;
  97.  
  98.     if($duration<4 && $size!=8) {
  99.         download(++$size,$round);
  100.     }else{
  101.         $sakuma_rx=trim($sakuma_rx);
  102.         $beigu_rx=trim($beigu_rx);
  103.         $rx=$beigu_rx-$sakuma_rx;
  104.         $rx_speed=((($rx*8)/1000)/1000)/$duration;
  105.         write_to_file(round($rx_speed, 2), "d");
  106.         if($duration<4){
  107.           print "Duration is ".round($duration, 2)."sec - this may introduce errors.\n";
  108.         }
  109.         print round(filesize($file)/1000000,2)."Mb at ".round($rx_speed, 2)."Mb/s";
  110.         $globaldownloadspeed+=$rx_speed;
  111.         if ($round > 1){
  112.            download($size,--$round);
  113.         } else {
  114.            print "\tAverage: ".round($globaldownloadspeed/$maxrounds, 2)." Mb/s.\n";
  115.   }
  116.     }
  117. }
  118.  
  119. function upload($size,$round){
  120.     global $server, $uploads, $do_server, $server, $iface, $randoms,$globaluploadspeed,$maxrounds;
  121.  
  122.     $file=$uploads."upload_".$size;
  123.  
  124.     print ".";
  125.     $ch = curl_init();
  126.     curl_setopt($ch, CURLOPT_HEADER, 0);
  127.     curl_setopt($ch, CURLOPT_VERBOSE, 0);
  128.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  129.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
  130.     curl_setopt($ch, CURLOPT_URL, $do_server[$server]."/speedtest/upload.php?x=0.".$randoms);
  131.     curl_setopt($ch, CURLOPT_POST, true);
  132.     $post = array(
  133.         "file_box"=>"@".$file,
  134.     );
  135.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  136.  
  137.     $starttime=microtime(true);
  138.     $sakuma_tx=shell_exec("sudo ifconfig ".$iface."|grep TX\ bytes|awk '{print $6}'|cut -d: -f2");
  139.     $response = curl_exec($ch);
  140.     $beigu_tx=shell_exec("sudo ifconfig ".$iface."|grep TX\ bytes|awk '{print $6}'|cut -d: -f2");
  141.     $endtme=microtime(true);
  142.  
  143.     if ($response === false){
  144.        print "Request failed:".curl_error($ch);
  145.     }
  146.     $aiznem=substr($response, 5);
  147.     $kopa=filesize($file)+$aiznem;
  148.  
  149.     $duration=$endtme-$starttime; // sekundes
  150.  
  151.     if($duration<4 && $size!=8){
  152.         #print round(filesize($file)/1000000,2)."Mb is to small: ".round($duration, 2)."sec\n";
  153.        upload(++$size,$round);
  154.     }else{
  155.         $sakuma_tx=trim($sakuma_tx);
  156.         $beigu_tx=trim($beigu_tx);
  157.         $tx=$beigu_tx-$sakuma_tx;
  158.         $tx_speed=((($tx*8)/1000)/1000)/$duration;
  159.         write_to_file(round($tx_speed, 2), "u");
  160.         if($duration<4){
  161.           print "Duration is ".round($duration, 2)."sec - this may introduce errors.\n";
  162.         }
  163.         print round(filesize($file)/1000000,2)."Mb at ".round($tx_speed, 2)."Mb/s";
  164.         $globaluploadspeed+=$tx_speed;
  165.         if ($round > 1){
  166.            upload($size,--$round);
  167.         } else {
  168.            print "\tAverage: ".round($globaluploadspeed/$maxrounds, 2)." Mb/s.\n";
  169.   }
  170.     }
  171. }
  172.  
  173. function write_to_file($data, $updown){ // u - upload; d - download
  174.     global $day, $time, $datadir, $iface;
  175.     $fp = fopen($datadir."data_".$day.".txt", "a");
  176.     fwrite($fp, $time."|".$updown."|".$iface."|".$data."\n");
  177.     fclose($fp);
  178. }
  179.  
  180. foreach ($do_server as $server => $serverurl){
  181.   $globallatency=0;
  182.   print "* Testing latency for $server...";
  183.   latency($maxrounds);
  184. }
  185. foreach ($do_server as $server => $serverurl){
  186.   $globaldownloadspeed=0;
  187.   print "* Testing download speed for $server...";
  188.   download(1,$maxrounds);
  189. }
  190. foreach ($do_server as $server => $serverurl){
  191.   $globaluploadspeed=0;
  192.   print "* Testing upload speed $server...";
  193.   upload(1,$maxrounds);
  194. }
  195.  
  196. ?>
Advertisement
Add Comment
Please, Sign In to add comment