Created
June 10, 2020 18:43
-
-
Save gjreasoner/6e13b1fe89b4d83a59fb3ed330550172 to your computer and use it in GitHub Desktop.
Guzzle Async Requests Methods/Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Unit; | |
use Tests\TestCase; | |
class ExampleTest extends TestCase | |
{ | |
public function testGuzzleBatch() | |
{ | |
$requests = function () { | |
yield new \GuzzleHttp\Psr7\Request('GET', 'http://localhost:8000/sleep/5000'); | |
foreach (array_reverse(range(1, 9)) as $timeout) { | |
yield new \GuzzleHttp\Psr7\Request('GET', 'http://localhost:8000/sleep/' . $timeout * 100); | |
} | |
yield new \GuzzleHttp\Psr7\Request('GET', 'http://localhost:8000/sleep/0'); | |
}; | |
$start = microtime(true); | |
$times = []; | |
\GuzzleHttp\Pool::batch( | |
new \GuzzleHttp\Client, | |
$requests(), | |
[ | |
'concurrency' => 50, | |
'fulfilled' => function (\Psr\Http\Message\ResponseInterface $response, $index) use ($start, &$times) { | |
$times[] = ['duration' => microtime(true) - $start, 'response' => (string)$response->getBody()]; | |
}, | |
'rejected' => function ($reason, $index) { | |
// Handle the failed request. | |
} | |
] | |
); | |
dd(microtime(true) - $start, $times); | |
} | |
public function testMultiExec() | |
{ | |
$start = microtime(true); | |
$multiCurl = array(); | |
$result = array(); | |
$mh = curl_multi_init(); | |
foreach (range(0, 20) as $i => $id) { | |
// URL from which data will be fetched | |
$fetchURL = 'http://localhost:8000/sleep/' . rand(10, $i * 100); | |
$multiCurl[$i] = curl_init(); | |
curl_setopt($multiCurl[$i], CURLOPT_URL, $fetchURL); | |
curl_setopt($multiCurl[$i], CURLOPT_HEADER, 0); | |
curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER, 1); | |
curl_multi_add_handle($mh, $multiCurl[$i]); | |
} | |
$index = null; | |
do { | |
curl_multi_exec($mh, $index); | |
} while ($index > 0); | |
foreach ($multiCurl as $k => $ch) { | |
$result[$k] = curl_multi_getcontent($ch); | |
curl_multi_remove_handle($mh, $ch); | |
} | |
curl_multi_close($mh); | |
dd(microtime(true) - $start, $result); | |
} | |
public function testGuzzleConcurrent() | |
{ | |
$start = microtime(true); | |
$promises[] = (new \GuzzleHttp\Client())->getAsync("http://localhost:8000/sleep/5000"); | |
foreach (array_reverse(range(1, 20)) as $timeout) { | |
$promises[] = (new \GuzzleHttp\Client())->getAsync("http://localhost:8000/sleep/" . ($timeout * 100)); | |
} | |
$responses = \GuzzleHttp\Promise\settle($promises)->wait(); | |
dd(microtime(true) - $start, collect($responses)->pluck('value')); | |
} | |
public function testRegular() | |
{ | |
$start = microtime(true); | |
file_get_contents('http://localhost:8000/sleep/100'); | |
file_get_contents('http://localhost:8000/sleep/100'); | |
file_get_contents('http://localhost:8000/sleep/100'); | |
dd(microtime(true) - $start); | |
} | |
public function testNew() | |
{ | |
$runs = range(0, 2); | |
$promises = (function () use ($runs) { | |
foreach ($runs as $run) { | |
// don't forget using generator | |
yield (new \GuzzleHttp\Client())->getAsync('http://localhost:8000/sleep/100'); | |
} | |
})(); | |
$start = microtime(true); | |
$times = []; | |
$eachPromise = new \GuzzleHttp\Promise\EachPromise($promises, [ | |
// how many concurrency we are use | |
'concurrency' => 4, | |
'fulfilled' => function (\GuzzleHttp\Psr7\Response $response) use ($start, &$times) { | |
$times[] = microtime(true) - $start; | |
}, | |
'rejected' => function ($reason) { | |
return 'rejected'; | |
} | |
]); | |
$eachPromise->promise()->wait(); | |
$times['finished'] = microtime(true) - $start; | |
dd($times); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment