Skip to content

Commit

Permalink
[php] Workeman update to PHP 8.4 (#9417)
Browse files Browse the repository at this point in the history
* Remove pgsql tests for swoole and swow

* Code optimization

* Update workerman.dockerfile to PHP/8.

* Update workerman-jit.dockerfile to PHP/8.4

* Update workerman-mysql-jit.dockerfile to PHP/8.4

* Update workerman-pgsql-jit.dockerfile to PHP/8.4

#9408

* Update workerman-pgsql.dockerfile to PHP/8.4

#9408

---------

Co-authored-by: walkor <[email protected]>
  • Loading branch information
joanhey and walkor authored Nov 25, 2024
1 parent 61ee574 commit c086685
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 130 deletions.
16 changes: 4 additions & 12 deletions frameworks/PHP/workerman/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ function db(): array

function query($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$count = min(max((int) $request->get('q'), 1), 500);
$arr = [];
while ($query_count--) {
while ($count--) {
$this->world->execute([mt_rand(1, 10000)]);
$arr[] = $this->world->fetch();
}
Expand All @@ -47,13 +43,9 @@ function query($request): array

function update($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$count = min(max((int) $request->get('q'), 1), 500);
$arr = [];
while ($query_count--) {
while ($count--) {
$id = mt_rand(1, 10000);
$this->world->execute([$id]);
$item = $this->world->fetch();
Expand Down
16 changes: 4 additions & 12 deletions frameworks/PHP/workerman/MysqlSwoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ function db(): array

function query($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$count = min(max((int) $request->get('q'), 1), 500);
$pdo = $this->pool->get();
$stmt = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
$arr = [];
while ($query_count--) {
while ($count--) {
$stmt->execute([mt_rand(1, 10000)]);
$arr[] = $stmt->fetch(PDO::FETCH_ASSOC);
}
Expand All @@ -49,16 +45,12 @@ function query($request): array

function update($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$count = min(max((int) $request->get('q'), 1), 500);
$arr = [];
$pdo = $this->pool->get();
$world = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
$update = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
while ($query_count--) {
while ($count--) {
$id = mt_rand(1, 10000);
$world->execute([$id]);
$item = $world->fetch(PDO::FETCH_ASSOC);
Expand Down
42 changes: 13 additions & 29 deletions frameworks/PHP/workerman/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,21 @@ public function __construct()

function update($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
$queries = $request->get('q');
$worlds = $keys = $values = [];
$count = min(max((int) $queries, 1), 500);
for ($i = 0; $i < $count; ++ $i) {
$values[] = $keys[] = $id = mt_rand(1, 10000);
$this->random->execute([$id]);
$row = $this->random->fetch();
$values[] = $row['randomNumber'] = mt_rand(1, 10000);
$worlds[] = $row;
}
$worlds = [];
while ($query_count--) {
$this->random->execute([\mt_rand(1, 10000)]);
$world = $this->random->fetch();
$world['randomNumber'] = \mt_rand(1, 10000);
$worlds[] = $world;
if (!isset($this->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)';
$this->updates[$count] = $this->pdo->prepare($sql);
}
$rows = count($worlds);

if (!isset($this->updates[$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)';

$this->updates[$rows] = $this->pdo->prepare($sql);
}

$val = [];
$keys = [];
foreach ($worlds as $world) {
$val[] = $keys[] = $world['id'];
$val[] = $world['randomNumber'];
}

$this->updates[$rows]->execute([...$val, ...$keys]);
$this->updates[$count]->execute([...$values, ...$keys]);
return $worlds;
}

Expand Down
8 changes: 2 additions & 6 deletions frameworks/PHP/workerman/PgsqlSwoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ public function __construct($size)

function update($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$count = min(max((int) $request->get('q'), 1), 500);
$worlds = [];
$pdo = $this->pool->get();
$random = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
while ($query_count--) {
while ($count--) {
$random->execute([mt_rand(1, 10000)]);
$world = $random->fetch(PDO::FETCH_ASSOC);
$world['randomNumber'] = mt_rand(1, 10000);
Expand Down
44 changes: 0 additions & 44 deletions frameworks/PHP/workerman/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,6 @@
"notes": "",
"versus": "php"
},
"pgsql-swow": {
"dockerfile": "workerman-pgsql-swow-jit.dockerfile",
"db_url": "/db",
"query_url": "/query?q=",
"update_url": "/update?q=",
"fortune_url": "/fortunes",
"port": 8080,
"approach": "Realistic",
"classification": "Platform",
"database": "Postgres",
"framework": "workerman",
"language": "PHP",
"flavor": "PHP8",
"orm": "Raw",
"platform": "workerman",
"webserver": "None",
"os": "Linux",
"database_os": "Linux",
"display_name": "workerman [jit, pgsql, swow, async]",
"notes": "",
"versus": "php"
},
"mysql-swow": {
"dockerfile": "workerman-mysql-swow-jit.dockerfile",
"db_url": "/db",
Expand All @@ -152,28 +130,6 @@
"notes": "",
"versus": "php"
},
"pgsql-swoole": {
"dockerfile": "workerman-pgsql-swoole-jit.dockerfile",
"db_url": "/db",
"query_url": "/query?q=",
"update_url": "/update?q=",
"fortune_url": "/fortunes",
"port": 8080,
"approach": "Realistic",
"classification": "Platform",
"database": "Postgres",
"framework": "workerman",
"language": "PHP",
"flavor": "PHP8",
"orm": "Raw",
"platform": "workerman",
"webserver": "None",
"os": "Linux",
"database_os": "Linux",
"display_name": "workerman [jit, pgsql, swoole, async]",
"notes": "",
"versus": "php"
},
"mysql-swoole": {
"dockerfile": "workerman-mysql-swoole-jit.dockerfile",
"db_url": "/db",
Expand Down
14 changes: 7 additions & 7 deletions frameworks/PHP/workerman/server.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
require_once __DIR__.'/vendor/autoload.php';

use Swoole\Coroutine;
use Workerman\Worker;
use Workerman\Events\Swow;
use Workerman\Events\Swoole;
Expand All @@ -11,10 +12,11 @@
$process = getenv('PROCESS_MULTIPLIER') ?: 1;
$pool_size = getenv('POOL_SIZE') ?: 2;
$process_count = (int) shell_exec('nproc') * $process;
$event_loop = getenv('EVENT_LOOP');

$db = $date = null;
$http_worker = new Worker('http://0.0.0.0:8080');
//$http_worker->reusePort = true;
$http_worker->reusePort = true;
$http_worker->count = $process_count;
$http_worker->onWorkerStart = static function () use ($test_type, $pool_size, &$db, &$date) {
$db = match ($test_type) {
Expand All @@ -28,12 +30,10 @@
};
$date = new Date();
};
if ($test_type === 'default') {
Worker::$eventLoopClass = Select::class;
} elseif (in_array($test_type, ['pgsql-swow', 'mysql-swow'])) {
Worker::$eventLoopClass = Swow::class;
} elseif (in_array($test_type, ['pgsql-swoole', 'mysql-swoole'])) {
Worker::$eventLoopClass = Swoole::class;

Worker::$eventLoopClass = "Workerman\\Events\\$event_loop";
if ($event_loop === 'Swoole') {
Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]);
}

$http_worker->onMessage = static function ($connection, $request) use (&$db, &$date) {
Expand Down
9 changes: 5 additions & 4 deletions frameworks/PHP/workerman/workerman-jit.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ FROM ubuntu:24.04

ENV TEST_TYPE default
ENV PROCESS_MULTIPLIER 1
ENV EVENT_LOOP Select

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null
RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php > /dev/null && \
apt-get update -yqq > /dev/null && apt-get upgrade -yqq > /dev/null

RUN apt-get install -yqq php8.3-cli php8.3-mysql php8.3-xml > /dev/null
RUN apt-get install -yqq php8.4-cli php8.4-mysql php8.4-xml > /dev/null

COPY --from=composer/composer:latest-bin --link /composer /usr/local/bin/composer

RUN apt-get install -y php-pear php8.3-dev libevent-dev git > /dev/null && \
pecl install event-3.1.4 > /dev/null && echo "extension=event.so" > /etc/php/8.3/cli/conf.d/30-event.ini
RUN apt-get install -y php-pear php8.4-dev libevent-dev git > /dev/null && \
pecl install event-3.1.4 > /dev/null && echo "extension=event.so" > /etc/php/8.4/cli/conf.d/30-event.ini

WORKDIR /workerman
COPY --link . .

RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
COPY php-jit.ini /etc/php/8.3/cli/conf.d/10-opcache.ini
COPY php-jit.ini /etc/php/8.4/cli/conf.d/10-opcache.ini

EXPOSE 8080

Expand Down
9 changes: 5 additions & 4 deletions frameworks/PHP/workerman/workerman-mysql-jit.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ FROM ubuntu:24.04

ENV TEST_TYPE mysql
ENV PROCESS_MULTIPLIER 4
ENV EVENT_LOOP Event

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null
RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php > /dev/null && \
apt-get update -yqq > /dev/null && apt-get upgrade -yqq > /dev/null

RUN apt-get install -yqq php8.3-cli php8.3-mysql php8.3-xml > /dev/null
RUN apt-get install -yqq php8.4-cli php8.4-mysql php8.4-xml > /dev/null

COPY --from=composer/composer:latest-bin --link /composer /usr/local/bin/composer

RUN apt-get install -y php-pear php8.3-dev libevent-dev git > /dev/null && \
pecl install event-3.1.4 > /dev/null && echo "extension=event.so" > /etc/php/8.3/cli/conf.d/30-event.ini
RUN apt-get install -y php-pear php8.4-dev libevent-dev git > /dev/null && \
pecl install event-3.1.4 > /dev/null && echo "extension=event.so" > /etc/php/8.4/cli/conf.d/30-event.ini

WORKDIR /workerman
COPY --link . .

RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
COPY php-jit.ini /etc/php/8.3/cli/conf.d/10-opcache.ini
COPY php-jit.ini /etc/php/8.4/cli/conf.d/10-opcache.ini

EXPOSE 8080

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ENV TEST_TYPE mysql-swoole
ENV SWOOLE_VERSION 5.1.5
ENV PROCESS_MULTIPLIER 1
ENV POOL_SIZE 4
ENV EVENT_LOOP Swoole

ARG DEBIAN_FRONTEND=noninteractive

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM ubuntu:24.04
ENV TEST_TYPE mysql-swow
ENV PROCESS_MULTIPLIER 1
ENV POOL_SIZE 4
ENV EVENT_LOOP Swow

ARG DEBIAN_FRONTEND=noninteractive

Expand Down
9 changes: 5 additions & 4 deletions frameworks/PHP/workerman/workerman-pgsql-jit.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ FROM ubuntu:24.04

ENV TEST_TYPE pgsql
ENV PROCESS_MULTIPLIER 4
ENV EVENT_LOOP Event

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null
RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php > /dev/null && \
apt-get update -yqq > /dev/null && apt-get upgrade -yqq > /dev/null

RUN apt-get install -yqq php8.3-cli php8.3-pgsql php8.3-xml > /dev/null
RUN apt-get install -yqq php8.4-cli php8.4-pgsql php8.4-xml > /dev/null

COPY --from=composer/composer:latest-bin --link /composer /usr/local/bin/composer

RUN apt-get install -y php-pear php8.3-dev libevent-dev git > /dev/null && \
pecl install event-3.1.4 > /dev/null && echo "extension=event.so" > /etc/php/8.3/cli/conf.d/30-event.ini
RUN apt-get install -y php-pear php8.4-dev libevent-dev git > /dev/null && \
pecl install event-3.1.4 > /dev/null && echo "extension=event.so" > /etc/php/8.4/cli/conf.d/30-event.ini

WORKDIR /workerman
COPY --link . .

RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
COPY php-jit.ini /etc/php/8.3/cli/conf.d/10-opcache.ini
COPY php-jit.ini /etc/php/8.4/cli/conf.d/10-opcache.ini

EXPOSE 8080

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ENV TEST_TYPE pgsql-swoole
ENV SWOOLE_VERSION 5.1.5
ENV PROCESS_MULTIPLIER 2
ENV POOL_SIZE 16
ENV EVENT_LOOP Swoole

ARG DEBIAN_FRONTEND=noninteractive

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM ubuntu:24.04
ENV TEST_TYPE pgsql-swow
ENV PROCESS_MULTIPLIER 2
ENV POOL_SIZE 16
ENV EVENT_LOOP Swow

ARG DEBIAN_FRONTEND=noninteractive

Expand Down
9 changes: 5 additions & 4 deletions frameworks/PHP/workerman/workerman-pgsql.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ FROM ubuntu:24.04

ENV TEST_TYPE pgsql
ENV PROCESS_MULTIPLIER 4
ENV EVENT_LOOP Event

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null
RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php > /dev/null && \
apt-get update -yqq > /dev/null && apt-get upgrade -yqq > /dev/null

RUN apt-get install -yqq php8.3-cli php8.3-pgsql php8.3-xml > /dev/null
RUN apt-get install -yqq php8.4-cli php8.4-pgsql php8.4-xml > /dev/null

COPY --from=composer/composer:latest-bin --link /composer /usr/local/bin/composer

RUN apt-get install -y php-pear php8.3-dev libevent-dev git > /dev/null && \
pecl install event-3.1.4 > /dev/null && echo "extension=event.so" > /etc/php/8.3/cli/conf.d/30-event.ini
RUN apt-get install -y php-pear php8.4-dev libevent-dev git > /dev/null && \
pecl install event-3.1.4 > /dev/null && echo "extension=event.so" > /etc/php/8.4/cli/conf.d/30-event.ini

WORKDIR /workerman
COPY --link . .

RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
COPY php.ini /etc/php/8.3/cli/php.ini
COPY php.ini /etc/php/8.4/cli/php.ini

EXPOSE 8080

Expand Down
Loading

0 comments on commit c086685

Please sign in to comment.