Created
July 24, 2017 17:10
-
-
Save DaveRandom/039ebd49f32f9df606d889924b204e1d to your computer and use it in GitHub Desktop.
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 | |
include dirname(__DIR__) . "/vendor/autoload.php"; | |
use Amp\Process\Process; | |
use function Amp\Promise\all; | |
const COUNT = 8; | |
$passwords = []; | |
for ($i = 0; $i < COUNT; $i++) { | |
$passwords[] = \random_bytes(20); | |
} | |
$start = microtime(true); | |
foreach ($passwords as $password) { | |
\password_hash($password, PASSWORD_DEFAULT); | |
} | |
$syncTime = microtime(true) - $start; | |
function get_hash(string $password): \Generator | |
{ | |
/** @var Process $process */ | |
$process = yield Process::start("php hash.php"); | |
$stdIn = $process->getStdin(); | |
$stdIn->write($password); | |
$stdIn->close(); | |
$result = ''; | |
$stream = $process->getStdout(); | |
while ($chunk = yield $stream->read()) { | |
$result .= $chunk; | |
} | |
yield $process->join(); | |
return $result; | |
} | |
$asyncTime = 0; | |
Amp\Loop::run(function() use($passwords, &$asyncTime) { | |
$promises = []; | |
foreach ($passwords as $i => $password) { | |
$promises[] = new \Amp\Coroutine(get_hash($password)); | |
} | |
$start = microtime(true); | |
yield all($promises); | |
$asyncTime = microtime(true) - $start; | |
}); | |
?> | |
Hashing <?= COUNT ?> passwords synchronously took <?= $syncTime ?> seconds | |
Hashing <?= COUNT ?> passwords asynchronously took <?= $asyncTime ?> seconds | |
Async took <?= ($asyncTime / $syncTime) * 100 ?>% of the time that sync took |
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 echo \password_hash(\stream_get_contents(STDIN), PASSWORD_DEFAULT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment