Browse Source

Update ubiquity swoole mysql (#5471)

* replace PDO with Swoole Async for query and fortune

* Fix typo

* Fix sed pb

* Add new db config

* Remove preloading with async (freeze)

* use Fortune class!

* Update README.md
jcheron 5 years ago
parent
commit
84315f7c2f

+ 12 - 5
frameworks/PHP/ubiquity/README.md

@@ -8,8 +8,8 @@ Ubiquity is a full-stack php framework, These tests involve:
 Tests are available with NginX server, Swoole and Workerman platforms.
 Tests are available with NginX server, Swoole and Workerman platforms.
 
 
 ## Test Type Implementation Source Code
 ## Test Type Implementation Source Code
-The tests are separated into 7 controllers:
-### Ubiquity + PDO pgsql
+The tests are separated into controllers:
+### Ubiquity + PDO Mysql
 - `Json` for JSON response
 - `Json` for JSON response
   * [JSON](app/controllers/Json.php)
   * [JSON](app/controllers/Json.php)
 - `Db` for database access with ORM (PDO Mysql)
 - `Db` for database access with ORM (PDO Mysql)
@@ -21,10 +21,17 @@ The tests are separated into 7 controllers:
   * [FORTUNES](app/controllers/Fortunes.php)
   * [FORTUNES](app/controllers/Fortunes.php)
 - `Plaintext` for plaintext response
 - `Plaintext` for plaintext response
   * [PLAINTEXT](app/controllers/Plaintext.php)
   * [PLAINTEXT](app/controllers/Plaintext.php)
-### Ubiquity Swoole + PDO Mysql
-- `SwooleDb` for database access with Swoole coroutine Mysql driver
+### Ubiquity Swoole + Mysql
+- `SwooleDbMy` for database access with PDO Mysql driver (update test only)
+  * [SwooleDbAsync](app/controllers/SwooleDbAsync.php)
+- `SwooleDbAsync` for database access with Swoole coroutine Mysql driver
+  * [SwooleDbAsync](app/controllers/SwooleDbAsync.php)
+- `SwooleFortunesAsync` used with Swoole coroutine Mysql driver
+  * [SwooleFortunes](app/controllers/SwooleFortunes.php)
+### Ubiquity Swoole + PostgreSQL
+- `SwooleDb` for database access with PDO pgsql driver
   * [SwooleDb](app/controllers/SwooleDb.php)
   * [SwooleDb](app/controllers/SwooleDb.php)
-- `SwooleFortunes` used with Swoole
+- `SwooleFortunes` used with PDO pgsql driver
   * [SwooleFortunes](app/controllers/SwooleFortunes.php)
   * [SwooleFortunes](app/controllers/SwooleFortunes.php)
 ### Ubiquity Workerman + PDO pgsql
 ### Ubiquity Workerman + PDO pgsql
 - `Workerman` with PDO pgsql driver
 - `Workerman` with PDO pgsql driver

+ 17 - 6
frameworks/PHP/ubiquity/app/config/config.php

@@ -1,8 +1,8 @@
 <?php
 <?php
 return array(
 return array(
 	"database" => [
 	"database" => [
-		'default'=>[
-			"wrapper"=>"\\Ubiquity\\db\\providers\\pdo\\PDOWrapper",
+		'default' => [
+			"wrapper" => "\\Ubiquity\\db\\providers\\pdo\\PDOWrapper",
 			"type" => "mysql",
 			"type" => "mysql",
 			"dbName" => "hello_world",
 			"dbName" => "hello_world",
 			"serverName" => "tfb-database", // tfb-database
 			"serverName" => "tfb-database", // tfb-database
@@ -12,10 +12,10 @@ return array(
 			"options" => [
 			"options" => [
 				\PDO::ATTR_PERSISTENT => true
 				\PDO::ATTR_PERSISTENT => true
 			],
 			],
-			"cache"=>false
+			"cache" => false
 		],
 		],
-		'pgsql'=>[
-			"wrapper"=>"\\Ubiquity\\db\\providers\\pdo\\PDOWrapper",
+		'pgsql' => [
+			"wrapper" => "\\Ubiquity\\db\\providers\\pdo\\PDOWrapper",
 			"type" => "pgsql",
 			"type" => "pgsql",
 			"dbName" => "hello_world",
 			"dbName" => "hello_world",
 			"serverName" => "tfb-database", // tfb-database
 			"serverName" => "tfb-database", // tfb-database
@@ -25,7 +25,18 @@ return array(
 			"options" => [
 			"options" => [
 				\PDO::ATTR_PERSISTENT => true
 				\PDO::ATTR_PERSISTENT => true
 			],
 			],
-			"cache"=>false
+			"cache" => false
+		],
+		'async' => [
+			"wrapper" => "\\Ubiquity\\db\\providers\\swoole\SwooleWrapper",
+			"type" => "mysql",
+			"dbName" => "hello_world",
+			"serverName" => "tfb-database", // tfb-database
+			"port" => 3306,
+			"user" => "benchmarkdbuser", // benchmarkdbuser
+			"password" => "benchmarkdbpass", // benchmarkdbpass
+			"options" => [],
+			"cache" => false
 		]
 		]
 	],
 	],
 	"test" => false,
 	"test" => false,

+ 38 - 0
frameworks/PHP/ubiquity/app/controllers/SwooleDbAsync.php

@@ -0,0 +1,38 @@
+<?php
+namespace controllers;
+
+use Ubiquity\orm\DAO;
+
+/**
+ * Bench controller.
+ */
+class SwooleDbAsync extends \Ubiquity\controllers\Controller {
+
+	public function initialize() {
+		\Ubiquity\utils\http\UResponse::setContentType('application/json');
+	}
+
+	public function index() {
+		$dbInstance = DAO::pool('async');
+
+		$world = DAO::executePrepared('world', [
+			'id' => \mt_rand(1, 10000)
+		]);
+		DAO::freePool($dbInstance);
+		echo \json_encode($world->_rest);
+	}
+
+	public function query($queries = 1) {
+		$queries = \min(\max($queries, 1), 500);
+		$worlds = [];
+		$dbInstance = DAO::pool('async');
+		for ($i = 0; $i < $queries; ++ $i) {
+			$worlds[] = (DAO::executePrepared('world', [
+				'id' => \mt_rand(1, 10000)
+			]))->_rest;
+		}
+		DAO::freePool($dbInstance);
+
+		echo \json_encode($worlds);
+	}
+}

+ 22 - 0
frameworks/PHP/ubiquity/app/controllers/SwooleFortunesAsync.php

@@ -0,0 +1,22 @@
+<?php
+namespace controllers;
+
+use Ubiquity\orm\DAO;
+use models\Fortune;
+
+class SwooleFortunesAsync extends \Ubiquity\controllers\SimpleViewAsyncController {
+
+	public function index() {
+		$dbInstance = DAO::pool('async');
+		$fortunes = DAO::executePrepared('fortune');
+		DAO::freePool($dbInstance);
+		$fortunes[] = (new Fortune())->setId(0)->setMessage('Additional fortune added at request time.');
+		\usort($fortunes, function ($left, $right) {
+			return $left->message <=> $right->message;
+		});
+		$this->loadView('Fortunes/index.php', [
+			'fortunes' => $fortunes
+		]);
+	}
+}
+

+ 20 - 3
frameworks/PHP/ubiquity/benchmark_config.json

@@ -72,10 +72,27 @@
         "versus": "swoole"
         "versus": "swoole"
       },
       },
       "swoole-mysql": {
       "swoole-mysql": {
-        "db_url": "/SwooleDb",
         "update_url": "/SwooleDbMy/update/",
         "update_url": "/SwooleDbMy/update/",
-        "query_url": "/SwooleDb/query/",
-        "fortune_url": "/SwooleFortunes",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Fullstack",
+        "database": "MYSQL",
+        "framework": "ubiquity",
+        "language": "PHP",
+        "flavor": "PHP7",
+        "orm": "Full",
+        "platform": "swoole",
+        "webserver": "none",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "ubiquity-swoole-mysql-pdo",
+        "notes": "",
+        "versus": "swoole"
+      },
+      "swoole-mysql-async": {
+        "db_url": "/SwooleDbAsync",
+        "query_url": "/SwooleDbAsync/query/",
+        "fortune_url": "/SwooleFortunesAsync",
         "port": 8080,
         "port": 8080,
         "approach": "Realistic",
         "approach": "Realistic",
         "classification": "Fullstack",
         "classification": "Fullstack",

+ 15 - 0
frameworks/PHP/ubiquity/deploy/swoole/swooleMysqlAsyncServices.php

@@ -0,0 +1,15 @@
+<?php
+\Ubiquity\cache\CacheManager::startProd($config);
+\Ubiquity\orm\DAO::setModelsDatabases([
+	'models\\Fortune' => 'async',
+	'models\\World' => 'async'
+]);
+\Ubiquity\cache\CacheManager::warmUpControllers([
+	'controllers\\SwooleDbAsync',
+	'controllers\\SwooleFortunesAsync'
+]);
+$swooleServer->on('workerStart', function ($srv) use (&$config) {
+	\Ubiquity\orm\DAO::initPooling($config, 'async', \intdiv(512, $srv->setting['worker_num']));
+	\Ubiquity\orm\DAO::prepareGetById('world', 'models\\World');
+	\Ubiquity\orm\DAO::prepareGetAll('fortune', 'models\\Fortune');
+});

+ 12 - 0
frameworks/PHP/ubiquity/deploy/swoole/swooleMysqlServices.php

@@ -0,0 +1,12 @@
+<?php
+\Ubiquity\cache\CacheManager::startProd($config);
+\Ubiquity\orm\DAO::setModelsDatabases([
+	'models\\World' => 'default'
+]);
+\Ubiquity\cache\CacheManager::warmUpControllers([
+	'controllers\\SwooleDbMy'
+]);
+$swooleServer->on('workerStart', function ($srv) use (&$config) {
+	\Ubiquity\orm\DAO::startDatabase($config, 'default');
+	\Ubiquity\orm\DAO::prepareGetById('world', 'models\\World');
+});

+ 0 - 1
frameworks/PHP/ubiquity/app/config/swooleServices.php → frameworks/PHP/ubiquity/deploy/swoole/swoolePgsqlServices.php

@@ -8,7 +8,6 @@
 	'controllers\\PlaintextAsync',
 	'controllers\\PlaintextAsync',
 	'controllers\\JsonAsync',
 	'controllers\\JsonAsync',
 	'controllers\\SwooleDb',
 	'controllers\\SwooleDb',
-	'controllers\\SwooleDbMy',
 	'controllers\\SwooleFortunes'
 	'controllers\\SwooleFortunes'
 ]);
 ]);
 $swooleServer->on('workerStart', function ($srv) use (&$config) {
 $swooleServer->on('workerStart', function ($srv) use (&$config) {

+ 37 - 0
frameworks/PHP/ubiquity/ubiquity-swoole-mysql-async.dockerfile

@@ -0,0 +1,37 @@
+FROM php:7.4
+
+RUN apt-get update > /dev/null
+
+RUN pecl install swoole-4.4.14 > /dev/null && \
+    docker-php-ext-enable swoole
+
+RUN docker-php-ext-install pdo_mysql pcntl > /dev/null
+
+COPY deploy/conf/php-async.ini /usr/local/etc/php/php.ini
+RUN echo "zend_extension=opcache.so" >> /usr/local/etc/php/php.ini
+
+ADD ./ /ubiquity
+WORKDIR /ubiquity
+
+RUN chmod -R 777 /ubiquity
+
+RUN ["chmod", "+x", "deploy/run/install-composer.sh"]
+
+RUN deploy/run/install-composer.sh
+
+RUN apt-get update -yqq > /dev/null && \
+    apt-get install -yqq git unzip > /dev/null
+
+RUN php composer.phar require phpmv/ubiquity-devtools:dev-master phpmv/ubiquity-swoole:dev-master --quiet
+
+RUN php composer.phar install --optimize-autoloader --classmap-authoritative --no-dev --quiet
+
+RUN chmod 777 -R /ubiquity/.ubiquity/*
+
+#RUN echo "opcache.preload=/ubiquity/app/config/preloader.script.php" >> /usr/local/etc/php/php.ini
+
+USER www-data
+
+COPY deploy/swoole/swooleMysqlAsyncServices.php app/config/swooleServices.php
+
+CMD /ubiquity/vendor/bin/Ubiquity serve -t=swoole -p=8080 -h=0.0.0.0

+ 1 - 1
frameworks/PHP/ubiquity/ubiquity-swoole-mysql.dockerfile

@@ -32,6 +32,6 @@ RUN echo "opcache.preload=/ubiquity/app/config/preloader.script.php" >> /usr/loc
 
 
 USER www-data
 USER www-data
 
 
-RUN sed -i "s|'pgsql'|'default'|g" /ubiquity/app/config/swooleServices.php
+COPY deploy/swoole/swooleMysqlServices.php app/config/swooleServices.php
 
 
 CMD /ubiquity/vendor/bin/Ubiquity serve -t=swoole -p=8080 -h=0.0.0.0
 CMD /ubiquity/vendor/bin/Ubiquity serve -t=swoole -p=8080 -h=0.0.0.0

+ 2 - 0
frameworks/PHP/ubiquity/ubiquity-swoole.dockerfile

@@ -34,4 +34,6 @@ RUN echo "opcache.preload=/ubiquity/app/config/preloader.script.php" >> /usr/loc
 
 
 USER www-data
 USER www-data
 
 
+COPY deploy/swoole/swoolePgsqlServices.php app/config/swooleServices.php
+
 CMD /ubiquity/vendor/bin/Ubiquity serve -t=swoole -p=8080 -h=0.0.0.0
 CMD /ubiquity/vendor/bin/Ubiquity serve -t=swoole -p=8080 -h=0.0.0.0