浏览代码

[Ubiquity] add ngx raw version (#6054)

* add ngx files

* add composer

* fix composer typo

* init_worker_by_php_block usage

* try to fix composer autoloading pb

* Update _ngx.php

* Update _ngx.php

* Update _ngx.php

* fix dir pb

* fix composer pb

* add Db

* add other db urls

* add preloading

* add ngx_php in README

* start ngx-raw

* add raw files

* add raw view and default fetchmod

* add default content-type

* add Content-Type header

* remove fetchAssoc

* add json & plaintext

* remove Plaintext and Json

* [ci skip] try to fix PG_VERSION pb

* [ci skip] force pg 12

* [ci skip] use postgresql 13
J-C HERON 5 年之前
父节点
当前提交
768db25f2e

+ 0 - 1
frameworks/PHP/ubiquity/.ubiquity/_ngx.php

@@ -1,6 +1,5 @@
 <?php
 use Ubiquity\servers\ngx\NgxServer;
-
 if (! defined('DS')) {
 	define('DS', DIRECTORY_SEPARATOR);
 	define('ROOT', __DIR__ . \DS . '..' . \DS . 'app' . \DS);

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

@@ -24,7 +24,8 @@ return array(
 			"password" => "benchmarkdbpass", // benchmarkdbpass
 			"options" => [
 				\PDO::ATTR_EMULATE_PREPARES => false,
-				'quote' => ''
+				'quote' => '',
+				\PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
 			],
 			"cache" => false
 		],

+ 77 - 0
frameworks/PHP/ubiquity/app/controllers/DbRaw.php

@@ -0,0 +1,77 @@
+<?php
+namespace controllers;
+
+use controllers\utils\DbTrait;
+
+/**
+ * Bench controller.
+ */
+class DbRaw extends \Ubiquity\controllers\Controller {
+	use DbTrait;
+
+	protected static $statement;
+
+	protected static $uStatements;
+
+	/**
+	 *
+	 * @var \Ubiquity\db\Database
+	 */
+	protected static $db;
+
+	private static function prepareUpdate(int $count) {
+		$sql = 'UPDATE World SET randomNumber = CASE id' . \str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $count) . 'END WHERE id IN (' . \str_repeat('?::INTEGER,', $count - 1) . '?::INTEGER)';
+		return self::$uStatements[$count] = self::$db->prepareStatement($sql);
+	}
+
+	public function __construct() {}
+
+	public static function warmup(\Ubiquity\db\Database $db) {
+		self::$db = $db;
+		self::$statement = $db->prepareStatement('SELECT id,randomNumber FROM World WHERE id=?');
+	}
+
+	public function initialize() {
+		\Ubiquity\utils\http\UResponse::setContentType('application/json');
+	}
+
+	public function index() {
+		self::$statement->execute([
+			\mt_rand(1, 10000)
+		]);
+		echo \json_encode(self::$statement->fetch());
+	}
+
+	public function query($queries = 1) {
+		$worlds = [];
+		$count = $this->getCount($queries);
+		while ($count --) {
+			self::$statement->execute([
+				\mt_rand(1, 10000)
+			]);
+			$worlds[] = self::$statement->fetch();
+		}
+		echo \json_encode($worlds);
+	}
+
+	public function update($queries = 1) {
+		$worlds = [];
+		$keys = $values = [];
+		$count = $this->getCount($queries);
+		for ($i = 0; $i < $count; ++ $i) {
+			$values[] = $keys[] = $id = \mt_rand(1, 10000);
+			self::$statement->execute([
+				$id
+			]);
+			$row = self::$statement->fetch();
+
+			$values[] = $row['randomNumber'] = \mt_rand(1, 10000);
+			$worlds[] = $row;
+		}
+		(self::$uStatements[$count] ?? self::prepareUpdate($count))->execute([
+			...$values,
+			...$keys
+		]);
+		echo \json_encode($worlds);
+	}
+}

+ 26 - 0
frameworks/PHP/ubiquity/app/controllers/FortunesRaw.php

@@ -0,0 +1,26 @@
+<?php
+namespace controllers;
+
+class FortunesRaw extends \Ubiquity\controllers\SimpleViewAsyncController {
+
+	protected static $statement;
+
+	public static function warmup(\Ubiquity\db\Database $db) {
+		self::$statement = $db->prepareStatement('SELECT id,message FROM Fortune');
+	}
+
+	public function initialize() {
+		\Ubiquity\utils\http\UResponse::setContentType('text/html', 'utf-8');
+	}
+
+	public function index() {
+		self::$statement->execute();
+		$fortunes = self::$statement->fetchAll(\PDO::FETCH_KEY_PAIR);
+		$fortunes[0] = 'Additional fortune added at request time.';
+		\asort($fortunes);
+		$this->loadView('Fortunes/raw.php', [
+			'fortunes' => $fortunes
+		]);
+	}
+}
+

+ 4 - 0
frameworks/PHP/ubiquity/app/views/Fortunes/raw.php

@@ -0,0 +1,4 @@
+<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>
+<?php foreach ($fortunes as $id => $value) : ?>
+<tr><td><?= $id ?></td><td><?= htmlspecialchars($value) ?></td></tr>
+<?php endforeach ?></table></body></html>

+ 21 - 0
frameworks/PHP/ubiquity/benchmark_config.json

@@ -201,6 +201,27 @@
         "display_name": "ubiquity-ngx",
         "notes": "",
         "versus": "ngx_php"
+      },
+      "ngx-raw": {
+        "db_url": "/DbRaw",
+        "query_url": "/DbRaw/query/",
+        "fortune_url": "/FortunesRaw",
+        "update_url": "/DbRaw/update/",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Fullstack",
+        "database": "Postgres",
+        "framework": "Ubiquity",
+        "language": "PHP",
+        "flavor": "PHP7",
+        "orm": "raw",
+        "platform": "ngx_php",
+        "webserver": "nginx",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "ubiquity-ngx-raw",
+        "notes": "",
+        "versus": "ngx_php"
       }
     }
   ]

+ 11 - 0
frameworks/PHP/ubiquity/deploy/conf/ngx/pgsql/raw/ngxServices.php

@@ -0,0 +1,11 @@
+<?php
+\Ubiquity\cache\CacheManager::startProd($config);
+
+\Ubiquity\cache\CacheManager::warmUpControllers([
+	\controllers\DbRaw::class,
+	\controllers\FortunesRaw::class
+]);
+
+$db = \Ubiquity\db\Database::start('pgsql', $config);
+\controllers\DbRaw::warmup($db);
+\controllers\FortunesRaw::warmup($db);

+ 46 - 0
frameworks/PHP/ubiquity/ubiquity-ngx-raw.dockerfile

@@ -0,0 +1,46 @@
+FROM ubuntu:20.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 > /dev/null
+RUN apt-get update -yqq > /dev/null && \
+    apt-get install -yqq wget git unzip libxml2-dev cmake make systemtap-sdt-dev \
+                    zlibc zlib1g zlib1g-dev libpcre3 libpcre3-dev libargon2-0-dev libsodium-dev \
+                    php7.4 php7.4-common php7.4-dev libphp7.4-embed php7.4-pgsql nginx > /dev/null
+
+ADD ./ ./
+
+ENV NGINX_VERSION=1.19.2
+
+RUN git clone -b v0.0.24 --single-branch --depth 1 https://github.com/rryqszq4/ngx_php7.git > /dev/null
+
+RUN wget -q http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
+    tar -zxf nginx-${NGINX_VERSION}.tar.gz && \
+    cd nginx-${NGINX_VERSION} && \
+    export PHP_LIB=/usr/lib && \ 
+    ./configure --user=www-data --group=www-data \
+            --prefix=/nginx \
+            --with-ld-opt="-Wl,-rpath,$PHP_LIB" \
+            --add-module=/ngx_php7/third_party/ngx_devel_kit \
+            --add-module=/ngx_php7 > /dev/null && \
+    make > /dev/null && make install > /dev/null
+
+RUN apt-get install -yqq composer > /dev/null
+
+RUN composer config -g repo.packagist composer https://packagist.phpcomposer.com
+
+RUN composer require phpmv/ubiquity-ngx:dev-master --quiet
+
+RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
+
+RUN chmod 777 -R app/cache/*
+
+COPY /deploy/conf/ngx/pgsql/raw/ngxServices.php /app/config/ngxServices.php
+
+RUN echo "opcache.preload=/app/config/preloader.script.php" >> /deploy/conf/php.ini
+
+RUN export WORKERS=$(( 4 * $(nproc) )) && \
+    sed -i "s|worker_processes  auto|worker_processes $WORKERS|g" /deploy/conf/ngx/nginx.conf
+
+CMD /nginx/sbin/nginx -c /deploy/conf/ngx/nginx.conf