Browse Source

[php-ngx] Remove globals vars in pgsql (#7312)

* [php-ngx] Remove globals vars in pgsql

* Update nginx
Joan Miquel 3 years ago
parent
commit
08a8a7779a

+ 70 - 0
frameworks/PHP/php-ngx/app-pg.php

@@ -0,0 +1,70 @@
+<?php
+require 'dbraw.php';
+DbRaw::init();
+
+function db()
+{
+    ngx_header_set('Content-Type', 'application/json');
+
+    DbRaw::$random->execute([mt_rand(1, 10000)]);
+    echo json_encode(DbRaw::$random->fetch(), JSON_NUMERIC_CHECK);
+}
+
+function query()
+{
+    ngx_header_set('Content-Type', 'application/json');
+
+    $query_count = 1;
+    $params      = (int) ngx::query_args()['q'];
+    if ($params > 1) {
+        $query_count = min($params, 500);
+    }
+    while ($query_count--) {
+        DbRaw::$random->execute([mt_rand(1, 10000)]);
+        $arr[] = DbRaw::$random->fetch();
+    }
+
+    echo json_encode($arr, JSON_NUMERIC_CHECK);
+}
+
+function update()
+{
+    ngx_header_set('Content-Type', 'application/json');
+
+    $query_count = 1;
+    $params      = (int) ngx::query_args()['q'];
+    if ($params > 1) {
+        $query_count = min($params, 500);
+    }
+    while ($query_count--) {
+
+        DbRaw::$random->execute([mt_rand(1, 10000)]);
+        $row = DbRaw::$random->fetch();
+        $row['randomNumber'] = mt_rand(1, 10000);
+
+        $worlds[] = $row;
+    }
+
+    DbRaw::update($worlds);
+
+    echo json_encode($worlds, JSON_NUMERIC_CHECK);
+}
+
+function fortune()
+{
+    ngx_header_set('Content-Type', 'text/html;charset=UTF-8');
+
+    DbRaw::$fortune->execute();
+
+    $arr    = DbRaw::$fortune->fetchAll(PDO::FETCH_KEY_PAIR);
+    $arr[0] = 'Additional fortune added at request time.';
+    asort($arr);
+
+    $html = '';
+    foreach ($arr as $id => $message) {
+        $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
+        $html .= "<tr><td>$id</td><td>$message</td></tr>";
+    }
+
+    echo "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>";
+}

+ 88 - 0
frameworks/PHP/php-ngx/dbraw.php

@@ -0,0 +1,88 @@
+<?php
+
+class DbRaw
+{
+    private static PDO $instance;
+    public static PDOStatement $db;
+    public static PDOStatement $fortune;
+    public static PDOStatement $random;
+    /**
+     * @var []PDOStatement
+     */
+    private static $update;
+
+    public static function init()
+    {
+        $pdo = new PDO(
+            'pgsql:host=tfb-database;dbname=hello_world',
+            'benchmarkdbuser',
+            'benchmarkdbpass',
+            [
+                PDO::ATTR_DEFAULT_FETCH_MODE  => PDO::FETCH_ASSOC,
+                PDO::ATTR_ERRMODE             => PDO::ERRMODE_EXCEPTION,
+                PDO::ATTR_EMULATE_PREPARES    => false
+            ]
+        );
+
+        self::$fortune   = $pdo->prepare('SELECT id,message FROM Fortune');
+        self::$random    = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
+        self::$instance  = $pdo;
+    }
+
+    /**
+     * Postgres bulk update
+     *
+     * @param array $worlds
+     * @return void
+     */
+    public static function update(array $worlds)
+    {
+        $rows = count($worlds);
+
+        if (!isset(self::$update[$rows])) {
+            $sql = 'UPDATE world SET randomNumber = CASE id'
+                . str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $rows)
+                . 'END WHERE id IN ('
+                . str_repeat('?::INTEGER,', $rows - 1) . '?::INTEGER)';
+
+            self::$update[$rows] = self::$instance->prepare($sql);
+        }
+
+        $val = [];
+        $keys = [];
+        foreach ($worlds as $world) {
+            $val[] = $keys[] = $world['id'];
+            $val[] = $world['randomNumber'];
+        }
+
+        self::$update[$rows]->execute([...$val, ...$keys]);
+    }
+
+    /**
+     * Alternative bulk update in Postgres
+     *
+     * @param array $worlds
+     * @return void
+     */
+    public static function update2(array $worlds)
+    {
+        $rows = count($worlds);
+
+        if (!isset(self::$update[$rows])) {
+            $sql = 'UPDATE world SET randomNumber = temp.randomNumber FROM (VALUES '
+                . implode(', ', array_fill(0, $rows, '(?::INTEGER, ?::INTEGER)')) .
+                ' ORDER BY 1) AS temp(id, randomNumber) WHERE temp.id = world.id';
+
+            self::$update[$rows] = self::$instance->prepare($sql);
+        }
+
+        $val = [];
+        foreach ($worlds as $world) {
+            $val[] = $world['id'];
+            $val[] = $world['randomNumber'];
+            //$update->bindParam(++$i, $world['id'], PDO::PARAM_INT);
+        }
+
+        self::$update[$rows]->execute($val);
+    }
+}

+ 1 - 1
frameworks/PHP/php-ngx/php-ngx-async.dockerfile

@@ -11,7 +11,7 @@ RUN apt-get update -yqq > /dev/null && \
 
 ADD ./ ./
 
-ENV NGINX_VERSION 1.21.4
+ENV NGINX_VERSION 1.21.6
 
 RUN git clone -b v0.0.26 --single-branch --depth 1 https://github.com/rryqszq4/ngx_php7.git > /dev/null
 

+ 1 - 1
frameworks/PHP/php-ngx/php-ngx-mysql.dockerfile

@@ -11,7 +11,7 @@ RUN apt-get update -yqq > /dev/null && \
 
 ADD ./ ./
 
-ENV NGINX_VERSION 1.21.4
+ENV NGINX_VERSION 1.21.6
 
 RUN git clone -b v0.0.26 --single-branch --depth 1 https://github.com/rryqszq4/ngx_php7.git > /dev/null
 

+ 2 - 2
frameworks/PHP/php-ngx/php-ngx-pgsql.dockerfile

@@ -11,7 +11,7 @@ RUN apt-get update -yqq > /dev/null && \
 
 ADD ./ ./
 
-ENV NGINX_VERSION 1.21.4
+ENV NGINX_VERSION 1.21.6
 
 RUN git clone -b v0.0.26 --single-branch --depth 1 https://github.com/rryqszq4/ngx_php7.git > /dev/null
 
@@ -26,7 +26,7 @@ RUN wget -q http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
             --add-module=/ngx_php7 > /dev/null && \
     make > /dev/null && make install > /dev/null
 
-RUN sed -i "s|mysql:|pgsql:|g" /app.php
+RUN sed -i "s|app.php|app-pg.php|g" /deploy/nginx.conf
 
 RUN export WORKERS=$(( 4 * $(nproc) )) && \
     sed -i "s|worker_processes  auto|worker_processes $WORKERS|g" /deploy/nginx.conf

+ 1 - 1
frameworks/PHP/php-ngx/php-ngx.dockerfile

@@ -10,7 +10,7 @@ RUN apt-get update -yqq > /dev/null && \
                     php8.1-cli php8.1-dev libphp8.1-embed php8.1-mysql nginx > /dev/null
 ADD ./ ./
 
-ENV NGINX_VERSION 1.21.4
+ENV NGINX_VERSION 1.21.6
 
 RUN git clone -b v0.0.26 --single-branch --depth 1 https://github.com/rryqszq4/ngx_php7.git > /dev/null