国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

PHP web開發(fā)HTTP協(xié)議中的KeepAlive

2020-03-24 17:02:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章已經(jīng)寫完將近一年了,最近從歷史郵件里面翻出來(lái),和大家分享一下。其中使用PHP實(shí)現(xiàn)持久的HTTP連接,讓我費(fèi)了很多心思。曾經(jīng)想過(guò)使用C語(yǔ)言編寫一個(gè)PHP的擴(kuò)展來(lái)實(shí)現(xiàn),后來(lái)發(fā)現(xiàn)pfsockopen這個(gè)函數(shù),讓我豁然開朗,避免重新發(fā)明一個(gè)輪子,呵呵。一,KeepAlive的概念:參見 http://en.wikipedia.org/wiki/HTTP_persistent_connection二,KeepAlive的客戶端實(shí)現(xiàn):使用了PHP支持的 pfsockopen 來(lái)實(shí)現(xiàn),參見:http://cn.php.net/pfsockopenKeepAlive必要的Header有:Connection: Keep-Alive
Content-Length: xxx三,性能對(duì)比測(cè)試:幾種對(duì)比實(shí)現(xiàn)方式:1,使用fsockopen來(lái)實(shí)現(xiàn),讀取body內(nèi)容后,關(guān)閉連接,參見測(cè)試程序中的ohttp_get實(shí)現(xiàn)。
2,使用pfsockopen來(lái)實(shí)現(xiàn),讀取body內(nèi)容后,不關(guān)閉連接,參見測(cè)試程序中的phttp_get實(shí)現(xiàn)。
3,php實(shí)現(xiàn)的file_get_contents
4,第三方測(cè)試工具ab前三種測(cè)試在測(cè)試程序中都包含了。測(cè)試用例 一:前三種php實(shí)現(xiàn)的客戶端單進(jìn)程單線程請(qǐng)求lighttpd服務(wù)器一個(gè)16字節(jié)的靜態(tài)文件。順序請(qǐng)求10000次。
客戶端與服務(wù)器部署在不同服務(wù)器,通過(guò)內(nèi)網(wǎng)請(qǐng)求。測(cè)試結(jié)果:第一次:[root@localhost ~]# /opt/bin/php tp.php
phttp_get: 5.3641529083252
ohttp_get: 8.1628580093384
file_get_contents: 12.217950105667第二次:[root@localhost ~]# /opt/bin/php tp.php
phttp_get: 5.033059835434
ohttp_get: 9.589075088501
file_get_contents: 12.775387048721第三次:[root@localhost ~]# /opt/bin/php tp.php
phttp_get: 5.0181269645691
ohttp_get: 8.2286441326141
file_get_contents: 11.089616060257測(cè)試用例 二:使用第三方工具ab來(lái)進(jìn)行測(cè)試,-k參數(shù)開打開keepalive支持,不做并發(fā)測(cè)試,順序請(qǐng)求10000次。
客戶端與服務(wù)器部署在不同服務(wù)器,通過(guò)內(nèi)網(wǎng)請(qǐng)求。以下測(cè)試結(jié)果部分省略:未打開keepalive:[root@localhost ~]# ab -n 10000 -c 1 http://10.69.2.206:8080/sms/ns2/save_msg.txt Finished 10000 requestsConcurrency Level: 1
Time taken for tests: 10.410467 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 2480000 bytes
HTML transferred: 160000 bytes
Requests per second: 960.57 [#/sec] (mean)
Time per request: 1.041 [ms] (mean)
Time per request: 1.041 [ms] (mean, across all conhtml' target='_blank'>current requests)
Transfer rate: 232.55 [Kbytes/sec] receivedConnection 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 requestsConcurrency 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] receivedConnection 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四,在實(shí)際中的應(yīng)用以上實(shí)現(xiàn)的phttp_get和mysql memcache的 中的 保持連接 概念類似,這種技術(shù)一般來(lái)說(shuō),只適用于fastcgi模式的web服務(wù)器。
對(duì)于本機(jī)之間的http通信,在測(cè)試過(guò)程中發(fā)現(xiàn)phttp_get的優(yōu)勢(shì)有限,基本合乎邏輯。
對(duì)于本身處理時(shí)間比較長(zhǎng)的服務(wù),phttp_get的優(yōu)勢(shì)也不明顯。
綜上,phttp_get適用于fastcgi模式的web應(yīng)用調(diào)用遠(yuǎn)程http服務(wù),且此http服務(wù)器響應(yīng)時(shí)間比較短的情況。五,服務(wù)端需要注意的事項(xiàng)1,http服務(wù)器必須支持HTTP/1.1協(xié)議
2,php應(yīng)用必須返回Content-Length:的header,具體實(shí)現(xiàn)參見:http://cn.php.net/manual/en/function.ob-get-length.php需要在代碼中加入:ob_start();
$size=ob_get_length();
header( Content-Length: $size );
ob_end_flush();最后附上測(cè)試代碼: ?php//$url=http://10.69.2.206:8080/sms/ns2/save_msg.txtfunction 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(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 ${query} 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 10000;$i++)
{
$host= 10.69.2.206
$port=8080;
$query= /sms/ns2/save_msg.txt
$body=
$r=ohttp_get($host,$port,$query,$body);
if($r != 200)
{
echo return code : $r/n
}
}
$time2=microtime(true);
for($i=0;$i 10000;$i++)
{
$url= http://10.69.2.206:8080/sms/ns2/save_msg.txt
$host= 10.69.2.206
$port=8080;
$query= /sms/ns2/save_msg.txt
$body=
$r=phttp_get($host,$port,$query,$body);
if($r != 200)
{
echo return code : $r/n
}
}
$time3=microtime(true);
for($i=0;$i array( timeout = 1 )
)
);
$body=file_get_contents($url, 0, $ctx);
$r=200;
if($r != 200)
{
echo return code : $r/n
}
}
$time4=microtime(true);echo phttp_get: .($time3-$time2). /n
echo ohttp_get: .($time2-$time1). /n
echo file_get_contents: .($time4-$time3). /n ?html教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 周至县| 镇雄县| 洛隆县| 龙岩市| 涟源市| 曲阜市| 崇仁县| 华安县| 望城县| 于都县| 舒兰市| 平阴县| 二手房| 巴东县| 台安县| 呼伦贝尔市| 卫辉市| 鄢陵县| 嵊州市| 满洲里市| 海安县| 兴安盟| 库伦旗| 乌苏市| 汉阴县| 绥宁县| 凤山市| 华安县| 阿拉善盟| 深圳市| 襄垣县| 枣阳市| 武功县| 东兰县| 布尔津县| 历史| 武平县| 拉萨市| 定远县| 正安县| 营山县|