Skip to content

Commit 082b573

Browse files
authored
[php] Simplify the code for Webman Workerman (#9474)
* Code optimization * Remove maintainers
1 parent 6a920c6 commit 082b573

File tree

5 files changed

+36
-64
lines changed

5 files changed

+36
-64
lines changed

frameworks/PHP/webman/app/controller/Index.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use support\bootstrap\Date;
66
use support\bootstrap\db\Raw as Db;
77
use support\Response;
8-
use PDO;
8+
use function json_encode;
9+
use function max;
10+
use function min;
11+
use function mt_rand;
912

1013
class Index
1114
{
@@ -29,7 +32,7 @@ public function json()
2932
public function db()
3033
{
3134
$statement = Db::$random;
32-
$statement->execute([\mt_rand(1, 10000)]);
35+
$statement->execute([mt_rand(1, 10000)]);
3336

3437
return new Response(200, [
3538
'Content-Type' => 'application/json',
@@ -63,14 +66,11 @@ public function queries(Request $request, $q = 1)
6366
{
6467
$statement = Db::$random;
6568

66-
$query_count = 1;
67-
if ((int) $q > 1) {
68-
$query_count = \min($q, 500);
69-
}
69+
$query_count = min(max((int) $q, 1), 500);
7070

7171
$arr = [];
7272
while ($query_count--) {
73-
$statement->execute([\mt_rand(1, 10000)]);
73+
$statement->execute([mt_rand(1, 10000)]);
7474
$arr[] = $statement->fetch();
7575
}
7676

@@ -82,29 +82,31 @@ public function queries(Request $request, $q = 1)
8282

8383
public function updates(Request $request, $q = 1)
8484
{
85-
$random = Db::$random;
85+
static $updates = [];
8686

87-
$query_count = 1;
88-
if ((int) $q > 1) {
89-
$query_count = \min($q, 500);
87+
$random = Db::$random;
88+
$pdo = Db::$pdo;
89+
$count = min(max((int) $q, 1), 500);
90+
91+
$worlds = $keys = $values = [];
92+
for ($i = 0; $i < $count; ++ $i) {
93+
$values[] = $keys[] = $id = mt_rand(1, 10000);
94+
$random->execute([$id]);
95+
$row = $random->fetch();
96+
$values[] = $row['randomNumber'] = mt_rand(1, 10000);
97+
$worlds[] = $row;
9098
}
91-
92-
$worlds = [];
93-
94-
while ($query_count--) {
95-
$random->execute([\mt_rand(1, 10000)]);
96-
$world = $random->fetch();
97-
$world['randomNumber'] = \mt_rand(1, 10000);
98-
99-
$worlds[] = $world;
99+
if (!isset($updates[$count])) {
100+
$sql = 'UPDATE World SET randomNumber = CASE id' . str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $count) . 'END WHERE id IN (' . str_repeat('?::INTEGER,', $count - 1) . '?::INTEGER)';
101+
$updates[$count] = $pdo->prepare($sql);
100102
}
101-
102-
Db::update($worlds);
103+
$updates[$count]->execute([...$values, ...$keys]);
103104

104105
return new Response(200, [
105106
'Content-Type' => 'application/json',
106107
'Date' => Date::$date
107-
], \json_encode($worlds));
108+
], json_encode($worlds));
109+
108110
}
109111

110112

frameworks/PHP/webman/support/bootstrap/db/Raw.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ class Raw implements Bootstrap
3131

3232
public static PDOStatement $random;
3333

34-
/**
35-
* @var PDOStatement[]
36-
*/
37-
public static array $update;
38-
3934
/**
4035
* @param Worker $worker
4136
*
@@ -53,32 +48,4 @@ public static function start($worker)
5348
self::$pdo = $pdo;
5449
}
5550

56-
/**
57-
* Postgres bulk update
58-
*
59-
* @param array $worlds
60-
* @return void
61-
*/
62-
public static function update(array $worlds)
63-
{
64-
$rows = count($worlds);
65-
66-
if (!isset(self::$update[$rows])) {
67-
$sql = 'UPDATE world SET randomNumber = CASE id'
68-
. str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $rows)
69-
. 'END WHERE id IN ('
70-
. str_repeat('?::INTEGER,', $rows - 1) . '?::INTEGER)';
71-
72-
self::$update[$rows] = self::$pdo->prepare($sql);
73-
}
74-
75-
$val = [];
76-
$keys = [];
77-
foreach ($worlds as $world) {
78-
$val[] = $keys[] = $world['id'];
79-
$val[] = $world['randomNumber'];
80-
}
81-
82-
self::$update[$rows]->execute([...$val, ...$keys]);
83-
}
8451
}

frameworks/PHP/workerman/Mysql.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ function query($request): array
3434
{
3535
$count = min(max((int) $request->get('q'), 1), 500);
3636
$arr = [];
37+
$world = $this->world;
3738
while ($count--) {
38-
$this->world->execute([mt_rand(1, 10000)]);
39-
$arr[] = $this->world->fetch();
39+
$world->execute([mt_rand(1, 10000)]);
40+
$arr[] = $world->fetch();
4041
}
4142
return $arr;
4243
}
@@ -45,11 +46,13 @@ function update($request): array
4546
{
4647
$count = min(max((int) $request->get('q'), 1), 500);
4748
$arr = [];
49+
$world = $this->world;
50+
$update = $this->update;
4851
while ($count--) {
4952
$id = mt_rand(1, 10000);
50-
$this->world->execute([$id]);
51-
$item = $this->world->fetch();
52-
$this->update->execute(
53+
$world->execute([$id]);
54+
$item = $world->fetch();
55+
$update->execute(
5356
[$item['randomNumber'] = mt_rand(1, 10000), $id]
5457
);
5558
$arr[] = $item;

frameworks/PHP/workerman/Pgsql.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ function update($request): array
3131
$queries = $request->get('q');
3232
$worlds = $keys = $values = [];
3333
$count = min(max((int) $queries, 1), 500);
34+
$random = $this->random;
3435
for ($i = 0; $i < $count; ++ $i) {
3536
$values[] = $keys[] = $id = mt_rand(1, 10000);
36-
$this->random->execute([$id]);
37-
$row = $this->random->fetch();
37+
$random->execute([$id]);
38+
$row = $random->fetch();
3839
$values[] = $row['randomNumber'] = mt_rand(1, 10000);
3940
$worlds[] = $row;
4041
}

frameworks/PHP/workerman/benchmark_config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"framework": "workerman",
3-
"maintainers": ["walkor"],
43
"tests": [{
54
"default": {
65
"dockerfile": "workerman-jit.dockerfile",

0 commit comments

Comments
 (0)