Skip to content

Instantly share code, notes, and snippets.

@chrisleavoy
Last active June 1, 2016 16:39
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 chrisleavoy/18b4032402d7b2d7f688655fe28eab60 to your computer and use it in GitHub Desktop.
Save chrisleavoy/18b4032402d7b2d7f688655fe28eab60 to your computer and use it in GitHub Desktop.
<?php
require_once 'vendor/autoload.php';
use Icicle\Loop;
/*
composer require amphp/dns
composer require icicleio/icicle
php test.php
example:
(makes 3 dns requires each having a 50% chance of 8 second timeout)
/usr/local/bin/php /Users/chris.leavoy/code/icicle/test2.php
start google.ca 1464794630.8973
start yahoo.com 1464794630.9166
start textnow.com 1464794630.9179
textnow.com timed out 8
stop textnow.com 7.9939210414886
yahoo.com timed out 7
stop yahoo.com 7.9953629970551
stop google.ca 8.0147550106049
total: 8.0236649513245
Process finished with exit code 0
*/
function resolve ($hostname, $x) {
$saneDefaults = [
'types' => \Amp\Dns\Record::A, 'server' => "8.8.8.{$x}", 'timeout' => $x * 500, 'tries' => 1,
'cache' => false
];
return \Amp\Dns\resolve($hostname, $saneDefaults);
}
/**
* Convert an Amp\Promise into an Icicle\Awaitable\Promise and schedule immediate resolution inside the Icicle loop
*
* @param \Amp\Promise $ampPromise
* @return \Icicle\Awaitable\Promise
*/
function adapt(\Amp\Promise $ampPromise)
{
$iciclePromise = new \Icicle\Awaitable\Promise(function (callable $resolve, callable $reject) use ($ampPromise) {
$ampPromise->when(function ($error, $result) use (&$resolve, &$reject) {
if ($error) {
$reject($error);
} else {
$resolve($result);
}
});
});
\Icicle\Loop\immediate(function() use ($ampPromise) {
\Amp\wait($ampPromise);
});
return $iciclePromise;
}
function analyze($pluginName)
{
$x = rand(7,8);
// inside plugin
try {
$ampPromise = resolve($pluginName, $x);
$iciclePromise = adapt($ampPromise);
$r = $iciclePromise->wait();
return $r;
} catch (\Exception $e) {
$o = "{$pluginName} timed out {$x}";
echo $o . "\n";
return [$o];
}
}
function analyzePlugin($pluginName)
{
$start = microtime(true);
echo("start $pluginName " . $start . "\n");
$r = analyze($pluginName);
echo("stop $pluginName " . (microtime(true) - $start) . "\n");
return $r;
}
function analyzePluginAsync($pluginName)
{
$d = new \Icicle\Awaitable\Deferred();
\Icicle\Loop\immediate(function() use ($d, $pluginName) {
$d->resolve(analyzePlugin($pluginName));
});
return $d->getPromise();
}
function analyzePluginsAsync()
{
$registeredPlugins = ['google.ca', 'yahoo.com', 'textnow.com'];
$promises = [];
foreach ($registeredPlugins as $pluginName) {
$promises []= analyzePluginAsync($pluginName);
}
$future = Icicle\Awaitable\all($promises);
$result = $future->wait();
return $result;
}
$start = microtime(true);
$result = analyzePluginsAsync();
echo "total: " . (microtime(true) - $start) . "\n";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment