Browse Source

Ubiquity swoole update (#5063)

* Remove query groups in Swoole update method

* mix PDO and Swoole Coroutine Mysql for testing

* Update config.php

* remove pooling from SwooleFortunes

* only swoole for pooling!

* Update composer.json

* Update raw for multidb

* Update README.md
jcheron 5 years ago
parent
commit
68dfdecc97

+ 1 - 3
frameworks/PHP/ubiquity/.ubiquity/_swoole.php

@@ -9,12 +9,10 @@ if (! defined ( 'DS' )) {
 $config=include ROOT.'config/config.php';
 $sConfig= include __DIR__.\DS.'swoole-config.php';
 $config["sessionName"]=null;
-$config["database"]["wrapper"]="\\Ubiquity\\db\\providers\\swoole\\SwooleWrapper";
 $address=$sConfig['host'].':'.$sConfig['port'];
 $config ["siteUrl"] = 'http://'.$address;
 require ROOT . './../vendor/autoload.php';
-require ROOT.'config/services.php';
 $reactServer=new \Ubiquity\servers\swoole\SwooleServer();
 $reactServer->init($config, __DIR__);
-\Ubiquity\orm\DAO::initPooling($config,'default');
+require ROOT.'config/services.php';
 $reactServer->run($sConfig['host'],$sConfig['port']);

+ 4 - 4
frameworks/PHP/ubiquity/README.md

@@ -11,7 +11,7 @@ Tests are available with NginX server and Swoole platform.
 The tests are separated into 8 controllers:
 - `Json` for JSON response
   * [JSON](app/controllers/Json.php)
-- `Db` for database access with ORM
+- `Db` for database access with ORM (PDO Mysql)
   * [DB](app/controllers/Db.php)
   * [QUERY](app/controllers/Db.php)
   * [CACHED QUERY (not implemented)]()
@@ -20,13 +20,13 @@ The tests are separated into 8 controllers:
   * [FORTUNES](app/controllers/Fortunes.php)
 - `Plaintext` for plaintext response
   * [PLAINTEXT](app/controllers/Plaintext.php)
-- `Raw` for database access without ORM
+- `Raw` for database access without ORM (PDO Mysql)
   * [Raw](app/controllers/Raw.php)
 - `RawFortunes` without ORM and without template engine
   * [FORTUNES](app/controllers/RawFortunes.php)
-- `SwooleDb` for database access with ORM (Swoole)
+- `SwooleDb` for database access with ORM (Swoole coroutine Mysql)
   * [DB](app/controllers/SwooleDb.php)
-- `SwooleFortunes` for using the internal template engine (Swoole)
+- `SwooleFortunes` for using the internal template engine (PDO Mysql test)
   * [FORTUNES](app/controllers/SwooleFortunes.php)
 
 ## Important Libraries

+ 23 - 9
frameworks/PHP/ubiquity/app/config/config.php

@@ -1,16 +1,30 @@
 <?php
 return array(
 	"database" => [
-		"type" => "mysql",
-		"dbName" => "hello_world",
-		"serverName" => "tfb-database", // tfb-database
-		"port" => "3306",
-		"user" => "benchmarkdbuser", // benchmarkdbuser
-		"password" => "benchmarkdbpass", // benchmarkdbpass
-		"options" => [
-			\PDO::ATTR_PERSISTENT => true
+		'default'=>[
+			"wrapper"=>"\\Ubiquity\\db\\providers\\pdo\\PDOWrapper",
+			"type" => "mysql",
+			"dbName" => "hello_world",
+			"serverName" => "tfb-database", // tfb-database
+			"port" => "3306",
+			"user" => "benchmarkdbuser", // benchmarkdbuser
+			"password" => "benchmarkdbpass", // benchmarkdbpass
+			"options" => [
+				\PDO::ATTR_PERSISTENT => true
+			],
+			"cache"=>false
 		],
-		"cache" => false
+		'swoole'=>[
+			"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,
 	"debug" => false,

+ 2 - 1
frameworks/PHP/ubiquity/app/config/services.php

@@ -1,4 +1,5 @@
 <?php
 \Ubiquity\cache\CacheManager::startProd($config);
 \Ubiquity\controllers\Startup::$templateEngine = new \Ubiquity\views\engine\micro\MicroTemplateEngine();
-\Ubiquity\orm\DAO::setModelsDatabases([\models\Fortune::class=>'default',\models\World::class=>'default']);
+\Ubiquity\orm\DAO::setModelsDatabases(["models\\Fortune"=>"default","models\\World"=>"swoole"]);
+\Ubiquity\orm\DAO::initPooling($config,'swoole');

+ 1 - 1
frameworks/PHP/ubiquity/app/controllers/Raw.php

@@ -9,7 +9,7 @@ class Raw extends \Ubiquity\controllers\Controller {
 	
 	public function initialize() {
 		\header('Content-type: application/json');
-		$this->db=\Ubiquity\db\Database::start();
+		$this->db=\Ubiquity\db\Database::start('default');
 	}
 	
 	public function index() {

+ 1 - 1
frameworks/PHP/ubiquity/app/controllers/RawFortunes.php

@@ -8,7 +8,7 @@ class RawFortunes extends \Ubiquity\controllers\Controller {
 	}
 	
 	public function index() {
-		$fortunes = \Ubiquity\db\Database::start()->fetchAll('SELECT id,message FROM Fortune',\PDO::FETCH_KEY_PAIR);
+		$fortunes = \Ubiquity\db\Database::start('default')->fetchAll('SELECT id,message FROM Fortune',\PDO::FETCH_KEY_PAIR);
 		$fortunes[0] = 'Additional fortune added at request time.';
 		\asort($fortunes);
 		$this->loadView('Fortunes/raw-index.php', [

+ 13 - 33
frameworks/PHP/ubiquity/app/controllers/SwooleDb.php

@@ -3,7 +3,6 @@ namespace controllers;
 
 use Ubiquity\orm\DAO;
 use models\World;
-use Swoole\Coroutine as co;
 
 /**
  * Bench controller.
@@ -15,7 +14,7 @@ class SwooleDb extends \Ubiquity\controllers\Controller {
 	}
 	
 	public function index() {
-	    $dbInstance=DAO::pool();
+		$dbInstance=DAO::pool('swoole');
 		$world = DAO::getById(World::class, \mt_rand(1, 10000), false);
 		DAO::freePool($dbInstance);
 		echo \json_encode($world->_rest);
@@ -24,7 +23,7 @@ class SwooleDb extends \Ubiquity\controllers\Controller {
 	public function query($queries = 1) {
 		$worlds = [];
 		$queries = \min(\max($queries, 1), 500);
-		$dbInstance=DAO::pool();
+		$dbInstance=DAO::pool('swoole');
 		for ($i = 0; $i < $queries; ++ $i) {
 			$worlds[] = (DAO::getById(World::class, \mt_rand(1, 10000), false))->_rest;
 		}
@@ -33,35 +32,16 @@ class SwooleDb extends \Ubiquity\controllers\Controller {
 	}
 	
 	public function update($queries = 1) {
-	    \Swoole\Runtime::enableCoroutine();
-	    $queries = \min(\max($queries, 1), 500);
-	    $worlds = new co\Channel($queries);
-	    $count=\min(4,$queries);
-	    $rest=$queries%$count;
-	    $nb=($queries-$rest)/$count;
-	    for ($i = 0; $i < $nb; ++ $i) {
-	        $this->_update($count, $worlds);
-	    }
-	    if($rest>0){
-	        $this->_update($rest, $worlds);
-	    }
-	    $result=[];
-	    for($i=0;$i<$queries;++$i){
-	        $result[]=$worlds->pop();
-	    }
-	    echo \json_encode($result);
-	}
-	
-	private function _update($queries,$worlds) {
-        go(static function() use($queries,$worlds){
-	         $dbInstance=DAO::pool();
-	         for ($j = 0; $j < $queries; ++ $j) {
-	             $world = DAO::getById(World::class, \mt_rand(1, 10000), false);
-	             $world->randomNumber = \mt_rand(1, 10000);
-	             DAO::update($world);
-	             $worlds->push($world->_rest);
-	         }
-	         DAO::freePool($dbInstance);
-        });
+		$worlds = [];
+		$queries = \min(\max($queries, 1), 500);
+		$dbInstance=DAO::pool('swoole');
+		for ($i = 0; $i < $queries; ++ $i) {
+			$world = DAO::getById(World::class, \mt_rand(1, 10000), false);
+			$world->randomNumber = \mt_rand(1, 10000);
+			DAO::update($world);
+			$worlds[]=$world->_rest;
+		}
+		DAO::freePool($dbInstance);
+		echo \json_encode($worlds);
 	}
 }

+ 0 - 2
frameworks/PHP/ubiquity/app/controllers/SwooleFortunes.php

@@ -7,9 +7,7 @@ use Ubiquity\orm\DAO;
 class SwooleFortunes extends \Ubiquity\controllers\Controller {
 
 	public function index() {
-	    $dbInstance=DAO::pool();
 		$fortunes = DAO::getAll(Fortune::class, '', false);
-		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;

+ 1 - 1
frameworks/PHP/ubiquity/composer.json

@@ -1,7 +1,7 @@
 {
 	"require" : {
 		"php" : "^7.1",
-		"phpmv/ubiquity" : "dev-master"
+		"phpmv/ubiquity" : "2.4.x-dev"
 	},
 	"require-dev" : {
 		"monolog/monolog" : "^1.24",