Home  >  Article  >  Backend Development  >  测试一个测网速的脚本,不知道哪里错了

测试一个测网速的脚本,不知道哪里错了

WBOY
WBOYOriginal
2016-06-23 13:25:511285browse

/*==创建将传输给用户的数据文件==*/  $fp=fopen("cs.txt","w");  for($i=0;$i<1704000;$i++){      fwrite($fp,"bandwidthkh");  }  fclose($fp);// ==现在编写计算网络速度的脚本=function microtime_float(){    list($usec, $sec) = explode(" ", microtime());    return ((float)$usec + (float)$sec);}$data=file_get_contents("cs.txt");$start=microtime_float();  $fsize=filesize("cs.txt")/1024;     //  filesize返回文件大小的字节数echo "向客户端发送" . $fsize . "KB数据<br>";  echo "<!--" . $data . "-->";  $stop=microtime_float();$duration=($stop-$start);echo $duration."s<br>";  $speed=round($fsize/$duration,2);  echo "Your network speed:$speed KB/sec";

结果测出的结果是

达到1g每秒,这不可能啊?


回复讨论(解决方案)

是可能!
你测试的只是
$fsize=filesize("cs.txt")/1024;     //  filesize返回文件大小的字节数
echo "向客户端发送" . $fsize . "KB数据
";  
echo "";  
这3句执行的速度,与网速无关

microtime(true) 得到的就是 float 类型的秒数
不必再写 microtime_float 函数

以前我用ajax做上传文件时,几G的文件几秒就传好了,所以我觉得用本地的电脑既当服务器又当客户端,这个速度是正常的

是可能!
你测试的只是
$fsize=filesize("cs.txt")/1024;     //  filesize返回文件大小的字节数
echo "向客户端发送" . $fsize . "KB数据
";  
echo "";  
这3句执行的速度,与网速无关


这是为什么呢?
<script language="javascript" type="text/javascript">  function gettime()  {        var time;        time=new Date();      return time.getTime();    }  start_time=gettime(); //  alert(start_time); </script><?php  echo '<h2>带宽测试</h2>'; for ($i=0;$i<100300;$i++){  echo "<!--1234567890#########0#########0#########0#########0#########0#########0#########0#########012345-->";}//echo strlen($str);  // 1个ascll码1字节?><p id="dk"></p>  <script language="javascript" type='text/javascript'>  var timea;  var netspeed;  timea=gettime()-start_time;  netspeed=Math.round(10240/timea*1000);  document.getElementById("dk").innerHTML="向客户端发送10MB字节数据,耗时"+timea+"ms<br />您与此服务器的连接速度为"+netspeed+"kB/s";  </script>

这个js脚本应该可以吧?

microtime(true) 得到的就是 float 类型的秒数
不必再写 microtime_float 函数


谢谢,

测网速是通过计算一个数据块从A到B的时间进行的,你主贴的只是在单方计算
#4 的代码理论上可行,但如果双方时钟不一致时,误差就极大了

以前我用ajax做上传文件时,几G的文件几秒就传好了,所以我觉得用本地的电脑既当服务器又当客户端,这个速度是正常的


局域网的网速好像是几十M每秒?

以前我用ajax做上传文件时,几G的文件几秒就传好了,所以我觉得用 本地的电脑既当服务器又当客户端,这个速度是正常的

这样的话,你能说是 网速吗?不要自欺欺人了
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP编译安装Next article:PHP网页打开速度超慢,急~