现在的位置: 首页 > 综合 > 正文

php 计算程序运行时间的类,以及用它和Curl结合Host,实现不用配置Host得到该测试机的访问速度。

2012年05月18日 ⁄ 综合 ⁄ 共 2852字 ⁄ 字号 评论关闭

经常在运行php程序的时候,想知道某个程序到底运行了多久。这样可以查找一些程序运行的效率问题。
一)最近写了一个程序运行的时间计算类,供大家参考:

  1. class Timer {    
  2.     private $StartTime = 0;//程序运行开始时间  
  3.     private $StopTime  = 0;//程序运行结束时间  
  4.     private $TimeSpent = 0;//程序运行花费时间  
  5.     function start(){//程序运行开始  
  6.         $this->StartTime = microtime();    
  7.     }    
  8.     function stop(){//程序运行结束  
  9.         $this->StopTime = microtime();    
  10.     }    
  11.     function spent(){//程序运行花费的时间  
  12.         if ($this->TimeSpent) {    
  13.             return $this->TimeSpent;    
  14.         } else {  
  15.          list($StartMicro$StartSecond) = explode(" "$this->StartTime);  
  16.          list($StopMicro$StopSecond) = explode(" "$this->StopTime);  
  17.             $start = doubleval($StartMicro) + $StartSecond;  
  18.             $stop = doubleval($StopMicro) + $StopSecond;  
  19.             $this->TimeSpent = $stop - $start;  
  20.             return substr($this->TimeSpent,0,8)."秒";//返回获取到的程序运行时间差  
  21.         }    
  22.     }    
  23. }    



调用:

  1. $timer = new Timer();    
  2. $timer->start();  
  3. //...程序运行的代码  
  4. $timer->stop();  
  5. echo "程序运行时间为:".$timer->spent();  



二)不用配置Host得到该测试机的访问速度:

如:我想测试一下那个Host为:C:\Windows\System32\drivers\etc\hosts
72.46.130.186  justwinit.cn 的访问速度,用PHP可以用如下代码:

  1. <?php  
  2. $timer = new Timer();  
  3. $timer->start();  
  4.   
  5. $data = "a=a";  
  6. $returnStr = curl_post($data);  
  7. echo $returnStr;  
  8.   
  9. $timer->stop();  
  10. echo "程序运行时间为:".$timer->spent();  
  11. ?>  
  12.   
  13. <?php  
  14.     function curl_post($data,$host = "http://72.46.130.186")  
  15.     {  
  16.       $ch = curl_init();  
  17.       $res= curl_setopt ($ch, CURLOPT_URL,$host);  
  18.       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  19.       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
  20.       curl_setopt ($ch, CURLOPT_HEADER, 0);  
  21.       curl_setopt($ch, CURLOPT_POST, 1);  
  22.       curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  23.       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  24.       curl_setopt($ch,CURLOPT_HTTPHEADER,array("Host: justwinit.cn"));  
  25.   
  26.       $xyz = curl_exec ($ch);  
  27.       if ($xyz == NULL)  
  28.       {  
  29.         return 0;  
  30.       }  
  31.       return $xyz;  
  32.     }  
  33.   
  34.   
  35. ?>  
  36.   
  37.   
  38.   
  39. <?php  
  40. class Timer {  
  41.     private $StartTime = 0;//程序运行开始时间  
  42.     private $StopTime  = 0;//程序运行结束时间  
  43.     private $TimeSpent = 0;//程序运行花费时间  
  44.     function start(){//程序运行开始  
  45.         $this->StartTime = microtime();  
  46.     }  
  47.     function stop(){//程序运行结束  
  48.         $this->StopTime = microtime();  
  49.     }  
  50.     function spent(){//程序运行花费的时间  
  51.         if ($this->TimeSpent) {  
  52.             return $this->TimeSpent;  
  53.         } else {  
  54.          list($StartMicro$StartSecond) = explode(" "$this->StartTime);  
  55.          list($StopMicro$StopSecond) = explode(" "$this->StopTime);  
  56.             $start = doubleval($StartMicro) + $StartSecond;  
  57.             $stop = doubleval($StopMicro) + $StopSecond;  
  58.             $this->TimeSpent = $stop - $start;  
  59.             return substr($this->TimeSpent,0,8)."秒";//返回获取到的程序运行时间差  
  60.         }  
  61.     }  
  62. }  
  63. ?>  

抱歉!评论已关闭.