douli8428
2016-09-12 13:18
浏览 218
已采纳

json_decode - 如何在GuzzleHttp异步请求中加快速度

In my application I'm using the GuzzleHttp library, but it not probably the problem, but is't good to say it.

Every minute (using cron) I need to get data from 40+ addresses, so I took GuzzleHttp lib to be fast as possible.

Guzzle code:

$client = new Client();
$rectangles = $this->db->query("SELECT * FROM rectangles");

$requests = function ($rectangles)
{
    foreach($rectangles as $rectangle)
    {
       // some GEO coords (It's not important)
       $left   = $rectangle["lft"];
       $right  = $rectangle["rgt"];
       $top    = $rectangle["top"];
       $bottom = $rectangle["bottom"];

       $this->indexes[] = $rectangle;

       $uri = "https://example.com/?left=$left&top=$top&right=$right&bototm=$bottom";
            yield new Request("GET", $uri);
   }

};

$pool = new Pool($client, $requests($rectangles), [
    'concurrency' => 5,
    'fulfilled' => function ($response, $index) {
        $resp   = $response->getBody();
        $carray = json_decode($resp,true);

        if($carray["data"] != null)
        {
            $alerts = array_filter($carray["data"], function($alert) {
                return $alert["type"] == 'xxx';
            });

            $this->data = array_merge($this->data, $alerts);
            $this->total_count += count($alerts);
        }
    },
    'rejected' => function ($reason, $index) {},
]);

$promise = $pool->promise();
$promise->wait();

return $this->data;

Of course i made a benchmark of this.

1. getting data from another server 0.000xx sec
2. json_decode 0.001-0.0100 (this is probably the problem :-()

The entire code takes about 6-8 seconds. It depends on the amount of data that is on a remote server.

All the time I thought Guzzle performs request asynchronously, so it will takes time as the longest request.

(slowest request = 200 ms == all request = 200 ms) - But this is probably not true! Or I am doing something wrong.

I used an associative array in json_decode (I feel that this is an acceleration of 1 sec (I'm not sure...)).

My question is, can I this code more optimize and speed it up?

I wish to make it fast as one the slowest request (0.200 sec).

PS: The data that I'm getting from URLs are just long JSONs. Thanks!

EDIT: I changed the 'concurrency' => 5 to 'concurrency' => 100 and now the duration is about 2-4 sec

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题