服务器测速,php在线测速

网络测速

既然是一台服务器,那么这台服务器谁要访问。访问的用户可能来自北京,上海,天津,浙江,全国范围的都会。英文站要考虑全球。

chinaz提供了一个全国各个地区来测试一台服务器速度的接口。ping.chinaz.com

当然前提是这台服务器可以ping通。

如果更详细的测速,那么最好是自己在服务器上下载一个500M的文件,来测试下载速度。上传一个100M的文件。来测试一下上传速度。

月小升找到一个php程序,可以在线测试下载上传。原理即上面的文件方法,所以这样测试出来的速度还是比较准确的。最起码你可以迅速对比两台拥有FTP账户的机房网络速度。

 
header('Content-Type: text/html; charset=utf-8');
 
header("Cache-Control:no-cache");
 //$self = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
 $self = $_SERVER['PHP_SELF'];
 if(isset($_POST['downTime'])&&isset($_POST['upTime'])){
  if(is_numeric($_POST['downTime'])&&is_numeric($_POST['upTime'])){
   showResult();
  }else{
   showTest();
  }
 }elseif($_POST['action']=='uploadTest'){
  echo 'ok';
 }else{
  showTest();
 }
 function showTest(){?>
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 <title>网速测试程序  测试正在进行中……</title>
 </head>
 <body>
 <table align="center" cellpadding="1" cellspacing="0">
 <tr>
  <td height="30" colspan="2" align="center"><b>下载速度测试中</b></td>
 </tr>
 <tr>
  <td width="300" style="border:1px solid blue"><table cellpadding="0" cellspacing="0">
  <tr>
   <td id="downProgressBar" bgcolor="blue" style="width:0px; height:17px"></td>
  </tr>
  </table></td>
  <td id="downProgressNum" align="right" width="35">0%</td>
 </tr>
 <form id="TestForm" action="" method="post"><input name="downTime" id="downTime" type="hidden" /><input name="upTime" id="upTime" type="hidden" /></form>
 </table>
 <script type="text/javascript">
 <!--
 function $id(id){
  return document.getElementById(id);
 }
 
function setDownProgress(){
  Percentage++;
  dpb.style.width=(Percentage*3)+'px';
  dpn.innerHTML = Percentage+'%';
 }
 
function setUpProgress(){
  Percentage+=10;
  upb.style.width=(Percentage*3)+'px';
  upn.innerHTML = Percentage+'%';
  if(Percentage==100){
   upTime =(new Date()).getTime() - upTime;
   submitTest();
  }
 }
 
function XmlHttp(){
  try{
   if(window.XMLHttpRequest){
    var req = new XMLHttpRequest();
    if(req.readyState == null){
     req.readyState = 1;
     req.addEventListener("load", function(){
      req.readyState = 4;
      if(typeof req.onreadystatechange == "function")
       req.onreadystatechange();
     }, false);
    }
    return req;
   }
   if(window.ActiveXObject){
    return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
   }
  }
  catch(e){}
 }
 
function getXmlHttpPrefix(){
  if(getXmlHttpPrefix.prefix)
   return getXmlHttpPrefix.prefix;
  var prefixes = ["MSXML2","Microsoft","MSXML","MSXML3"];
  var o;
  for(var i=0;i<prefixes.length;i++){
   try{
    o = new ActiveXObject(prefixes[i] + ".XmlHttp");
    return getXmlHttpPrefix.prefix = prefixes[i];
   }
   catch(ex){};
  }
 }
 
function upTest(){
  var xmlHttp = XmlHttp();
  if(!xmlHttp)errorSubmit();
  xmlHttp.open('POST','<?=$GLOBALS['self']?>',true);
  xmlHttp.setRequestHeader("Content-Length",upBody.length);
  xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
  xmlHttp.onreadystatechange = function(){
   if(xmlHttp.readyState == 4){
    setUpProgress();
   }else if(xmlHttp.readyState == 3){
    upTest();
   }
  }
  xmlHttp.send(upBody);
 }
 
function submitTest(){
  $id('downTime').value=downTime;
  $id('upTime').value=upTime;
  $id('TestForm').submit();
 }
 
function errorSubmit(t){
  if(t)
   upTime = t;
  else
   upTime = 0;
  submitTest();
 }
 
var dpb = $id("downProgressBar");
 var dpn = $id("downProgressNum");
 var upClickCount = Percentage = 0;
 var downTime =(new Date()).getTime();
 -->
 </script><?php
 $outText = getTestText();
 for($i = 1;$i<100;$i++){
  echo '<!--'.$outText."-->\n";
  echo "<script type=\"text/javascript\">setDownProgress();</script>\n";
 }
 ?>
 <script type="text/javascript">
 var outstr = '<?=$outText?>';
 setDownProgress();
 downTime =(new Date()).getTime()-downTime;
 </script>
 <table align="center" cellpadding="1" cellspacing="0">
 <tr>
  <td height="30" colspan="2" align="center"><b>上转速度测试中</b></td>
 </tr>
 <tr>
  <td width="300" style="border:1px solid blue"><table cellpadding="0" cellspacing="0">
  <tr>
   <td id="upProgressBar" bgcolor="blue" style="width:0px; height:17px"></td>
  </tr>
  </table></td>
  <td id="upProgressNum" align="right" width="35">0%</td>
 </tr>
 </table>
 <script type="text/javascript">
 Percentage =0;
 var upb = $id("upProgressBar");
 var upn = $id("upProgressNum");
 var upBody='';
 for(i=0;i<10;i++)upBody +=outstr;
 upBody = 'action=uploadTest&content='+upBody;
 setTimeout('errorSubmit(-1)',300000);
 upTest();
 var upTime =(new Date()).getTime();
 </script>
 </body>
 </html><?php
 }
 function showResult(){?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 <title>网速测试</title>
 </head>
 <body>
 
 <div align="center">
 说明,本程序测试的上传速度为上传到本站的速度<br /><br/>
 <?php
  $dKBps = round(500000/$_POST['downTime'],2);
  $dKbps = $dKBps * 8;
  echo '下载速度:'.$dKbps.'Kbps,相当于'.$dKBps."KB/s<br />\n";
  if($_POST['upTime']=='-1'){
   echo '上转测试超时。';
  }else if($_POST['upTime']=='0'){
   echo '您的浏览器不支持本上传测试程序,请使用IE6.0以上或FireFox。';
  }else{
   $uKBps = round(500000/$_POST['upTime'],2);
   $uKbps = $uKBps * 8;
   echo '上传速度:'.$uKbps.'Kbps,相当于'.$uKBps.'KB/s';
  }
  echo '<br /><br /><br /><span style="padding-left:100px"><a href="'.$GLOBALS['self'].'">再测一次</a></span>'?>
  </div>
 </body>
 </html><?php
 }
 function getTestText(){
  $result;
  for($i = 0;$i<5000;$i++){
   $result .= '*';
  }
  return $result;
 }

来看一下我的服务器速度么。 http://java-er.com/blog/test.php


This entry was posted in PHP, 互联网观察 and tagged . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/server-speed/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力

17 Responses to 服务器测速,php在线测速

  1. 梁焕彬 says:

    <>
    我将代码直接拷贝后,页面打开出错,请教如何才可正常??

  2. 梁焕彬 says:

    请教安装设置步骤?

  3. 梁焕彬 says:

    挂上服务器后出错如下:
    Parse error: syntax error, unexpected ‘}’ in /www/ipos/test/index.php on line 170

    • 月小升 says:

      你从115上下载的源码不会错,从这里拷贝的是有问题。似乎博客后台自动给转码了。给你QQ邮箱发了邮件了。请查收。

  4. 敬鹏 says:

    我用您的代码在我的服务器测速,数据好像不对,而且没有下载数据的追踪。

  5. 孟兆剑 says:

    您好,能给我qq发一份吗 694211627

  6. Brian says:

    哈喽小升前辈,我是一名web新人… 我发现把源码上传到空间以后测试速度下载永远是瞬间完成,然后显示 下载速度:34042.56Kbps,相当于4255.32KB/s 上传则比较正常,请问这是为什么呢

  7. says:

    您好,能给我邮箱发一份吗 1019776119@qq.com

  8. 孙先生 says:

    好好好! 请给一份源码

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

  

About Me

静水流深,水滴石穿