存档

‘客户端’ 分类的存档

关于HTTP 协议中的 KeepAlive

2009年4月1日

原创文章
这篇文章已经写完将近一年了,最近从历史邮件里面翻出来,和大家分享一下。
其中使用PHP实现持久的HTTP连接,让我费了很多心思。
曾经想过使用C语言编写一个PHP的扩展来实现,后来发现pfsockopen这个函数,让我豁然开朗,避免重新发明一个轮子,呵呵。

一,KeepAlive的概念:

参见 http://en.wikipedia.org/wiki/HTTP_persistent_connection

二,KeepAlive的客户端实现:

使用了PHP支持的 pfsockopen 来实现,参见:http://cn.php.net/pfsockopen

KeepAlive必要的Header有:

Connection: Keep-Alive
Content-Length: xxx

三,性能对比测试:

几种对比实现方式:

1,使用fsockopen来实现,读取body内容后,关闭连接,参见测试程序中的ohttp_get实现。
2,使用pfsockopen来实现,读取body内容后,不关闭连接,参见测试程序中的phttp_get实现。
3,php实现的file_get_contents
4,第三方测试工具ab

前三种测试在测试程序中都包含了。

测试用例 一:

前三种php实现的客户端单进程单线程请求lighttpd服务器一个16字节的静态文件。顺序请求10000次。
客户端与服务器部署在不同服务器,通过内网请求。

测试结果:

第一次:

[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

测试用例 二:

使用第三方工具ab来进行测试,-k参数开打开keepalive支持,不做并发测试,顺序请求10000次。
客户端与服务器部署在不同服务器,通过内网请求。

以下测试结果部分省略:

未打开keepalive:

[root@localhost ~]# ab -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: 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 concurrent requests)
Transfer rate: 232.55 [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(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”;

?>

mengguang web服务, 客户端, 系统

Linux跨网络运行X Window程序

2009年2月11日

X Window在设计上就是跨网络的,X Client是需要图形显示的应用程序, X Server则负责具体显示和传递用户交互行为。二者之间通信的协议称为 X Protocol,X协议。

基于主机验证的X Window配置

(1) 在X Server端,加入允许发送X Request的机器地址。

$ xhost +192.168.0.1

关于xhost的用法示例:

$ xhost -192.168.0.1 #取消192.168.0.1发送X Request到本机
$ xhost + #允许所有主机发送X Request到本机
$ xhost + #再次执行该命令取消允许所有主机的授权

此外,可在/etc/X*.hosts中永久加入某些授权主机,其中*是本机显示编号,比如X0.hosts。细节可看man xhost的说明。

192.168.0.1
192.168.0.2

(2) 现在,就可以ssh(可能需要配置ssh转发X11数据,我没尝试过)或者telnet到X Client机器,并运行X Window应用程序,而显示和操作在X Server端。

$ xeyes -display 192.168.0.254:0

其中192.168.0.254是(1)中配置的主机,后面的:0表示发送到0号显示屏幕。有些X程序不支持-display参数,此时可考虑导出DISPLAY环境变量。

$ export DISPLAY=192.168.0.254:0

也许你会问,一台机器可以有多个显示屏幕吗?有的,默认启动的屏幕为0,不过你还可以启动多个。对于gdm启动X Window的方式,你可以修改/etc/X11/gdm/gdm.conf:

0=/usr/bin/X11/X -bpp 8 vt7
1=/usr/bin/X11/X -bpp 8 vt9
...

-bpp.参数指定颜色数,此处为8位色深。vt7表示Ctrl+Alt+F7可切换到该屏幕,vt9表示Ctrl+Alt+F9。你可以指定任意数目的显示屏幕。

如果要配置不同屏幕的登录界面,可执行如下操作:

$ cp /etc/X11/gdm/Init/Default /etc/X11/gdm/Init/:0
$ cp /etc/X11/gdm/Init/Default /etc/X11/gdm/Init/:1

然后可修改其中的配置命令。

对于startx启动X Window的方式,可直接在命令行指定,比如 startx — :1。

基于每用户验证的X Window配置

基本步骤是:先在X Server端的用户目录生成用户的cookie,然后把该cookie加入到X Client的用户目录。这样X Client程序运行的时候,会根据当前的DISPLAY搜寻cookie信息,并发送到X Server,从而得到验证。

因此,首先需要在X Server端生成cookie,可用xauth命令。

$ xauth
Using authority file /home/yingyuan/.Xauthority
xauth>list
192.168.0.199/unix:0 MIT-MAGIC-COOKIE-1 8432567fa3ae2341
xauth>add 192.168.0.199:0 MIT-MAGIC-COOKIE-1 8432567fa3ae2341
xauth>list
192.168.0.199/unix:0 MIT-MAGIC-COOKIE-1 8432567fa3ae2341
192.168.0.199:0 MIT-MAGIC-COOKIE-1 8432567fa3ae2341
xauth>exit

系统原来就有了一个cookie,我们用add命令新加了一个。

那么,如何把cookie传递给X Client呢?实现方法有三种,以下分别介绍。

(1) 直接把~/.Xauthority从X Server复制为X Client下的~/.Xauthority。这是最简单的实现办法。

(2) 用xauth的extract和merge命令。

在X Server端,

$ xauth
...
xauth>extract MyCookie 192.168.0.199:0
xauth>exit

然后我们把MyCookie文件传到X Client,并在X Client运行如下命令,

$ xauth
...
xauth>merge MyCookie
xauth>exit

(3) 记下X Server端的cookie值(用xauth的list可查看),

$ xauth
...
xauth>list
192.168.0.199/unix:0 MIT-MAGIC-COOKIE-1 8432567fa3ae2341
192.168.0.199:0 MIT-MAGIC-COOKIE-1 8432567fa3ae2341
xauth>exit

然后在X Client用xauth的add添加到.Xauthority文件。

$ xauth
...
xauth>add 192.168.0.199:0 MIT-MAGIC-COOKIE-1 8432567fa3ae2341
xauth>exit

X Window为我们运行程序提供了很大的灵活性,不是一般的GUI操作系统所能比拟的。Microsoft Windows可以通过运行X OnNet、X-WinPro、Omni-X等程序提供X Server服务,从而可以运行Linux上的X Client程序。

yingyuan 客户端, 操作系统, 服务器 ,