我正在使用Amp进行一些测试,并尝试了解它如何通过异步运行 SQL 查询来帮助加速它们。我认为我做错了什么,因为这个测试文件的结果非常令人失望,而不是我所期望的。有什么我做错了吗?
下面的代码给了我这样的结果,第一个数字是 Amp\Mysql 并且由于某种原因它要慢很多:
0.37159991264343
0.10906314849854
PHP代码:
<?php
require 'vendor/autoload.php';
require 'Timer.php';
$runThisManyTimes = 1000;
///////////////////////////////////////////////////////////
use Amp\Mysql\ConnectionConfig;
use Amp\Loop;
Loop::run(function() use ($runThisManyTimes) {
$timer = Timer::start();
$config = ConnectionConfig::fromString(
"host=127.0.0.1 user=test password=test db=test "
);
/** @var \Amp\Mysql\Pool $pool */
$pool = Amp\Mysql\pool($config);
/** @var \Amp\Mysql\Statement $statement */
$statement = yield $pool->prepare("SELECT * FROM accounts WHERE id = :id");
for ($i = 1; $i <= $runThisManyTimes; $i++) {
/** @var \Amp\Mysql\ResultSet $result */
$result = yield $statement->execute(['id' => '206e5903-98bd-4af5-8fb1-86a520e9a330']);
while (yield $result->advance()) {
$row = $result->getCurrent();
}
}
$timer->stop();
echo $timer->getSeconds();
Loop::stop();
});
echo PHP_EOL;
///////////////////////////////////////////////////////////
$timer = Timer::start();
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test', 'test', 'test');
$statement = $pdo->prepare("SELECT * FROM accounts WHERE id = :id");
for ($i = 1; $i <= $runThisManyTimes; $i++) {
$statement->execute(['id' => '206e5903-98bd-4af5-8fb1-86a520e9a330']);
$statement->fetch();
}
$timer->stop();
echo $timer->getSeconds();