Pārlūkot izejas kodu

Added swoole-postgres (#5973)

* Added swoole-postgres

* Remove none db test.

* Merge pg and mysql.
Bruce Dou 5 gadi atpakaļ
vecāks
revīzija
136c9e2747

+ 22 - 1
frameworks/PHP/swoole/benchmark_config.json

@@ -24,6 +24,27 @@
       "notes": "",
       "versus": "php"
     },
+    "postgres": {
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "database": "Postgres",
+      "framework": "None",
+      "language": "PHP",
+      "flavor": "PHP7",
+      "orm": "Raw",
+      "platform": "swoole",
+      "webserver": "none",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Swoole-postgres",
+      "notes": "",
+      "versus": "php"
+    },
     "no-async": {
       "db_url": "/db",
       "query_url": "/db?queries=",
@@ -41,7 +62,7 @@
       "webserver": "none",
       "os": "Linux",
       "database_os": "Linux",
-      "display_name": "Swoole-postgres",
+      "display_name": "Swoole-noasync",
       "notes": "Without async db pool connection",
       "versus": "php"
     }

+ 1 - 1
frameworks/PHP/swoole/swoole-no-async.dockerfile

@@ -1,4 +1,4 @@
-FROM php:7.3
+FROM php:7.4
 
 RUN pecl install swoole > /dev/null && \
     docker-php-ext-enable swoole

+ 12 - 5
frameworks/PHP/swoole/swoole-postgres.dockerfile

@@ -1,16 +1,23 @@
-FROM php:7.2
+FROM php:7.4
 
-ENV SWOOLE_VERSION=2.1.3
+ENV SWOOLE_VERSION=4.5.1
 
 RUN     apt-get update && apt-get install -y libpq-dev \
         && cd /tmp && curl -sSL "https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz" | tar xzf - \
         && cd swoole-src-${SWOOLE_VERSION} \
-        && phpize && ./configure --enable-coroutine --enable-coroutine-postgresql > /dev/null && make > /dev/null && make install > /dev/null \
+        && phpize && ./configure > /dev/null && make > /dev/null && make install > /dev/null \
         && docker-php-ext-enable swoole
+
+RUN     cd /tmp && curl -sSL "https://github.com/swoole/ext-postgresql/archive/v${SWOOLE_VERSION}.tar.gz" | tar xzf - \
+        && cd ext-postgresql-${SWOOLE_VERSION} \
+        && phpize && ./configure > /dev/null && make > /dev/null && make install > /dev/null \
+        && docker-php-ext-enable swoole_postgresql
  
 WORKDIR /swoole
+
 COPY swoole-server.php swoole-server.php
+RUN sed -i "s|_postgres||g" swoole-server.php
+
 COPY php.ini /usr/local/etc/php/
 
-CMD sed -i 's|NUMCORES|'"$(nproc)"'|g' swoole-server.php && \
-    php swoole-server.php
+CMD php swoole-server.php

+ 3 - 1
frameworks/PHP/swoole/swoole-server-noasync.php

@@ -6,7 +6,9 @@ use Swoole\Http\Response;
 
 $server = new swoole_http_server('0.0.0.0', 8080, SWOOLE_BASE);
 $server->set([
-    'worker_num' => swoole_cpu_num()
+    'worker_num' => swoole_cpu_num() * 2,
+    'log_file' => '/dev/null',
+    'log_level' => 5,
 ]);
 
 /**

+ 133 - 12
frameworks/PHP/swoole/swoole-server.php

@@ -5,16 +5,18 @@ use Swoole\Http\Response;
 
 $server = new swoole_http_server('0.0.0.0', 8080, SWOOLE_BASE);
 $server->set([
-    'worker_num' => swoole_cpu_num()
+    'worker_num' => swoole_cpu_num() * 2,
+    'log_file' => '/dev/null',
+    'log_level' => 5,
 ]);
 
-$pool = new \DatabasePool('mysql');
+$pool = new \DatabasePool('postgres');
 
 /**
  * On start of the PHP worker. One worker per server process is started.
  */
 $server->on('workerStart', function ($srv) use ($pool) {
-	$pool->init(\intdiv(512, $srv->setting['worker_num']));
+    $pool->init(\intdiv(512, $srv->setting['worker_num']));
 });
 
 /**
@@ -25,7 +27,121 @@ $server->on('workerStart', function ($srv) use ($pool) {
  *
  * @return string
  */
-$db = function (int $queries = 0) use ($pool): string {
+$db_postgres = function (int $queries = 0) use ($pool): string {
+    $db = $pool->get();
+    // Read number of queries to run from URL parameter
+    $query_count = 1;
+    if ($queries > 1) {
+        $query_count = $queries > 500 ? 500 : $queries;
+    }
+
+    // Create an array with the response string.
+    $arr = [];
+
+    $db->prepare('s', 'SELECT id, randomnumber FROM World WHERE id = $1');
+
+    // For each query, store the result set values in the response array
+    while ($query_count--) {
+        $id = mt_rand(1, 10000);
+        $res = $db->execute('s', [$id]);
+        $ret = $db->fetchAll($res);
+        // Store result in array.
+        $arr[] = ['id' => $id, 'randomnumber' => $ret[0]['randomnumber']];
+    }
+
+    // Use the PHP standard JSON encoder.
+    // http://www.php.net/manual/en/function.json-encode.php
+    if ($queries === -1) {
+        $arr = $arr[0];
+    }
+
+    $pool->put($db);
+
+    return \json_encode($arr, JSON_NUMERIC_CHECK);
+};
+
+/**
+ * The Fortunes test
+ *
+ * @param string $database_type
+ *
+ * @return string
+ */
+$fortunes_postgres = function () use ($pool): string {
+    $db = $pool->get();
+
+    $fortune = [];
+
+    $db->prepare('f', 'SELECT id, message FROM Fortune');
+    $res = $db->execute('f', []);
+    $arr = $db->fetchAll($res);
+
+    foreach ($arr as $row) {
+        $fortune[$row['id']] = $row['message'];
+    }
+    $fortune[0] = 'Additional fortune added at request time.';
+    \asort($fortune);
+
+    $html = '';
+    foreach ($fortune as $id => $message) {
+        $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
+        $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
+    }
+
+    $pool->put($db);
+
+    return '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'
+            .$html.
+            '</table></body></html>';
+};
+
+/**
+ * The Updates test
+ *
+ * @param string $database_type
+ * @param int $queries
+ *
+ * @return string
+ */
+$updates_postgres = function (int $queries = 0) use ($pool): string {
+    $db = $pool->get();
+
+    $query_count = 1;
+    if ($queries > 1) {
+        $query_count = $queries > 500 ? 500 : $queries;
+    }
+
+    $arr = [];
+
+    $db->prepare('us', 'SELECT randomnumber FROM World WHERE id = $1');
+    $db->prepare('uu', 'UPDATE World SET randomnumber = $1 WHERE id = $2');
+
+    while ($query_count--) {
+        $id = mt_rand(1, 10000);
+        $randomNumber = mt_rand(1, 10000);
+        $res = $db->execute('us', [$id]);
+        $ret = $db->fetchAll($res);
+        // Store result in array.
+        $world = ['id' => $id, 'randomnumber' => $ret[0]['randomnumber']];
+        $world['randomnumber'] = $randomNumber;
+        $res = $db->execute('uu', [$randomNumber, $id]);
+        $arr[] = $world;
+    }
+
+    $pool->put($db);
+
+    return \json_encode($arr, JSON_NUMERIC_CHECK);
+};
+
+/**
+ * The DB test
+ *
+ * @param string $database_type
+ * @param int $queries
+ *
+ * @return string
+ */
+$db_mysql = function (int $queries = 0) use ($pool): string {
     $db = $pool->get();
 
     // Read number of queries to run from URL parameter
@@ -66,12 +182,14 @@ $db = function (int $queries = 0) use ($pool): string {
  *
  * @return string
  */
-$fortunes = function () use ($pool): string {
+$fortunes_mysql = function () use ($pool): string {
     $db = $pool->get();
 
     $fortune = [];
+    
     $db->fortune_test = $db->fortune_test ?? $db->prepare('SELECT id, message FROM Fortune');
     $arr = $db->fortune_test->execute();
+
     foreach ($arr as $row) {
         $fortune[$row['id']] = $row['message'];
     }
@@ -99,7 +217,7 @@ $fortunes = function () use ($pool): string {
  *
  * @return string
  */
-$updates = function (int $queries = 0) use ($pool): string {
+$updates_mysql = function (int $queries = 0) use ($pool): string {
     $db = $pool->get();
 
     $query_count = 1;
@@ -223,15 +341,18 @@ class DatabasePool
 
     private function createDbInstance()
     {
-        if ($this->type === 'mysql') {
-            $db = new Swoole\Coroutine\Mysql;
-        }
         if ($this->type === 'postgres') {
             $db = new Swoole\Coroutine\PostgreSql;
+            if ($db->connect("host={$this->server['host']} port=5432 dbname={$this->server['database']} user={$this->server['user']} password={$this->server['password']}")){
+                return $db;
+            }
+        } else if($this->type === 'mysql') {
+            $db = new Swoole\Coroutine\Mysql;
+            if ($db->connect($this->server)){
+                return $db;
+            }
         }
-        if ($db->connect($this->server)){
-            return $db;
-        }
+        
         return false;
     }
 

+ 4 - 2
frameworks/PHP/swoole/swoole.dockerfile

@@ -3,10 +3,12 @@ FROM php:7.4
 RUN pecl install swoole > /dev/null && \
     docker-php-ext-enable swoole
 
-RUN docker-php-ext-install pdo_mysql > /dev/null
-
 WORKDIR /swoole
+
 COPY swoole-server.php swoole-server.php
+RUN sed -i "s|DatabasePool('postgres|DatabasePool('mysql|g" swoole-server.php
+RUN sed -i "s|_mysql||g" swoole-server.php
+
 COPY php.ini /usr/local/etc/php/
 
 CMD php swoole-server.php