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

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-server
Server Hostname:        127.0.0.1
Server Port:            9501

Document Path:          /
Document Length:        0 bytes

Concurrency Level:      100
Time taken for tests:   0.207 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      147000 bytes
HTML transferred:       0 bytes
Requests 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] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   3.1      0       9
Processing:     4   18   6.8     21      26
Waiting:        0   17   7.9     21      26
Total:          5   20   4.3     21      26

Percentage 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-server
Server Hostname:        127.0.0.1
Server Port:            9501

Document Path:          /
Document Length:        0 bytes

Concurrency Level:      100
Time taken for tests:   127.424 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      147000 bytes
HTML transferred:       0 bytes
Requests 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] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.6      0       2
Processing:   111 12019 2622.8  12825   15007
Waiting:      111 12019 2622.9  12825   15007
Total:        113 12019 2622.4  12825   15007

Percentage 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)

你可能感兴趣的:(PHP,Swoole)