Skip to content

Instantly share code, notes, and snippets.

@ekojs
Last active October 10, 2018 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekojs/e112a89aaf1c342d3f06115b9e14a534 to your computer and use it in GitHub Desktop.
Save ekojs/e112a89aaf1c342d3f06115b9e14a534 to your computer and use it in GitHub Desktop.
Open issue in Guzzle 6
<?php
require_once __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Promise;
class ApiTest {
public $client;
public static $instance;
public function __construct() {
$this->client = new Client([
'base_uri' => '',
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'verify' => false,
'http_errors' => false
]);
self::$instance = $this;
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function bulkRequest($params,&$res,$concurrency=10,$fulfillCallback=null,$rejectCallback=null){
if(empty($params)) die('Parameters must array.');
if(empty($fulfillCallback)){
$fulfillCallback = function(ResponseInterface $response, $index) use (&$res) {
array_push($res,$response);
};
}
if(empty($rejectCallback)){
$rejectCallback = function($reason, $index) use (&$res) {
array_push($res,$reason);
};
}
$requests = function ($params) {
foreach ($params as $v) {
yield new Request($v['method'],$v['url'],$v['headers'],$v['body']);
}
};
(new Pool(
$this->client,
$requests($params),[
'concurrency' => $concurrency,
'fulfilled' => $fulfillCallback,
'rejected' => $rejectCallback
]
))->promise()->wait();
}
}
<?php
require_once 'ApiTest.php';
$client = ApiTest::getInstance();
$interval = 100;
$concurrency = 10;
$res = array();
echo sprintf("Printing memory usage every %d requests\n", $interval);
echo "Fetching package list... ";
$packageNames = json_decode($client->client->get('https://packagist.org/packages/list.json')->getBody()->getContents())->packageNames;
echo 'done. (' . count($packageNames) . " packages)\n\n";
$params = function($packageNames){
foreach($packageNames as $v){
yield array(
'method' => 'GET',
'url' => "https://packagist.org/p/{$v}.json",
'headers' => [],
'body' => null
);
}
};
$fulfillCallback = function(\Psr\Http\Message\ResponseInterface $response, $index) use (&$counter, $interval) {
$counter++;
if ($counter % $interval === 0) {
echo sprintf(
"Processed %s requests. Memory used: %s MB\n",
number_format($counter),
number_format(memory_get_peak_usage()/1024/1024, 3)
);
}
};
$rejectCallback = function($reason, $index) use (&$counter, $interval) {
$counter++;
if ($counter % $interval === 0) {
echo sprintf(
'Processed %s requests. Memory used: %s MB',
number_format($counter),
number_format(memory_get_peak_usage()/1024/1024, 3)
);
}
};
$client->bulkRequest($params($packageNames),$res,$concurrency,$fulfillCallback,$rejectCallback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment