/** * Execute the console command. * * @return void */ public function fire() { echo 'Iniciado' . PHP_EOL; $start = microtime(true); $totalUsers = User::count(); //cria o main loop $loop = \React\EventLoop\Factory::create(); //cria a conexão MySQL $connection = new \React\MySQL\Connection($loop, array('dbname' => $_ENV['DB_DATABASE'], 'user' => $_ENV['DB_USERNAME'], 'passwd' => $_ENV['DB_PASSWORD'])); $connection->connect(function () { }); $query1 = ' SELECT * FROM users LEFT JOIN companies ON users.company_id = companies.id LIMIT ' . $totalUsers / 2 . ' ;'; $query2 = ' SELECT * FROM users LEFT JOIN companies ON users.company_id = companies.id LIMIT ' . $totalUsers / 2 . ' OFFSET ' . $totalUsers / 2 . ' ;'; $this->getUsers($connection, $query1, $loop); $this->getUsers($connection, $query2, $loop); $loop->run(); echo 'Processados ' . count($this->users) . ' usuários' . PHP_EOL; echo 'Demorou ' . number_format((microtime(true) - $start) * 1000, 2, ',', '') . 'ms' . PHP_EOL; }
/** * @inheritDoc */ public function onSessionStart($session, $transport) { parent::onSessionStart($session, $transport); $connection = new React\MySQL\Connection($this->getLoop(), array('dbname' => 'thruway_auth_example', 'user' => 'root', 'passwd' => 'root', 'port' => 8889)); //connecting to mysql server, not required. $connection->connect(function () { }); $this->mysqlConnection = $connection; }
public function testInsertSimple() { $loop = \React\EventLoop\Factory::create(); $connection = new \React\MySQL\Connection($loop, array('dbname' => 'test', 'user' => 'test', 'passwd' => 'test')); $connection->connect(function () { }); $connection->query("insert into book (`name`) values('foo')", function ($command, $conn) use($loop) { $this->assertEquals(false, $command->hasError()); $this->assertEquals(1, $command->affectedRows); $this->assertEquals(3, $command->insertId); $loop->stop(); }); $loop->run(); }
public function testSelectAfterDelay() { $loop = \React\EventLoop\Factory::create(); $connection = new \React\MySQL\Connection($loop, array('dbname' => 'test', 'user' => 'test', 'passwd' => 'test')); $callback = function () use($connection, $loop) { $connection->query('select 1+1', function ($command, $conn) use($loop) { $this->assertEquals(false, $command->hasError()); $this->assertEquals([['1+1' => 2]], $command->resultRows); $loop->stop(); }); }; $timeoutCb = function () use($loop) { $loop->stop(); $this->fail('Test timeout'); }; $connection->connect(function ($err, $conn) use($callback, $loop, $timeoutCb) { $this->assertEquals(null, $err); $loop->addTimer(0.1, $callback); $loop->addTimer(1, $timeoutCb); }); $loop->run(); }
<?php require __DIR__ . '/init.php'; //create the main loop $loop = React\EventLoop\Factory::create(); //create a mysql connection for executing queries $connection = new React\MySQL\Connection($loop, array('dbname' => 'test', 'user' => 'test', 'passwd' => 'test')); //connecting to mysql server, not required. $connection->connect(function () { }); $connection->query('select * from book', function ($command, $conn) use($loop) { if ($command->hasError()) { //test whether the query was executed successfully //error $error = $command->getError(); // get the error object, instance of Exception. } else { $results = $command->resultRows; //get the results $fields = $command->resultFields; // get table fields } $loop->stop(); //stop the main loop. }); $loop->run();
#!/usr/bin/env php <?php require_once __DIR__ . "/../vendor/autoload.php"; use React\Http\Request; use React\Http\Response; use React\EventLoop\Factory; use Voidcontext\Arc\Reactor\App; use Voidcontext\Arc\Reactor\Server\Adapter\ReactHttpServer; $loop = Factory::create(); $connection = new React\MySQL\Connection($loop, array('dbname' => 'benchmark', 'user' => 'root')); $connection->connect(function () { }); $server = new ReactHttpServer($loop); $app = new App($server, ['port' => 1337]); $app->get('/', function (Request $request, Response $response) use($connection) { $connection->query("INSERT INTO product (name) values (?)", [uniqid("product")], function ($command, $conn) use($response) { if ($command->hasError()) { $response->writeHead(500); $response->end(); } else { $conn->query("SELECT LAST_INSERT_ID() as id", function ($cmd, $conn) use($response) { if ($cmd->hasError()) { $response->writeHead(500); $response->end(); } else { $response->writeHead(200, ["Content-Type" => "application/json"]); $response->write(json_encode([id => $cmd->resultRows[0]['id']])); $response->end(); } }); }