Browse Source

Ubiquity workerman mysqli (#5083)

* Remove Raw + add Workerman

* Update workerServices.php

* Fix charset

* Update swoole Fortunes url

* remove daemonize
jcheron 5 years ago
parent
commit
104c9695a6

+ 19 - 0
frameworks/PHP/ubiquity/.ubiquity/_workerman.php

@@ -0,0 +1,19 @@
+#!/usr/bin/env php
+<?php
+// workerman.php
+
+if (! defined ( 'DS' )) {
+	define ( 'DS', DIRECTORY_SEPARATOR );
+	define ( 'ROOT', __DIR__ . \DS .'..'.\DS. 'app' . \DS );
+}
+$config=include ROOT.'config/config.php';
+$sConfig= include __DIR__.\DS.'workerman-config.php';
+$config["sessionName"]=$sConfig["sessionName"];
+$address=$sConfig['host'].':'.$sConfig['port'];
+$config ["siteUrl"] = 'http://'.$address;
+require ROOT . './../vendor/autoload.php';
+require ROOT.'config/workerServices.php';
+$workerServer=new \Ubiquity\servers\workerman\WorkermanServer();
+$workerServer->init($config, __DIR__);
+$workerServer->setDefaultCount();
+$workerServer->run($sConfig['host'],$sConfig['port'],$sConfig['socket']??[]);

+ 5 - 0
frameworks/PHP/ubiquity/.ubiquity/workerman-config.php

@@ -0,0 +1,5 @@
+<?php
+return array(
+	"host" => "0.0.0.0",
+	"port" => 8080
+);

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

@@ -5,10 +5,10 @@ Ubiquity is a full-stack php framework, These tests involve:
 - the ORM part (Full)
 - the JSON serialization (native php)
 
-Tests are available with NginX server and Swoole platform.
+Tests are available with NginX server, Swoole and Workerman platforms.
 
 ## Test Type Implementation Source Code
-The tests are separated into 8 controllers:
+The tests are separated into 7 controllers:
 - `Json` for JSON response
   * [JSON](app/controllers/Json.php)
 - `Db` for database access with ORM (PDO Mysql)
@@ -20,14 +20,12 @@ 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 (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 coroutine Mysql)
-  * [DB](app/controllers/SwooleDb.php)
-- `SwooleFortunes` for using the internal template engine (PDO Mysql test)
-  * [FORTUNES](app/controllers/SwooleFortunes.php)
+- `SwooleDb` for database access with Swoole coroutine Mysql driver
+  * [SwooleDb](app/controllers/SwooleDb.php)
+- `Workerman` with Mysqli driver
+  * [WorkerDb](app/controllers/WorkerDb.php)
+- `AsyncFortunes` used with Swoole or Workerman
+  * [AsyncFortunes](app/controllers/AsyncFortunes.php)
 
 ## Important Libraries
 The tests were run with:
@@ -36,6 +34,8 @@ The tests were run with:
 * [nginx 1.14](http://nginx.org/)
 * [Swoole](https://www.swoole.com/)
 * [Ubiquity-swoole](https://github.com/phpMv/ubiquity-swoole)
+* [Workerman](https://github.com/walkor/Workerman)
+* [Ubiquity-workerman](https://github.com/phpMv/ubiquity-workerman)
 * [MySQL 5.7](https://dev.mysql.com/)
 
 

+ 11 - 0
frameworks/PHP/ubiquity/app/config/config.php

@@ -24,6 +24,17 @@ return array(
 			"password" => "benchmarkdbpass", // benchmarkdbpass
 			"options" => [],
 			"cache" => false
+		],
+		'worker'=>[
+			"wrapper"=>"\\Ubiquity\\db\\providers\\mysqli\\MysqliWrapper",
+			"type" => "mysql",
+			"dbName" => "hello_world",
+			"serverName" => "p:tfb-database", // tfb-database
+			"port" => "3306",
+			"user" => "benchmarkdbuser", // benchmarkdbuser
+			"password" => "benchmarkdbpass", // benchmarkdbpass
+			"options" => [],
+			"cache" => false
 		]
 	],
 	"test" => false,

+ 4 - 0
frameworks/PHP/ubiquity/app/config/workerServices.php

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

+ 1 - 1
frameworks/PHP/ubiquity/app/controllers/SwooleFortunes.php → frameworks/PHP/ubiquity/app/controllers/AsyncFortunes.php

@@ -4,7 +4,7 @@ namespace controllers;
 use models\Fortune;
 use Ubiquity\orm\DAO;
 
-class SwooleFortunes extends \Ubiquity\controllers\Controller {
+class AsyncFortunes extends \Ubiquity\controllers\Controller {
 
 	public function index() {
 		$fortunes = DAO::getAll(Fortune::class, '', false);

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

@@ -7,7 +7,7 @@ namespace controllers;
 class Plaintext extends \Ubiquity\controllers\Controller {
 	
 	public function initialize(){
-		\Ubiquity\utils\http\UResponse::setContentType('text/plain; charset=utf-8');
+		\Ubiquity\utils\http\UResponse::setContentType('text/plain','utf-8');
 	}
 	public function index() {
 		echo 'Hello, World!';

+ 0 - 46
frameworks/PHP/ubiquity/app/controllers/Raw.php

@@ -1,46 +0,0 @@
-<?php
-namespace controllers;
-
-/**
- * Raw Bench controller.
- */
-class Raw extends \Ubiquity\controllers\Controller {
-	protected $db;
-	
-	public function initialize() {
-		\header('Content-type: application/json');
-		$this->db=\Ubiquity\db\Database::start('default');
-	}
-	
-	public function index() {
-		echo \json_encode($this->getRandomWorld(\mt_rand(1, 10000)));
-	}
-	
-	public function query($queries = 1) {
-		$worlds = [];
-		$queries = \min(\max($queries, 1), 500);
-		for ($i = 0; $i < $queries; ++ $i) {
-			$worlds[] = $this->getRandomWorld(\mt_rand(1, 10000));
-		}
-		echo \json_encode($worlds);
-	}
-	
-	public function update($queries = 1) {
-		$worlds = [];
-		$queries = \min(\max($queries, 1), 500);
-		for ($i = 0; $i < $queries; ++ $i) {
-			$world = $this->getRandomWorld(\mt_rand(1, 10000));
-			$this->db->prepareAndExecuteUpdate('UPDATE World SET randomNumber = ? WHERE id = ?',[$world['randomNumber'] = \mt_rand(1, 10000),$world['id']]);
-			$worlds[] = $world;
-		}
-		echo \json_encode($worlds);
-	}
-	
-	private function getRandomWorld($id) {
-		return $this->db->prepareAndFetchOne('SELECT id,randomNumber FROM World WHERE id = ? LIMIT 1', [
-			$id
-		],\PDO::FETCH_ASSOC);
-	}
-}
-
-

+ 0 - 20
frameworks/PHP/ubiquity/app/controllers/RawFortunes.php

@@ -1,20 +0,0 @@
-<?php
-namespace controllers;
-
-class RawFortunes extends \Ubiquity\controllers\Controller {
-	
-	public function initialize() {
-		\Ubiquity\controllers\Startup::$templateEngine = new \Ubiquity\views\engine\micro\MicroTemplateEngine();
-	}
-	
-	public function index() {
-		$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', [
-			'fortunes' => $fortunes
-		]);
-	}
-}
-
-

+ 41 - 0
frameworks/PHP/ubiquity/app/controllers/WorkerDb.php

@@ -0,0 +1,41 @@
+<?php
+namespace controllers;
+
+use Ubiquity\orm\DAO;
+use models\World;
+
+/**
+ * Bench controller.
+ */
+class WorkerDb extends \Ubiquity\controllers\Controller {
+
+	public function initialize() {
+		\Ubiquity\utils\http\UResponse::setContentType('application/json');
+	}
+	
+	public function index() {
+		$world = DAO::getById(World::class, \mt_rand(1, 10000), false);
+		echo \json_encode($world->_rest);
+	}
+	
+	public function query($queries = 1) {
+		$worlds = [];
+		$queries = \min(\max($queries, 1), 500);
+		for ($i = 0; $i < $queries; ++ $i) {
+			$worlds[] = (DAO::getById(World::class, \mt_rand(1, 10000), false))->_rest;
+		}
+		echo \json_encode($worlds);
+	}
+	
+	public function update($queries = 1) {
+		$worlds = [];
+		$queries = \min(\max($queries, 1), 500);
+		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;
+		}
+		echo \json_encode($worlds);
+	}
+}

+ 24 - 22
frameworks/PHP/ubiquity/benchmark_config.json

@@ -25,34 +25,36 @@
         "notes": "",
         "versus": "php"
       },
-      "raw": {
-            "db_url": "/Raw",
-            "query_url": "/Raw/query/",
-            "fortune_url": "/RawFortunes",
-            "update_url": "/Raw/update/",
-            "port": 8080,
-            "approach": "Realistic",
-            "classification": "Fullstack",
-            "database": "MySQL",
-            "framework": "Ubiquity",
-            "language": "PHP",
-            "flavor": "PHP7",
-            "orm": "Raw",
-            "platform": "FPM/FastCGI",
-            "webserver": "nginx",
-            "os": "Linux",
-            "database_os": "Linux",
-            "display_name": "ubiquity-raw",
-            "notes": "",
-            "versus": "php"
-          },
+      "workerman": {
+        "json_url": "/Json",
+        "plaintext_url": "/Plaintext",
+        "db_url": "/WorkerDb",
+        "query_url": "/WorkerDb/query/",
+        "fortune_url": "/AsyncFortunes",
+        "update_url": "/WorkerDb/update/",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Fullstack",
+        "database": "MySQL",
+        "framework": "Ubiquity",
+        "language": "PHP",
+        "flavor": "PHP7",
+        "orm": "Full",
+        "platform": "workerman",
+        "webserver": "none",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "ubiquity-workerman",
+        "notes": "",
+        "versus": "workerman"
+      },
       "swoole": {
         "json_url": "/Json",
         "plaintext_url": "/Plaintext",
         "db_url": "/SwooleDb",
         "update_url": "/SwooleDb/update/",
         "query_url": "/SwooleDb/query/",
-        "fortune_url": "/SwooleFortunes",
+        "fortune_url": "/AsyncFortunes",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Fullstack",

+ 8 - 0
frameworks/PHP/ubiquity/deploy/conf/php-async.ini

@@ -0,0 +1,8 @@
+opcache.enable=1
+opcache.enable_cli=1
+opcache.validate_timestamps=0
+opcache.save_comments=0
+opcache.enable_file_override=1
+opcache.huge_code_pages=1
+
+mysqlnd.collect_statistics = Off

+ 0 - 2
frameworks/PHP/ubiquity/deploy/conf/php-cli.ini

@@ -1,2 +0,0 @@
-# Enable OPcache also for CLI scripts
-opcache.enable_cli = On

+ 0 - 2
frameworks/PHP/ubiquity/deploy/conf/php-swoole.ini

@@ -1,2 +0,0 @@
-opcache.enable_cli=1
-opcache.validate_timestamps=0

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

@@ -9,7 +9,7 @@ RUN cd /tmp && curl -sSL "https://github.com/swoole/swoole-src/archive/v${SWOOLE
 
 RUN docker-php-ext-install pdo_mysql pcntl > /dev/null
 
-COPY deploy/conf/php-swoole.ini /usr/local/etc/php/
+COPY deploy/conf/php-async.ini /usr/local/etc/php/
 
 ADD ./ /ubiquity
 WORKDIR /ubiquity

+ 36 - 0
frameworks/PHP/ubiquity/ubiquity-workerman.dockerfile

@@ -0,0 +1,36 @@
+  
+FROM ubuntu:19.04
+
+ARG DEBIAN_FRONTEND=noninteractive
+
+RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null
+RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
+RUN apt-get update -yqq > /dev/null && \
+    apt-get install -yqq php7.3 php7.3-common php7.3-cli php7.3-mysql  > /dev/null
+
+RUN apt-get install -yqq composer > /dev/null
+
+RUN apt-get install -y php-pear php-dev libevent-dev > /dev/null
+RUN printf "\n\n /usr/lib/x86_64-linux-gnu/\n\n\nno\n\n\n" | pecl install event > /dev/null && echo "extension=event.so" > /etc/php/7.3/cli/conf.d/event.ini
+
+COPY deploy/conf/php-async.ini /etc/php/7.3/cli/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-workerman:dev-master phpmv/ubiquity-mysqli:dev-master --quiet
+
+RUN php composer.phar install --optimize-autoloader --classmap-authoritative --no-dev --quiet
+
+RUN chmod 777 -R /ubiquity/.ubiquity/*
+
+CMD /ubiquity/vendor/bin/Ubiquity serve -t=workerman -p=8080 -h=0.0.0.0