Swoole中异步连接客户端/请求API

来源:互联网 发布:wifi防蹭网软件 编辑:程序博客网 时间:2024/06/05 21:13

Swoole的异步客户端给我们提供了异步发送请求,接受数据的功能。这里使用异步客户端请求API数据。
上代码:

class MyClient{    private $cli;    private $ip;    private $port;    public function __construct($ip, $port, $href)    {        $this->ip = $ip;//请求地址        $this->port = $port;//端口号        Swoole\Async::dnsLookup($this->ip, function ($domainName, $ip) use ($href){            $this->cli = new swoole_http_client($ip, $this->port);//异步非阻塞            $this->cli->setHeaders([                'Host' => $domainName,                "User-Agent" => 'Chrome/49.0.2587.3',                'Accept-Encoding' => 'gzip',            ]);            $this->cli->get($href, function () {                //var_dump(json_decode($this->cli->body));                echo $this->ip."\n";            });        });    }    public function onConnect()    {        echo "success:".$this->ip."\n";    }    public function onReceive(swoole_client $cli, $data)    {        echo "Receive:".$this->ip.":\n";        var_dump($data);    }    public function onError()    {        echo "error\n";    }    public function onClose()    {        echo "Connection close:".$this->ip."\n";    }}class MyServer{    private $Client1;    private $Client2;    private $baidu = "api.map.baidu.com";    private $amap = "restapi.amap.com";    private $baidu_href = "/location/ip?ip=yourip&ak=yourkey";    private $amap_href = "/v3/ip?key=yourkey&ip=yourip";    public function __construct()    {        $this->serv = new swoole_http_server("0.0.0.0", 9501);        $this->serv->on('request', array($this, 'onRequest'));        $this->serv->start();    }    public function onRequest()    {        $this->Client1 = new MyClient($this->baidu, 80, $this->baidu_href);        $this->Client2 = new MyClient($this->amap, 80, $this->amap_href);    }}$Server = new MyServer();

这里使用两个接口,百度地图的IP定位接口和高德地图的IP定位接口,使用时将IP和KEY替换成需要的。
使用ab工具进行测试:

ab -c 100 -n 1000 http://127.0.0.1:9501/

测试结果:

Server Software:        swoole-http-serverServer Hostname:        127.0.0.1Server Port:            9501Document Path:          /Document Length:        0 bytesConcurrency Level:      100Time taken for tests:   0.207 secondsComplete requests:      1000Failed requests:        0Write errors:           0Total transferred:      147000 bytesHTML transferred:       0 bytesRequests per second:    4837.04 [#/sec] (mean)Time per request:       20.674 [ms] (mean)Time per request:       0.207 [ms] (mean, across all concurrent requests)Transfer rate:          694.38 [Kbytes/sec] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    2   3.1      0       9Processing:     4   18   6.8     21      26Waiting:        0   17   7.9     21      26Total:          5   20   4.3     21      26Percentage of the requests served within a certain time (ms)  50%     21  66%     23  75%     24  80%     24  90%     24  95%     25  98%     26  99%     26 100%     26 (longest request)

对比使用file_get_contents方式请求API数据的方法,效率提升得非常非常多。下面是使用file_get_contents方法请求的代码和测试数据:

class MyServer{    private $Client1;    private $Client2;    private $baidu = "http://api.map.baidu.com";    private $amap = "http://restapi.amap.com";    private $baidu_href = "/location/ip?ip=yourip&ak=yourkey";    private $amap_href = "/v3/ip?key=yourkey&ip=yourip";    public function __construct()    {        $this->serv = new swoole_http_server("0.0.0.0", 9501);        $this->serv->on('request', array($this, 'onRequest'));        $this->serv->start();    }    public function onRequest()    {       $this->Client1 = file_get_contents($this->baidu.$this->baidu_href);       echo "success:".$this->baidu."\n";       $this->Client2 = file_get_contents($this->amap.$this->amap_href);       echo "success:".$this->amap."\n";    }}$Server = new MyServer();

测试命令:

ab -c 100 -n 1000 http://127.0.0.1:9501/

测试结果:

Server Software:        swoole-http-serverServer Hostname:        127.0.0.1Server Port:            9501Document Path:          /Document Length:        0 bytesConcurrency Level:      100Time taken for tests:   127.424 secondsComplete requests:      1000Failed requests:        0Write errors:           0Total transferred:      147000 bytesHTML transferred:       0 bytesRequests per second:    7.85 [#/sec] (mean)Time per request:       12742.387 [ms] (mean)Time per request:       127.424 [ms] (mean, across all concurrent requests)Transfer rate:          1.13 [Kbytes/sec] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0   0.6      0       2Processing:   111 12019 2622.8  12825   15007Waiting:      111 12019 2622.9  12825   15007Total:        113 12019 2622.4  12825   15007Percentage of the requests served within a certain time (ms)  50%  12825  66%  13005  75%  13178  80%  13237  90%  13626  95%  14821  98%  14928  99%  14947 100%  15007 (longest request)