php长连接测试
<?php//http://i3.sinaimg.cn/home/07index/sinahome_wscfy_031.gif
//$url=http://10.69.2.206:8080/sms/ns2/save_msg.txt
function ohttp_get($host,$port,$query,&$body)
{
$fp=pfsockopen($host,$port,$errno,$errstr,1);
if(!$fp)
{
var_dump($errno,$errstr);
return -1;
}
$out = "GET /home/07index/sinahome_wscfy_031.gif HTTP/1.1\r\n";
$out.= "Host: ${host}\r\n";
$out.= "Connection: close\r\n";
$out.= "\r\n";
fwrite($fp,$out);
$line=trim(fgets($fp));
$header.=$line;
list($proto,$rcode,$result)=explode(" ",$line);
$len=-1;
while( ($line=trim(fgets($fp))) != "" )
{
$header.=$line;
if(strstr($line,"Content-Length:"))
{
list($cl,$len)=explode(" ",$line);
}
if(strstr($line,"Connection: close"))
{
$close=true;
}
}
if($len < 0)
{
echo "ohttp_get must cope with Content-Length header!\n";
return -1;
}
$body=fread($fp,$len);
if($close)
fclose($fp);
return $rcode;
}
function phttp_get($host,$port,$query,&$body)
{
$fp=pfsockopen($host,$port,$errno,$errstr,1);
if(!$fp)
{
var_dump($errno,$errstr);
return -1;
}
$out = "GET /home/07index/sinahome_wscfy_031.gif HTTP/1.1\r\n";
$out.= "Host: ${host}\r\n";
$out.= "Connection: Keep-Alive\r\n";
$out.= "\r\n";
fwrite($fp,$out);
$line=trim(fgets($fp));
$header.=$line;
list($proto,$rcode,$result)=explode(" ",$line);
$len=-1;
while( ($line=trim(fgets($fp))) != "" )
{
$header.=$line;
if(strstr($line,"Content-Length:"))
{
list($cl,$len)=explode(" ",$line);
}
if(strstr($line,"Connection: close"))
{
$close=true;
}
}
if($len < 0)
{
echo "phttp_get must cope with Content-Length header!\n";
return -1;
}
$body=fread($fp,$len);
if($close)
fclose($fp);
return $rcode;
}
$time1=microtime(true);
for($i=0;$i<100;$i++)
{
$host="i3.sinaimg.cn";
$port=80;
$query="/sms/ns2/save_msg.txt";
$body="";
$r=phttp_get($host,$port,$query,$body);
if($r != 200)
{
echo "return code : $r\n";
}
}
$time2=microtime(true);
for($i=0;$i<100;$i++)
{
$host="i3.sinaimg.cn";
$port=80;
$query="/sms/ns2/save_msg.txt";
$body="";
$r=ohttp_get($host,$port,$query,$body);
if($r != 200)
{
echo "return code : $r\n";
}
}
$time3=microtime(true);
echo "<br />";echo "<hr>";
echo "Time1:".$time1;echo "<br />";
echo "Time2:".$time2;echo "<br />";
echo "Time3:".$time3;echo "<br />";
echo "<br />";echo "<hr>";
echo "keep alive: ".($time2-$time1)."\n";
echo "close: ".($time3-$time2)."\n";
?>
页:
[1]