Kbytes/sec] received
Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 30.0 0 3002 Processing: 0 0 0.4 0 9 Waiting: 0 0 0.3 0 9 Total: 0 0 30.0 0 3003
打开keepalive:
[root@localhost ~]# ab -k -n 10000 -c 1 “http://10.69.2.206:8080/sms/ns2/save_msg.txt”
Finished 10000 requests
Concurrency Level: 1 Time taken for tests: 4.148619 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Keep-Alive requests: 9412 Total transferred: 2527060 bytes HTML transferred: 160000 bytes Requests per second: 2410.44 [#/sec] (mean) Time per request: 0.415 [ms] (mean) Time per request: 0.415 [ms] (mean, across all concurrent requests) Transfer rate: 594.66 [Kbytes/sec] received
Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.1 0 5 Processing: 0 0 2.1 0 203 Waiting: 0 0 2.1 0 203 Total: 0 0 2.1 0 203
四,在实际中的应用
以上实现的phttp_get和mysql memcache的 中的“保持连接”概念类似,这种技术一般来说,只适用于fastcgi模式的web服务器。 对于本机之间的http通信,在测试过程中发现phttp_get的优势有限,基本合乎逻辑。 对于本身处理时间比较长的服务,phttp_get的优势也不明显。 综上,phttp_get适用于fastcgi模式的web应用调用远程http服务,且此http服务器响应时间比较短的情况。
五,服务端需要注意的事项
1,http服务器必须支持HTTP/1.1协议 2,php应用必须返回Content-Length:的header,具体实现参见:
http://cn.php.net/manual/en/function.ob-get-length.php
需要在代码中加入:
ob_start(); $size=ob_get_length(); header(”Content-Length: $size”); ob_end_flush();
最后附上测试代码:
<?php
//$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 ${query} 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(fge |