1.安装
pecl install yar
vim /etc/php.ini 加上extension=yar.so
查看支持的配置:
php --re yar
- Dependencies {
Dependency [ json (Required) ]
}
- INI {
Entry [ yar.packager <PERDIR> ] //打包协议
Current = 'php'
}
Entry [ yar.transport <PERDIR> ]
Current = 'curl'
}
Entry [ yar.debug <ALL> ]
Current = 'Off'
}
Entry [ yar.expose_info <PERDIR> ]
Current = 'On'
}
Entry [ yar.connect_timeout <ALL> ]
Current = '1000'
}
Entry [ yar.timeout <ALL> ]
Current = '5000'
}
Entry [ yar.content_type <ALL> ]
Current = 'application/octet-stream'
}
Entry [ yar.allow_persistent <ALL> ]
Current = '0'
}
}
2.代码
服务端,放到网站根目录/var/www/html:
<?php
/**
yar_server.php
*/
class API {
/**
* the doc info will be generated automatically into service info page.
* @params
* @return
*/
public function api2() {
echo "api function";
}
public function test1(){
sleep(1); //模拟实际情况
$arg = func_get_args();
echo "test1 method with arg 0[$arg[0]] \n";
return "ret1:".date("y-m-d H:i:s");
}
public function test2(){
sleep(2); //模拟实际情况
$arg = func_get_args();
echo "test2 method with arg 0[$arg[0]] \n";
return "ret2:".date("y-m-d H:i:s");
}
public function test3(){
sleep(3); <span style="font-family: Arial, Helvetica, sans-serif;">//模拟实际情况</span>
$arg = func_get_args();
echo "test3 method with arg 0[$arg[0]] \n";
return "ret3:".date("y-m-d H:i:s");
}
public function test4(){
sleep(4); <span style="font-family: Arial, Helvetica, sans-serif;">//模拟实际情况</span>
$arg = func_get_args();
echo "test4 method with arg 0[$arg[0]] \n";
return "ret4:".date("y-m-d H:i:s");
}
protected function client_can_not_see() {
echo __FILE__;
}
protected function client_can_not_see2() {
echo __FILE__;
}
}
$service = new Yar_Server(new API());
$service->handle();通过浏览器访问,如下:客户端:
<?php
/**
*yar_client.php
*/
$url = "http://localhost/yar_server.php";
echo str_repeat("-",40),"single request",str_repeat("-",40),"\n";
$start = time();
$c = new Yar_Client($url);
$c->test1("single arg1");
$c->test2("single arg2");
$c->test3("single arg3");
$c->test4("single arg4");
echo str_repeat("-",30),"time:",time()-$start,str_repeat("-",30),"\n";
echo str_repeat("-",40),"concurrent",str_repeat("-",40),"\n";
$start = time();
Yar_Concurrent_Client::call($url,"test1",array("arg1"),"callback");
Yar_Concurrent_Client::call($url,"test2",array("arg1"),"callback");
Yar_Concurrent_Client::call($url,"test3",array("arg1"),"callback");
Yar_Concurrent_Client::call($url,"test4",array("arg1"),"callback");
Yar_concurrent_Client::loop();
function callback($retval,$info){
$args = func_get_args();
$args['time'] = date("y-m-d H:i:s");
var_dump($args);
}
echo str_repeat("-",30),"time:",time()-$start,str_repeat("-",30),"\n";
php yar_client.php
----------------------------------------single request----------------------------------------
test1 method with arg 0[single arg1]
test2 method with arg 0[single arg2]
test3 method with arg 0[single arg3]
test4 method with arg 0[single arg4]
------------------------------total time:10s------------------------------
----------------------------------------concurrent----------------------------------------
array(3) {
[0]=>
string(22) "ret1:14-06-12 19:35:03" //进入时间
[1]=>
array(3) {
["sequence"]=>
int(1)
["uri"]=>
string(31) "http://localhost/yar_server.php"
["method"]=>
string(5) "test1"
}
["time"]=>
string(17) "14-06-12 19:35:03"
}
array(3) {
[0]=>
string(22) "ret2:14-06-12 19:35:04" //进入时间
[1]=>
array(3) {
["sequence"]=>
int(2)
["uri"]=>
string(31) "http://localhost/yar_server.php"
["method"]=>
string(5) "test2"
}
["time"]=>
string(17) "14-06-12 19:35:04"
}
array(3) {
[0]=>
string(22) "ret3:14-06-12 19:35:05" //进入时间
[1]=>
array(3) {
["sequence"]=>
int(3)
["uri"]=>
string(31) "http://localhost/yar_server.php"
["method"]=>
string(5) "test3"
}
["time"]=>
string(17) "14-06-12 19:35:05"
}
array(3) {
[0]=>
string(22) "ret4:14-06-12 19:35:06" //进入时间
[1]=>
array(3) {
["sequence"]=>
int(4)
["uri"]=>
string(31) "http://localhost/yar_server.php"
["method"]=>
string(5) "test4"
}
["time"]=>
string(17) "14-06-12 19:35:06"
}
------------------------------total time:4s------------------------------
可见,四个函数在串行时,用时10s,在并发时,仅4s.提高了效率。

1146

被折叠的 条评论
为什么被折叠?



