Bläddra i källkod

Update mixphp-workerman-pgsql orm to raw (#6823)

* feat: add mixphp

* update /fortunes views and mixphp-workerman.dockerfile

* Add bin/*.php

* update dockerfile

* update mixphp-workerman.dockerfile

* Add mixphp-workerman-pgsql

* update views/fortunes.php

* update fortunes()

* update conn pool $maxOpen

* orm switch to object

* update orm switch to object

* Remove useless code

* debug build docker

* build dockerfile remove > /dev/null

* handle false & update routes

* Add 500 handle, close swoole coroutine

* Update dockerfile

* Update dockerfile

* Update dockerfile

* Update dockerfile

* Update dockerfile

* Update dockerfile

* Update readme

* Enable swoole coroutine

* disable swoole coroutine

* enable swoole coroutine

* Update pool

* disable swoole coroutine

* Update ubuntu version

* Update composer install

* Remove workerman-mysql json_url plaintext_url

* Update start params

* Add phpfpm

* Add phpfpm

* Add phpfpm

* Update mixphp.dockerfile

* Update mixphp.dockerfile

* Update mixphp.dockerfile

* Update dockerfile

* Update swoole.php

* Update db options

* Update db options

* Update mixphp-workerman-pgsql orm to raw

* Update mixphp-workerman-pgsql orm to raw

* Update mixphp-workerman-pgsql orm to raw

* Update mixphp-workerman-pgsql orm to raw

* Update mixphp-workerman-pgsql orm to raw

* Modify the method of obtaining parameters

* Modify the method of obtaining parameters
LIU JIAN 4 år sedan
förälder
incheckning
8acd997fa6

+ 1 - 1
frameworks/PHP/mixphp/benchmark_config.json

@@ -83,7 +83,7 @@
         "framework": "MixPHP",
         "language": "PHP",
         "flavor": "PHP8",
-        "orm": "micro",
+        "orm": "raw",
         "platform": "workerman",
         "webserver": "None",
         "os": "Linux",

+ 2 - 2
frameworks/PHP/mixphp/config.toml

@@ -62,7 +62,7 @@ classification = "Fullstack"
 database = "Postgres"
 database_os = "Linux"
 os = "Linux"
-orm = "micro"
+orm = "raw"
 platform = "workerman"
 webserver = "None"
-versus = "workerman"
+versus = "workerman"

+ 1 - 1
frameworks/PHP/mixphp/mixphp-workerman-pgsql.dockerfile

@@ -15,7 +15,7 @@ COPY php-jit.ini /etc/php/8.0/cli/php.ini
 ADD ./ /mixphp
 WORKDIR /mixphp
 
-RUN sed -i "s|'mysql:host|'pgsql:host|g" /mixphp/src/Container/DB.php
+RUN sed -i "s|Benchmark();|BenchmarkRaw();|g" /mixphp/routes/index.php
 
 RUN curl -sSL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
 RUN composer install --no-dev --classmap-authoritative --quiet > /dev/null

+ 1 - 0
frameworks/PHP/mixphp/routes/index.php

@@ -1,6 +1,7 @@
 <?php
 
 use App\Controller\Benchmark;
+use App\Controller\BenchmarkRaw;
 
 return function (Mix\Vega\Engine $vega) {
     $benchmark = new Benchmark();

+ 19 - 10
frameworks/PHP/mixphp/src/Controller/Benchmark.php

@@ -42,18 +42,14 @@ class Benchmark
     public function query(Context $ctx)
     {
         $queryCount = 1;
-        $q = (int)$ctx->query('q');
+        $q = static::getQuery($ctx);
         if ($q > 1) {
             $queryCount = min($q, 500);
         }
 
         $arr = [];
         while ($queryCount--) {
-            $id = mt_rand(1, 10000);
-            $ret = DB::instance()->raw('SELECT id,randomNumber FROM World WHERE id=?', $id)->first();
-            if (!$ret) {
-                continue;
-            }
+            $ret = DB::instance()->raw('SELECT id,randomNumber FROM World WHERE id=?', mt_rand(1, 10000))->first();
             $arr[] = $ret;
         }
 
@@ -80,7 +76,7 @@ class Benchmark
     public function update(Context $ctx)
     {
         $queryCount = 1;
-        $q = (int)$ctx->query('q');
+        $q = static::getQuery($ctx);
         if ($q > 1) {
             $queryCount = min($q, 500);
         }
@@ -89,9 +85,6 @@ class Benchmark
         while ($queryCount--) {
             $id = mt_rand(1, 10000);
             $ret = DB::instance()->raw('SELECT id,randomNumber FROM World WHERE id=?', $id)->first();
-            if (!$ret) {
-                continue;
-            }
             DB::instance()->exec('UPDATE World SET randomNumber=? WHERE id=?', $ret->randomNumber = mt_rand(1, 10000), $id);
             $arr[] = $ret;
         }
@@ -100,4 +93,20 @@ class Benchmark
         $ctx->string(200, json_encode($arr));
     }
 
+    /**
+     * @param Context $ctx
+     * @return int
+     */
+    protected static function getQuery(Context $ctx): int
+    {
+        $request = $ctx->request;
+        if ($request instanceof \Swoole\Http\Request) {
+            return (int)($request->get['q'] ?? '');
+        } elseif ($request instanceof \Workerman\Protocols\Http\Request) {
+            return (int)$request->get('q');
+        } else {
+            return (int)$ctx->query('q');
+        }
+    }
+
 }

+ 154 - 0
frameworks/PHP/mixphp/src/Controller/BenchmarkRaw.php

@@ -0,0 +1,154 @@
+<?php
+
+namespace App\Controller;
+
+use Mix\Vega\Context;
+
+class BenchmarkRaw
+{
+
+    public function init()
+    {
+        global $world, $fortune, $update;
+        if (isset($world)) {
+            return;
+        }
+
+        $pdo = new \PDO(
+            'pgsql:host=tfb-database;dbname=hello_world',
+            'benchmarkdbuser',
+            'benchmarkdbpass',
+            [
+                \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
+            ]
+        );
+        $world = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
+        $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
+        $update = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
+    }
+
+    /**
+     * @param Context $ctx
+     */
+    public function json(Context $ctx)
+    {
+        $ctx->setHeader('Content-Type', 'application/json');
+        $ctx->string(200, json_encode(['message' => 'Hello, World!']));
+    }
+
+    /**
+     * @param Context $ctx
+     */
+    public function plaintext(Context $ctx)
+    {
+        $ctx->setHeader('Content-Type', 'text/plain; charset=utf-8');
+        $ctx->string(200, 'Hello, World!');
+    }
+
+    /**
+     * @param Context $ctx
+     */
+    public function db(Context $ctx)
+    {
+        $this->init();
+        global $world;
+
+        $world->execute([mt_rand(1, 10000)]);
+
+        $ctx->setHeader('Content-Type', 'application/json');
+        $ctx->string(200, json_encode($world->fetch()));
+    }
+
+    /**
+     * @param Context $ctx
+     */
+    public function query(Context $ctx)
+    {
+        $this->init();
+        global $world;
+
+        $queryCount = 1;
+        $q = static::getQuery($ctx);
+        if ($q > 1) {
+            $queryCount = min($q, 500);
+        }
+
+        $arr = [];
+        while ($queryCount--) {
+            $world->execute([mt_rand(1, 10000)]);
+            $arr[] = $world->fetch();
+        }
+
+        $ctx->setHeader('Content-Type', 'application/json');
+        $ctx->string(200, json_encode($arr));
+    }
+
+    /**
+     * @param Context $ctx
+     */
+    public function fortunes(Context $ctx)
+    {
+        $this->init();
+        global $fortune;
+
+        $fortune->execute();
+
+        $arr = $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>";
+        }
+
+        $ctx->string(200, "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>");
+    }
+
+    /**
+     * @param Context $ctx
+     */
+    public function update(Context $ctx)
+    {
+        $this->init();
+        global $world, $update;
+
+        $queryCount = 1;
+        $q = static::getQuery($ctx);
+        if ($q > 1) {
+            $queryCount = min($q, 500);
+        }
+
+        $arr = [];
+        while ($queryCount--) {
+            $id = mt_rand(1, 10000);
+            $world->execute([$id]);
+            $ret = $world->fetch();
+            $update->execute(
+                [$ret['randomNumber'] = mt_rand(1, 10000), $id]
+            );
+            $arr[] = $ret;
+        }
+
+        $ctx->setHeader('Content-Type', 'application/json');
+        $ctx->string(200, json_encode($arr));
+    }
+
+    /**
+     * @param Context $ctx
+     * @return int
+     */
+    protected static function getQuery(Context $ctx): int
+    {
+        $request = $ctx->request;
+        if ($request instanceof \Swoole\Http\Request) {
+            return (int)($request->get['q'] ?? '');
+        } elseif ($request instanceof \Workerman\Protocols\Http\Request) {
+            return (int)$request->get('q');
+        } else {
+            return (int)$ctx->query('q');
+        }
+    }
+
+}

+ 1 - 0
frameworks/PHP/mixphp/src/Vega.php

@@ -17,6 +17,7 @@ class Vega
     public static function new(): Engine
     {
         $vega = new Engine();
+        $vega->mode(Engine::FAST_MODE);
 
         // 500
         $vega->use(function (Context $ctx) {