Browse Source

[php] Simplify the code for Webman Workerman (#9474)

* Code optimization

* Remove maintainers
walkor 7 months ago
parent
commit
082b573713

+ 25 - 23
frameworks/PHP/webman/app/controller/Index.php

@@ -5,7 +5,10 @@ use support\Request;
 use support\bootstrap\Date;
 use support\bootstrap\Date;
 use support\bootstrap\db\Raw as Db;
 use support\bootstrap\db\Raw as Db;
 use support\Response;
 use support\Response;
-use PDO;
+use function json_encode;
+use function max;
+use function min;
+use function mt_rand;
 
 
 class Index
 class Index
 {
 {
@@ -29,7 +32,7 @@ class Index
     public function db()
     public function db()
     {
     {
         $statement = Db::$random;
         $statement = Db::$random;
-        $statement->execute([\mt_rand(1, 10000)]);
+        $statement->execute([mt_rand(1, 10000)]);
 
 
         return new Response(200, [
         return new Response(200, [
             'Content-Type' => 'application/json',
             'Content-Type' => 'application/json',
@@ -63,14 +66,11 @@ class Index
     {
     {
         $statement = Db::$random;
         $statement = Db::$random;
 
 
-        $query_count = 1;
-        if ((int) $q > 1) {
-            $query_count = \min($q, 500);
-        }
+        $query_count = min(max((int) $q, 1), 500);
 
 
         $arr = [];
         $arr = [];
         while ($query_count--) {
         while ($query_count--) {
-            $statement->execute([\mt_rand(1, 10000)]);
+            $statement->execute([mt_rand(1, 10000)]);
             $arr[] = $statement->fetch();
             $arr[] = $statement->fetch();
         }
         }
 
 
@@ -82,29 +82,31 @@ class Index
 
 
     public function updates(Request $request, $q = 1)
     public function updates(Request $request, $q = 1)
     {
     {
-        $random = Db::$random;
+        static $updates = [];
 
 
-        $query_count = 1;
-        if ((int) $q > 1) {
-            $query_count = \min($q, 500);
+        $random = Db::$random;
+        $pdo = Db::$pdo;
+        $count = min(max((int) $q, 1), 500);
+
+        $worlds = $keys = $values = [];
+        for ($i = 0; $i < $count; ++ $i) {
+            $values[] = $keys[] = $id = mt_rand(1, 10000);
+            $random->execute([$id]);
+            $row = $random->fetch();
+            $values[] = $row['randomNumber'] = mt_rand(1, 10000);
+            $worlds[] = $row;
         }
         }
-
-        $worlds = [];
-
-        while ($query_count--) {
-            $random->execute([\mt_rand(1, 10000)]);
-            $world = $random->fetch();
-            $world['randomNumber'] = \mt_rand(1, 10000);
-
-            $worlds[] = $world;
+        if (!isset($updates[$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)';
+            $updates[$count] = $pdo->prepare($sql);
         }
         }
-
-        Db::update($worlds);
+        $updates[$count]->execute([...$values, ...$keys]);
 
 
         return new Response(200, [
         return new Response(200, [
             'Content-Type' => 'application/json',
             'Content-Type' => 'application/json',
             'Date'         => Date::$date
             'Date'         => Date::$date
-        ], \json_encode($worlds));
+        ], json_encode($worlds));
+
     }
     }
 
 
 
 

+ 0 - 33
frameworks/PHP/webman/support/bootstrap/db/Raw.php

@@ -31,11 +31,6 @@ class Raw implements Bootstrap
 
 
     public static PDOStatement $random;
     public static PDOStatement $random;
 
 
-    /**
-     * @var PDOStatement[]
-     */
-    public static array $update;
-
     /**
     /**
      * @param Worker $worker
      * @param Worker $worker
      *
      *
@@ -53,32 +48,4 @@ class Raw implements Bootstrap
         self::$pdo = $pdo;
         self::$pdo = $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::$pdo->prepare($sql);
-        }
-
-        $val = [];
-        $keys = [];
-        foreach ($worlds as $world) {
-            $val[] = $keys[] = $world['id'];
-            $val[] = $world['randomNumber'];
-        }
-
-        self::$update[$rows]->execute([...$val, ...$keys]);
-    }
 }
 }

+ 8 - 5
frameworks/PHP/workerman/Mysql.php

@@ -34,9 +34,10 @@ class Mysql
     {
     {
         $count = min(max((int) $request->get('q'), 1), 500);
         $count = min(max((int) $request->get('q'), 1), 500);
         $arr = [];
         $arr = [];
+        $world = $this->world;
         while ($count--) {
         while ($count--) {
-            $this->world->execute([mt_rand(1, 10000)]);
-            $arr[] = $this->world->fetch();
+            $world->execute([mt_rand(1, 10000)]);
+            $arr[] = $world->fetch();
         }
         }
         return $arr;
         return $arr;
     }
     }
@@ -45,11 +46,13 @@ class Mysql
     {
     {
         $count = min(max((int) $request->get('q'), 1), 500);
         $count = min(max((int) $request->get('q'), 1), 500);
         $arr = [];
         $arr = [];
+        $world = $this->world;
+        $update = $this->update;
         while ($count--) {
         while ($count--) {
             $id = mt_rand(1, 10000);
             $id = mt_rand(1, 10000);
-            $this->world->execute([$id]);
-            $item = $this->world->fetch();
-            $this->update->execute(
+            $world->execute([$id]);
+            $item = $world->fetch();
+            $update->execute(
                 [$item['randomNumber'] = mt_rand(1, 10000), $id]
                 [$item['randomNumber'] = mt_rand(1, 10000), $id]
             );
             );
             $arr[] = $item;
             $arr[] = $item;

+ 3 - 2
frameworks/PHP/workerman/Pgsql.php

@@ -31,10 +31,11 @@ class Pgsql extends Mysql
         $queries = $request->get('q');
         $queries = $request->get('q');
         $worlds = $keys = $values = [];
         $worlds = $keys = $values = [];
         $count = min(max((int) $queries, 1), 500);
         $count = min(max((int) $queries, 1), 500);
+        $random = $this->random;
         for ($i = 0; $i < $count; ++ $i) {
         for ($i = 0; $i < $count; ++ $i) {
             $values[] = $keys[] = $id = mt_rand(1, 10000);
             $values[] = $keys[] = $id = mt_rand(1, 10000);
-            $this->random->execute([$id]);
-            $row = $this->random->fetch();
+            $random->execute([$id]);
+            $row = $random->fetch();
             $values[] = $row['randomNumber'] = mt_rand(1, 10000);
             $values[] = $row['randomNumber'] = mt_rand(1, 10000);
             $worlds[] = $row;
             $worlds[] = $row;
         }
         }

+ 0 - 1
frameworks/PHP/workerman/benchmark_config.json

@@ -1,6 +1,5 @@
 {
 {
   "framework": "workerman",
   "framework": "workerman",
-  "maintainers": ["walkor"],
   "tests": [{
   "tests": [{
     "default": {
     "default": {
       "dockerfile": "workerman-jit.dockerfile",
       "dockerfile": "workerman-jit.dockerfile",