|
@@ -3,7 +3,14 @@ $server = new swoole_http_server('0.0.0.0', 8080, SWOOLE_BASE);
|
|
$server->set(array(
|
|
$server->set(array(
|
|
'worker_num' => NUMCORES
|
|
'worker_num' => NUMCORES
|
|
));
|
|
));
|
|
-$server->on('request', function ($req, $res) {
|
|
|
|
|
|
+
|
|
|
|
+$pool = new MySQLPool();
|
|
|
|
+
|
|
|
|
+$server->on('workerStart', function () use (&$pool) {
|
|
|
|
+ $pool->set_host_ip();
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+$server->on('request', function ($req, $res) use ($pool) {
|
|
|
|
|
|
switch ($req->server['request_uri'])
|
|
switch ($req->server['request_uri'])
|
|
{
|
|
{
|
|
@@ -18,14 +25,7 @@ $server->on('request', function ($req, $res) {
|
|
break;
|
|
break;
|
|
|
|
|
|
case "/db":
|
|
case "/db":
|
|
- $db = new Swoole\Coroutine\Mysql;
|
|
|
|
- $server = [
|
|
|
|
- 'host' => co::gethostbyname('tfb-database'),
|
|
|
|
- 'user' => 'benchmarkdbuser',
|
|
|
|
- 'password' => 'benchmarkdbpass',
|
|
|
|
- 'database' => 'hello_world'
|
|
|
|
- ];
|
|
|
|
- $db->connect($server);
|
|
|
|
|
|
+ $db = $pool->get();
|
|
|
|
|
|
// Read number of queries to run from URL parameter
|
|
// Read number of queries to run from URL parameter
|
|
$query_count = 1;
|
|
$query_count = 1;
|
|
@@ -52,19 +52,14 @@ $server->on('request', function ($req, $res) {
|
|
if (count($arr) === 1)
|
|
if (count($arr) === 1)
|
|
$arr = $arr[0];
|
|
$arr = $arr[0];
|
|
|
|
|
|
|
|
+ $pool->put($db);
|
|
|
|
+
|
|
$res->header('Content-Type', 'application/json');
|
|
$res->header('Content-Type', 'application/json');
|
|
$res->end(json_encode($arr));
|
|
$res->end(json_encode($arr));
|
|
break;
|
|
break;
|
|
|
|
|
|
case "/fortunes":
|
|
case "/fortunes":
|
|
- $db = new Swoole\Coroutine\Mysql;
|
|
|
|
- $server = [
|
|
|
|
- 'host' => co::gethostbyname('tfb-database'),
|
|
|
|
- 'user' => 'benchmarkdbuser',
|
|
|
|
- 'password' => 'benchmarkdbpass',
|
|
|
|
- 'database' => 'hello_world' //;charset=utf8
|
|
|
|
- ];
|
|
|
|
- $db->connect($server);
|
|
|
|
|
|
+ $db = $pool->get();
|
|
|
|
|
|
$fortune = [];
|
|
$fortune = [];
|
|
// Define query
|
|
// Define query
|
|
@@ -80,19 +75,14 @@ $server->on('request', function ($req, $res) {
|
|
|
|
|
|
$html .= "</table></body></html>";
|
|
$html .= "</table></body></html>";
|
|
|
|
|
|
|
|
+ $pool->put($db);
|
|
|
|
+
|
|
$res->header('Content-Type', 'text/html; charset=utf-8');
|
|
$res->header('Content-Type', 'text/html; charset=utf-8');
|
|
$res->end($html);
|
|
$res->end($html);
|
|
break;
|
|
break;
|
|
|
|
|
|
case "/updates":
|
|
case "/updates":
|
|
- $db = new Swoole\Coroutine\Mysql;
|
|
|
|
- $server = [
|
|
|
|
- 'host' => co::gethostbyname('tfb-database'),
|
|
|
|
- 'user' => 'benchmarkdbuser',
|
|
|
|
- 'password' => 'benchmarkdbpass',
|
|
|
|
- 'database' => 'hello_world'
|
|
|
|
- ];
|
|
|
|
- $db->connect($server);
|
|
|
|
|
|
+ $db = $pool->get();
|
|
|
|
|
|
$query_count = 1;
|
|
$query_count = 1;
|
|
if (isset($req->get['queries']) && $req->get['queries'] > 0)
|
|
if (isset($req->get['queries']) && $req->get['queries'] > 0)
|
|
@@ -116,6 +106,8 @@ $server->on('request', function ($req, $res) {
|
|
$arr[] = $world;
|
|
$arr[] = $world;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ $pool->put($db);
|
|
|
|
+
|
|
$res->header('Content-Type', 'application/json');
|
|
$res->header('Content-Type', 'application/json');
|
|
$res->end(json_encode($arr));
|
|
$res->end(json_encode($arr));
|
|
break;
|
|
break;
|
|
@@ -124,3 +116,59 @@ $server->on('request', function ($req, $res) {
|
|
});
|
|
});
|
|
|
|
|
|
$server->start();
|
|
$server->start();
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Class MySQLPool
|
|
|
|
+ *
|
|
|
|
+ * Deal with the fact that Swoole 2.1.3 has no build in database pooling
|
|
|
|
+ */
|
|
|
|
+class MySQLPool
|
|
|
|
+{
|
|
|
|
+ protected $pool;
|
|
|
|
+ var $pool_count = 0;
|
|
|
|
+
|
|
|
|
+ var $server = [
|
|
|
|
+ 'host' => '',
|
|
|
|
+ 'user' => 'benchmarkdbuser',
|
|
|
|
+ 'password' => 'benchmarkdbpass',
|
|
|
|
+ 'database' => 'hello_world'
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ function __construct()
|
|
|
|
+ {
|
|
|
|
+ $this->pool = new \SplQueue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function set_host_ip()
|
|
|
|
+ {
|
|
|
|
+ if( empty( $this->server['host'] ) )
|
|
|
|
+ {
|
|
|
|
+ $tfb_database_ip = Swoole\Coroutine::gethostbyname('tfb-database');
|
|
|
|
+ $this->server['host'] = $tfb_database_ip;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function put($db)
|
|
|
|
+ {
|
|
|
|
+ $this->pool->push($db);
|
|
|
|
+ $this->pool_count++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function get()
|
|
|
|
+ {
|
|
|
|
+ if($this->pool_count > 0)
|
|
|
|
+ {
|
|
|
|
+ $this->pool_count--;
|
|
|
|
+ return $this->pool->pop();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // No idle connection to create a new connection
|
|
|
|
+ $db = new Swoole\Coroutine\Mysql;
|
|
|
|
+ $db->connect($this->server);
|
|
|
|
+
|
|
|
|
+ if ($db == false)
|
|
|
|
+ return false;
|
|
|
|
+ else
|
|
|
|
+ return $db;
|
|
|
|
+ }
|
|
|
|
+}
|