From 90dfd340f04fd20bff4877986a53190268e29fb8 Mon Sep 17 00:00:00 2001 From: walkor Date: Tue, 24 Dec 2024 15:07:52 +0800 Subject: [PATCH 1/2] Code optimization --- .../PHP/webman/app/controller/Index.php | 48 ++++++++++--------- .../PHP/webman/support/bootstrap/db/Raw.php | 33 ------------- frameworks/PHP/workerman/Mysql.php | 13 +++-- frameworks/PHP/workerman/Pgsql.php | 5 +- 4 files changed, 36 insertions(+), 63 deletions(-) diff --git a/frameworks/PHP/webman/app/controller/Index.php b/frameworks/PHP/webman/app/controller/Index.php index 1b27503762f..5d40b24b0ed 100644 --- a/frameworks/PHP/webman/app/controller/Index.php +++ b/frameworks/PHP/webman/app/controller/Index.php @@ -5,7 +5,10 @@ use support\bootstrap\Date; use support\bootstrap\db\Raw as Db; use support\Response; -use PDO; +use function json_encode; +use function max; +use function min; +use function mt_rand; class Index { @@ -29,7 +32,7 @@ public function json() public function db() { $statement = Db::$random; - $statement->execute([\mt_rand(1, 10000)]); + $statement->execute([mt_rand(1, 10000)]); return new Response(200, [ 'Content-Type' => 'application/json', @@ -63,14 +66,11 @@ public function queries(Request $request, $q = 1) { $statement = Db::$random; - $query_count = 1; - if ((int) $q > 1) { - $query_count = \min($q, 500); - } + $query_count = min(max((int) $q, 1), 500); $arr = []; while ($query_count--) { - $statement->execute([\mt_rand(1, 10000)]); + $statement->execute([mt_rand(1, 10000)]); $arr[] = $statement->fetch(); } @@ -82,29 +82,31 @@ public function queries(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, [ 'Content-Type' => 'application/json', 'Date' => Date::$date - ], \json_encode($worlds)); + ], json_encode($worlds)); + } diff --git a/frameworks/PHP/webman/support/bootstrap/db/Raw.php b/frameworks/PHP/webman/support/bootstrap/db/Raw.php index d27d846bb3c..5c4389c2347 100644 --- a/frameworks/PHP/webman/support/bootstrap/db/Raw.php +++ b/frameworks/PHP/webman/support/bootstrap/db/Raw.php @@ -31,11 +31,6 @@ class Raw implements Bootstrap public static PDOStatement $random; - /** - * @var PDOStatement[] - */ - public static array $update; - /** * @param Worker $worker * @@ -53,32 +48,4 @@ public static function start($worker) 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]); - } } diff --git a/frameworks/PHP/workerman/Mysql.php b/frameworks/PHP/workerman/Mysql.php index c8dcd0ff40a..968ffcfef80 100644 --- a/frameworks/PHP/workerman/Mysql.php +++ b/frameworks/PHP/workerman/Mysql.php @@ -34,9 +34,10 @@ function query($request): array { $count = min(max((int) $request->get('q'), 1), 500); $arr = []; + $world = $this->world; while ($count--) { - $this->world->execute([mt_rand(1, 10000)]); - $arr[] = $this->world->fetch(); + $world->execute([mt_rand(1, 10000)]); + $arr[] = $world->fetch(); } return $arr; } @@ -45,11 +46,13 @@ function update($request): array { $count = min(max((int) $request->get('q'), 1), 500); $arr = []; + $world = $this->world; + $update = $this->update; while ($count--) { $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] ); $arr[] = $item; diff --git a/frameworks/PHP/workerman/Pgsql.php b/frameworks/PHP/workerman/Pgsql.php index 4f4be58fe44..637497eb03e 100644 --- a/frameworks/PHP/workerman/Pgsql.php +++ b/frameworks/PHP/workerman/Pgsql.php @@ -31,10 +31,11 @@ function update($request): array $queries = $request->get('q'); $worlds = $keys = $values = []; $count = min(max((int) $queries, 1), 500); + $random = $this->random; for ($i = 0; $i < $count; ++ $i) { $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); $worlds[] = $row; } From 98d4a040f48d60c8fbde04d19b560156e8fa91ea Mon Sep 17 00:00:00 2001 From: walkor Date: Tue, 24 Dec 2024 17:24:03 +0800 Subject: [PATCH 2/2] Remove maintainers --- frameworks/PHP/workerman/benchmark_config.json | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/PHP/workerman/benchmark_config.json b/frameworks/PHP/workerman/benchmark_config.json index 8e79f1b124c..0d4b57bad54 100644 --- a/frameworks/PHP/workerman/benchmark_config.json +++ b/frameworks/PHP/workerman/benchmark_config.json @@ -1,6 +1,5 @@ { "framework": "workerman", - "maintainers": ["walkor"], "tests": [{ "default": { "dockerfile": "workerman-jit.dockerfile",