Created
September 4, 2020 23:26
-
-
Save missoxd/fc52585f451fa37faf3b2a1800e0bbfe to your computer and use it in GitHub Desktop.
Magento 2 deferred async web requests test
This file contains 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 | |
require __DIR__ . '/app/bootstrap.php'; | |
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); | |
$objectManager = $bootstrap->getObjectManager(); | |
$objectManager->get('\Magento\Framework\App\State')->setAreaCode('adminhtml'); | |
$registry = $objectManager->get('\Magento\Framework\Registry'); | |
$registry->register('isSecureArea', 'true'); | |
#################################################################################################### | |
## BEGIN | |
/** | |
* Magento 2 Async Operations. | |
* @see: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/async-operations.html | |
* | |
* Guzzle sends async web requests using curl_multi_* functions. | |
* @see: https://stackoverflow.com/a/35675265 | |
*/ | |
use Magento\Framework\HTTP\AsyncClientInterface; | |
use Magento\Framework\HTTP\AsyncClient\Request; | |
//Start the clock. | |
$start = microtime(true); | |
//Do deferred operations. | |
$firstSecondsEndpoint = 'https://httpstat.us/200?sleep=10000'; | |
$secondSecondsEndpoint = 'https://httpstat.us/201?sleep=10000'; | |
/** @var AsyncClientInterface $asyncClient */ | |
$asyncClient = $objectManager->get(AsyncClientInterface::class); | |
$asyncOperations = [ | |
$asyncClient->request(new Request($firstSecondsEndpoint, 'GET', ['Accept' => 'text/plain'], null)), | |
$asyncClient->request(new Request($secondSecondsEndpoint, 'GET', ['Accept' => 'text/plain'], null)) | |
]; | |
foreach ($asyncOperations as $asyncOperation) { | |
echo $asyncOperation->get()->getBody() . PHP_EOL; | |
} | |
//End clock time. | |
$end = microtime(true); | |
$total = ceil($end - $start); | |
echo "Execution time of script = {$total} sec"; | |
/* | |
Sample response. | |
200 OK | |
201 Created | |
Execution time of script = 12 sec | |
*/ | |
#################################################################################################### | |
## END | |
$registry->unregister('isSecureArea'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment