yar安装使用

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";


3.测试

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.提高了效率。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anssummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值