Last active
December 19, 2015 22:58
-
-
Save joseph-montanez/6030755 to your computer and use it in GitHub Desktop.
async mysqli vs sync mysqli
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
{ | |
"name": "shabb/mysql", | |
"description": "async mysql", | |
"require": { | |
"react/react": "0.3.*" | |
}, | |
"license": "BSD", | |
"authors": [ | |
{ | |
"name": "Joseph Montanez", | |
"email": "[email protected]" | |
} | |
] | |
} |
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 | |
require __DIR__ . '/../vendor/autoload.php'; | |
date_default_timezone_set('America/Los_Angeles'); | |
$link1 = mysqli_connect('127.0.0.1', 'root', '', 'test', 3306); | |
$i = 0; | |
$mysql_populate = function ($request, $response) use ($link1) { | |
$link1->query("DROP TABLE IF EXISTS assets2"); | |
$link1->query(" | |
CREATE TABLE IF NOT EXISTS assets2 | |
( | |
id INT NOT NULL PRIMARY KEY, | |
uuid VARCHAR(20) NOT NULL, | |
value INT NOT NULL | |
) ENGINE=MYISAM; | |
"); | |
$text = "Done\n"; | |
for ($i = 0; $i < 100000; $i++) { | |
$uuid = uniqid(); | |
$value = rand(0, 100000); | |
$link1->query("INSERT DELAYED INTO assets2 VALUES({$i}, '{$uuid}', {$value})"); | |
$text .= $link1->error . PHP_EOL; | |
} | |
$headers = array('Content-Type' => 'text/plain'); | |
$response->writeHead(200, $headers); | |
$response->end($text); | |
}; | |
$mysql_sync = function ($request, $response) use ($link1) { | |
$result = $link1->query("SELECT assets2.* FROM assets2 ORDER BY rand() LIMIT 1"); | |
$asset = $result->fetch_row(); | |
$text = "Done\n" . var_export($asset, true);; | |
$headers = array('Content-Type' => 'text/plain'); | |
$response->writeHead(200, $headers); | |
$response->end($text); | |
}; | |
$mysql_async = function ($request, $response) use (&$link1, &$loop) { | |
$link1->query("SELECT * FROM assets2 ORDER BY rand() LIMIT 1", MYSQLI_ASYNC); | |
$all_links = array($link1); | |
$processed = 0; | |
$asset = false; | |
$timer = function () use (&$all_links, &$processed, &$link1, &$request, &$response, &$loop, &$timer, &$asset) { | |
$links = $errors = $reject = array(); | |
foreach ($all_links as $link) { | |
$links[] = $errors[] = $reject[] = $link; | |
} | |
if (!mysqli_poll($links, $errors, $reject, 0, 1)) { | |
$loop->addTimer(0.001, $timer); | |
return; | |
} | |
foreach ($links as $link) { | |
if ($result = $link->reap_async_query()) { | |
$asset = $result->fetch_row(); | |
if (is_object($result)) | |
mysqli_free_result($result); | |
} else die(sprintf("MySQLi Error: %s", mysqli_error($link))); | |
$processed++; | |
} | |
if ($processed < count($all_links)) { | |
$loop->addTimer(0.001, $timer); | |
} else { | |
$text = "Done\n" . var_export($asset, true); | |
$headers = array('Content-Type' => 'text/plain'); | |
$response->writeHead(200, $headers); | |
$response->end($text); | |
} | |
}; | |
$loop->addTimer(0.01, $timer); | |
}; | |
$app = function ($request, $response) use (&$i, $mysql_sync, $mysql_async, $mysql_populate) { | |
$i++; | |
$time = explode('.', microtime(1)); | |
echo date('F d G:i:s') . ':' . array_pop($time) . ' - ' . $request->getPath(), PHP_EOL; | |
if ($request->getPath() === '/mysql-sync') { | |
$mysql_sync($request, $response); | |
} | |
else if ($request->getPath() === '/mysql-async') { | |
$mysql_async($request, $response); | |
} | |
else if ($request->getPath() === '/mysql-populate') { | |
$mysql_populate($request, $response); | |
} else { | |
$text = "This is request number $i.\n"; | |
$headers = array('Content-Type' => 'text/plain'); | |
$response->writeHead(200, $headers); | |
$response->end($text); | |
} | |
}; | |
$loop = React\EventLoop\Factory::create(); | |
$socket = new React\Socket\Server($loop); | |
$http = new React\Http\Server($socket); | |
$http->on('request', $app); | |
$socket->listen(1337); | |
$loop->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment