Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[php] Simplify the code for Webman Workerman #9474

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions frameworks/PHP/webman/app/controller/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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',
Expand Down Expand Up @@ -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();
}

Expand All @@ -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));

}


Expand Down
33 changes: 0 additions & 33 deletions frameworks/PHP/webman/support/bootstrap/db/Raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class Raw implements Bootstrap

public static PDOStatement $random;

/**
* @var PDOStatement[]
*/
public static array $update;

/**
* @param Worker $worker
*
Expand All @@ -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]);
}
}
13 changes: 8 additions & 5 deletions frameworks/PHP/workerman/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions frameworks/PHP/workerman/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion frameworks/PHP/workerman/benchmark_config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"framework": "workerman",
"maintainers": ["walkor"],
"tests": [{
"default": {
"dockerfile": "workerman-jit.dockerfile",
Expand Down
Loading