diff --git a/frameworks/PHP/openswoole/README.md b/frameworks/PHP/openswoole/README.md index f24e8b086541..402e011528f4 100644 --- a/frameworks/PHP/openswoole/README.md +++ b/frameworks/PHP/openswoole/README.md @@ -1,62 +1,55 @@ -

- Open Swoole Logo -

+

-[![lib-openswoole](https://github.com/openswoole/swoole-src/workflows/lib-openswoole/badge.svg)](https://github.com/openswoole/swoole-src/actions?query=workflow%3Alib-openswoole) -[![ext-openswoole](https://github.com/openswoole/swoole-src/workflows/ext-openswoole/badge.svg)](https://github.com/openswoole/swoole-src/actions?query=workflow%3Aext-openswoole) -[![test-linux](https://github.com/openswoole/swoole-src/workflows/test-linux/badge.svg)](https://github.com/openswoole/swoole-src/actions?query=workflow%3Atest-linux) -[![Coverity Scan Build Status](https://scan.coverity.com/projects/23970/badge.svg)](https://scan.coverity.com/projects/open-swoole-src) -[![codecov](https://codecov.io/gh/openswoole/swoole-src/branch/master/graph/badge.svg)](https://codecov.io/gh/openswoole/swoole-src) -**Open Swoole (since 2017) is a programmatic server for PHP with async IO, coroutines and fibers: secure, reliable, high performance** +

lib-openswoole +ext-openswoole +test-linux +Coverity Scan Build Status

-## Documentation & Community +## Open Swoole -+ __Documentation__: +Open Swoole is a programmatic server for PHP with async IO, coroutines and fibers: secure, reliable, high performance + ++ __Website__: + __Twitter__: -+ __Join Slack Group__: -+ __Join Discord Channel__: -+ __IDE Helper & API__: ++ __Slack__: ++ __Discord__: ++ __IDE Helper__: -## Installation +## Documentation -> As with any open source project, Open Swoole always provides the most reliable stability and the most powerful features in **the latest released version**. Please ensure as much as possible that you are using the latest version. +Documentation for Open Swoole can be found on the [Open Swoole website](https://openswoole.com/docs). -### Compiling requirements +## Installation -+ Linux, OS X or Cygwin, WSL -+ PHP 7.2.0 or later (The higher the version, the better the performance.) -+ GCC 4.8 or later +> Open Swoole always provides the most reliable stability and the most powerful features in **the latest released version**. Please ensure as much as possible that you are using the latest version. -### 1. Install with PECL (beginners) +### 1. Install or upgrade Open Swoole from multiple distribution channels -```shell -pecl install openswoole -``` +Please check [Open Swoole Installation Guide](https://openswoole.com/docs/get-started/installation) about how to install Open Swoole on Ubuntu/CentOS/Windows WSL from Docker, PECL or Binary releases channels. -### 2. Install from source (recommended) +### 2. Compile from source -Please download the source packages from [Releases](https://github.com/openswoole/swoole-src/releases) or: +#### Compiling requirements -```shell -git clone https://github.com/openswoole/swoole-src.git && \ -cd swoole-src -git checkout v4.8.0 -``` ++ Linux, OS X or Cygwin, WSL ++ PHP 7.4.0 or later (The higher the version, the better the performance.) ++ GCC 4.8 or later -Compile and install at the source folder: +Download the source packages from [Releases](https://github.com/openswoole/ext-openswoole/releases) or: ```shell +git clone https://github.com/openswoole/ext-openswoole.git && \ +cd ext-openswoole +git checkout v22.0.0 phpize && \ ./configure && \ make && make install ``` -#### Enable extension in PHP - -After compiling and installing the openswoole extension, you have to add a new line `extension=openswoole.so` to `php.ini` to enable Open Swoole. It is recommended to be added after all the other extensions because openswoole may depend on extensions: sockets, mysqlnd, curl etc. +You can find how to fix [Common Installation Errors](https://openswoole.com/docs/get-started/common-install-errors) if there are errors in the installation. -#### Extra compile configurations +#### Compile configurations > for example: `./configure --enable-openssl --enable-sockets` @@ -64,39 +57,34 @@ After compiling and installing the openswoole extension, you have to add a new l + `--enable-sockets` + `--enable-http2` + `--enable-mysqlnd` (need mysqlnd, it just for supporting `$mysql->escape` method) -+ `--enable-swoole-json` -+ `--enable-swoole-curl` ++ `--enable-hook-curl` + `--with-postgres[=DIR]` -### Upgrade +#### Enable Open Swoole extension -> If you upgrade from source, don't forget to `make clean` before you upgrade your swoole +After compiling and installing the openswoole extension, you have to add a new line `extension=openswoole.so` at the end of `php.ini` or create a ini file at `conf.d` folder to enable Open Swoole. It is recommended to be added after all the other extensions because openswoole may depend on extensions: sockets, mysqlnd, curl etc. -1. `pecl upgrade openswoole` -2. `cd swoole-src && git pull && make clean && make && sudo make install` -3. if you have changed PHP version, please re-run `phpize clean && phpize` then try to compile +## Frameworks & Components -## Call for Contributors +> PR are welcome if your framework is using openswoole + + - [**Laravel Octane**](https://laravel.com/docs/9.x/octane) Laravel Octane supercharges your application's performance by serving your application using high-powered application servers. + - [**PHP Runtime**](https://github.com/php-runtime) make it easy to run any kind of PHP Application (Symfony, Laravel, PSR7, Native) with all kinds of Runtimes like OpenSwoole, Bref, Google Cloud Functions, Roadrunner and React PHP with minimal configuration. + - [**Mezzio Swoole**](https://docs.mezzio.dev/mezzio-swoole/) allows you to run Mezzio and [PSR-15](https://www.php-fig.org/psr/psr-15/) applications on Open Swoole. + +## For Contributors If you like to involve the maintenance of this repo, it is better to get started by submitting PR, you will be invited to the dev group once there are significant contributions. Or join Slack group firstly, the team will provide mentoring and internal support to help you get started. -* [Report issues and feedback](https://github.com/openswoole/swoole-src/issues) +* [Report issues and feedback](https://github.com/openswoole/ext-openswoole/issues) * Submit fixes, features via Pull Request -This project exists thanks to all the historical [[Contributors](https://github.com/openswoole/swoole-src/graphs/contributors)]. +This project exists thanks to all the historical [[Contributors](https://github.com/openswoole/ext-openswoole/graphs/contributors)]. ## Security issues -Security issues should be reported privately, via email, to the Open Swoole develop team [hello@swoole.co.uk](mailto:hello@swoole.co.uk). You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. - -## Frameworks & Components - -> PR are welcome if your framework is using openswoole - - - [**Laravel Octane**](https://laravel.com/docs/8.x/octane) Laravel Octane supercharges your application's performance by serving your application using high-powered application servers. - - [**PHP Runtime**](https://github.com/php-runtime) make it easy to run any kind of PHP Application (Symfony, Laravel, PSR7, Native) with all kinds of Runtimes like OpenSwoole, Bref, Google Cloud Functions, Roadrunner and React PHP with minimal configuration. - - [**Mezzio Swoole**](https://docs.mezzio.dev/mezzio-swoole/) allows you to run Mezzio and [PSR-15](https://www.php-fig.org/psr/psr-15/) applications on Open Swoole. +Security issues should be reported privately, via email, to the Open Swoole develop team [hello@openswoole.com](mailto:hello@openswoole.com). You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. ## License -Apache License Version 2.0 see http://www.apache.org/licenses/LICENSE-2.0.html +Open Swoole is open-sourced software licensed under the [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0.html). diff --git a/frameworks/PHP/openswoole/benchmark_config.json b/frameworks/PHP/openswoole/benchmark_config.json index 4aaf36357d4a..f29fdcdb4633 100644 --- a/frameworks/PHP/openswoole/benchmark_config.json +++ b/frameworks/PHP/openswoole/benchmark_config.json @@ -14,34 +14,13 @@ "database": "MySQL", "framework": "openswoole", "language": "PHP", - "flavor": "PHP8.1", + "flavor": "PHP8.2", "orm": "Raw", "platform": "openswoole", "webserver": "none", "os": "Linux", "database_os": "Linux", - "display_name": "Open Swoole", - "notes": "", - "versus": "php" - }, - "postgres": { - "db_url": "/db", - "query_url": "/db?queries=", - "fortune_url": "/fortunes", - "update_url": "/updates?queries=", - "port": 8080, - "approach": "Realistic", - "classification": "Platform", - "database": "Postgres", - "framework": "openswoole", - "language": "PHP", - "flavor": "PHP8", - "orm": "Raw", - "platform": "openswoole", - "webserver": "none", - "os": "Linux", - "database_os": "Linux", - "display_name": "Open Swoole Postgres", + "display_name": "openswoole", "notes": "", "versus": "php" }, @@ -56,13 +35,13 @@ "database": "MySQL", "framework": "openswoole", "language": "PHP", - "flavor": "PHP8.1", + "flavor": "PHP8.2", "orm": "Raw", "platform": "openswoole", "webserver": "none", "os": "Linux", "database_os": "Linux", - "display_name": "Open Swoole no-async", + "display_name": "openswoole no-async", "notes": "Without async db pool connection", "versus": "php" } diff --git a/frameworks/PHP/openswoole/composer.json b/frameworks/PHP/openswoole/composer.json new file mode 100644 index 000000000000..f71d50109b37 --- /dev/null +++ b/frameworks/PHP/openswoole/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "openswoole/core": "dev-master" + } +} diff --git a/frameworks/PHP/openswoole/openswoole-no-async.dockerfile b/frameworks/PHP/openswoole/openswoole-no-async.dockerfile index c05c7c53ddbd..ddfa2d2da580 100644 --- a/frameworks/PHP/openswoole/openswoole-no-async.dockerfile +++ b/frameworks/PHP/openswoole/openswoole-no-async.dockerfile @@ -1,15 +1,21 @@ -FROM php:8.1-cli +FROM php:8.2-cli + +RUN apt-get update && apt-get install -y git > /dev/null + +RUN docker-php-ext-install opcache pdo_mysql > /dev/null RUN pecl install openswoole > /dev/null && \ docker-php-ext-enable openswoole -RUN docker-php-ext-install opcache pdo_mysql > /dev/null +COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer + +COPY php.ini /usr/local/etc/php/ ADD ./ /openswoole WORKDIR /openswoole -COPY php.ini /usr/local/etc/php/ +RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet EXPOSE 8080 -CMD php swoole-server-noasync.php +CMD php /openswoole/openswoole-server-pdo.php diff --git a/frameworks/PHP/openswoole/openswoole-postgres.dockerfile b/frameworks/PHP/openswoole/openswoole-postgres.dockerfile index 83bfc993a5cf..02aa7432b328 100644 --- a/frameworks/PHP/openswoole/openswoole-postgres.dockerfile +++ b/frameworks/PHP/openswoole/openswoole-postgres.dockerfile @@ -1,22 +1,26 @@ -FROM php:8.1-cli +FROM php:8.2-cli + +RUN apt-get update && apt-get install -y git > /dev/null RUN docker-php-ext-install opcache > /dev/null -ENV SWOOLE_VERSION 4.8.0 +ENV VERSION 22.0.0 RUN apt-get update && apt-get install -y libpq-dev \ - && cd /tmp && curl -sSL "https://github.com/openswoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz" | tar xzf - \ - && cd swoole-src-${SWOOLE_VERSION} \ + && cd /tmp && curl -sSL "https://github.com/openswoole/ext-openswoole/archive/v${VERSION}.tar.gz" | tar xzf - \ + && cd ext-openswoole-${VERSION} \ && phpize && ./configure --with-postgres > /dev/null && make > /dev/null && make install > /dev/null \ && docker-php-ext-enable openswoole -WORKDIR /openswoole - -COPY swoole-server.php swoole-server.php -RUN sed -i "s|_postgres||g" swoole-server.php +COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer COPY php.ini /usr/local/etc/php/ +ADD ./ /openswoole +WORKDIR /openswoole + +RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet + EXPOSE 8080 -CMD php swoole-server.php +CMD php /openswoole/openswoole-server-postgres.php diff --git a/frameworks/PHP/openswoole/openswoole-server-mysql.php b/frameworks/PHP/openswoole/openswoole-server-mysql.php new file mode 100644 index 000000000000..8c921a3f1844 --- /dev/null +++ b/frameworks/PHP/openswoole/openswoole-server-mysql.php @@ -0,0 +1,197 @@ +set([ + 'worker_num' => Util::getCPUNum() * 2, + 'log_file' => '/dev/null', + 'log_level' => 5, + 'open_tcp_nodelay' => true, +]); + + +$pool = null; + +/** + * On start of the PHP worker. One worker per server process is started. + */ +$server->on('workerStart', function () use(&$pool) { + $config = (new PDOConfig()) + ->withHost('tfb-database') + ->withDbname('hello_world') + ->withUsername('benchmarkdbuser') + ->withPassword('benchmarkdbpass') + ->withOptions([ + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_EMULATE_PREPARES => false + ]); + $pool = new ClientPool(PDOClientFactory::class, $config, \intdiv(512, Util::getCPUNum() * 2)); +}); + +/** + * The DB test + * + * @param string $database_type + * @param int $queries + * + * @return string + */ +$db_mysql = function (int $queries, $pool): string { + $db = $pool->get(); + + $query_count = 1; + if ($queries > 1) { + $query_count = $queries > 500 ? 500 : $queries; + } + + // Create an array with the response string. + $arr = []; + + $db_test = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?'); + + // For each query, store the result set values in the response array + while ($query_count--) { + $db_test->execute([\mt_rand(1, 10000)]); + $arr[] = $db_test->fetch(); + } + $pool->put($db); + + if($queries < 0) { + return \json_encode($arr[0], JSON_NUMERIC_CHECK); + } + + return \json_encode($arr, JSON_NUMERIC_CHECK); +}; + +/** + * The Fortunes test + * + * @param string $database_type + * + * @return string + */ +$fortunes_mysql = function ($pool): string { + $db = $pool->get(); + + $fortune = []; + + $fortune_test = $db->prepare('SELECT id, message FROM Fortune'); + $fortune_test->execute(); + $fortune = $fortune_test->fetchAll(PDO::FETCH_KEY_PAIR); + + $fortune[0] = 'Additional fortune added at request time.'; + asort($fortune); + + $html = ''; + foreach ($fortune as $id => $message) { + $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); + $html .= "$id$message"; + } + + $pool->put($db); + + return "Fortunes$html
idmessage
"; +}; + +/** + * The Updates test + * + * @param string $database_type + * @param int $queries + * + * @return string + */ +$updates_mysql = function (int $queries = 0, $pool): string { + $db = $pool->get(); + + $query_count = 1; + if ($queries > 1) { + $query_count = $queries > 500 ? 500 : $queries; + } + + $arr = []; + $updates_test_select = $db->prepare('SELECT id,randomNumber FROM World WHERE id = ?'); + $updates_test_update = $db->prepare('UPDATE World SET randomNumber = ? WHERE id = ?'); + + while ($query_count--) { + $id = \mt_rand(1, 10000); + $updates_test_select->execute([$id]); + $world = ["id" => $id, "randomNumber" => $updates_test_select->fetchColumn()]; + $world['randomNumber'] = mt_rand(1, 10000); + $updates_test_update->execute([$world['randomNumber'], $world['id']]); + $arr[] = $world; + } + + $pool->put($db); + + return \json_encode($arr, JSON_NUMERIC_CHECK); +}; + + +/** + * On every request to the (web)server, execute the following code + */ +$server->on('request', static function (Request $req, Response $res) use ($db_mysql, $db_query, $fortunes_mysql, $updates_mysql, &$pool) { + try { + switch ($req->server['request_uri']) { + case '/json': + $res->header('Content-Type', 'application/json'); + $res->header('Server', 'os'); + $res->end(json_encode(['message' => 'Hello, World!'])); + break; + + case '/plaintext': + $res->header('Content-Type', 'text/plain; charset=utf-8'); + $res->header('Server', 'os'); + $res->end('Hello, World!'); + break; + + case '/db': + $res->header('Content-Type', 'application/json'); + $res->header('Server', 'os'); + if (isset($req->get['queries'])) { + $res->end($db_mysql((int)$req->get['queries'], $pool)); + } else { + $res->end($db_mysql(-1, $pool)); + } + break; + + case '/fortunes': + $res->header('Content-Type', 'text/html; charset=utf-8'); + $res->header('Server', 'os'); + $res->end($fortunes_mysql($pool)); + break; + + case '/updates': + $res->header('Content-Type', 'application/json'); + $res->header('Server', 'os'); + if (isset($req->get['queries'])) { + $res->end($updates_mysql((int)$req->get['queries'], $pool)); + } else { + $res->end($updates_mysql(-1, $pool)); + } + break; + + default: + $res->status(404); + $res->end('Error 404'); + + } + + } catch (\Throwable $e) { + $res->status(500); + $res->end('Error 500'); + } +}); + +$server->start(); diff --git a/frameworks/PHP/openswoole/swoole-server-noasync.php b/frameworks/PHP/openswoole/openswoole-server-pdo.php similarity index 78% rename from frameworks/PHP/openswoole/swoole-server-noasync.php rename to frameworks/PHP/openswoole/openswoole-server-pdo.php index 066522533a7f..d8f7b672ffcf 100644 --- a/frameworks/PHP/openswoole/swoole-server-noasync.php +++ b/frameworks/PHP/openswoole/openswoole-server-pdo.php @@ -1,12 +1,14 @@ set([ - 'worker_num' => swoole_cpu_num() * 4, + 'worker_num' => Util::getCPUNum() * 2, 'log_file' => '/dev/null', 'log_level' => 5, 'open_tcp_nodelay' => true, @@ -27,37 +29,37 @@ switch ($req->server['request_uri']) { case '/json': $res->header('Content-Type', 'application/json'); - $res->header('Server', 'openswoole'); + $res->header('Server', 'os'); $res->end(json_encode(['message' => 'Hello, World!'])); break; case '/plaintext': $res->header('Content-Type', 'text/plain; charset=utf-8'); - $res->header('Server', 'openswoole'); + $res->header('Server', 'os'); $res->end('Hello, World!'); break; case '/db': $res->header('Content-Type', 'application/json'); - $res->header('Server', 'openswoole'); + $res->header('Server', 'os'); $res->end(db()); break; case '/query': $res->header('Content-Type', 'application/json'); - $res->header('Server', 'openswoole'); + $res->header('Server', 'os'); $res->end(query((int) $req->get['q'] ?? 1)); break; case '/fortunes': $res->header('Content-Type', 'text/html; charset=utf-8'); - $res->header('Server', 'openswoole'); + $res->header('Server', 'os'); $res->end(fortunes()); break; case '/updates': $res->header('Content-Type', 'application/json'); - $res->header('Server', 'openswoole'); + $res->header('Server', 'os'); $res->end(updates((int) $req->get['q'] ?? 1)); break; diff --git a/frameworks/PHP/openswoole/openswoole-server-postgres.php b/frameworks/PHP/openswoole/openswoole-server-postgres.php new file mode 100644 index 000000000000..31a712ca9ce2 --- /dev/null +++ b/frameworks/PHP/openswoole/openswoole-server-postgres.php @@ -0,0 +1,205 @@ +set([ + 'worker_num' => Util::getCPUNum() * 2, + 'log_file' => '/dev/null', + 'log_level' => 5, + 'open_tcp_nodelay' => true, +]); + +$pool = null; + +/** + * On start of the PHP worker. One worker per server process is started. + */ +$server->on('workerStart', function () use (&$pool) { + $config = (new PostgresConfig()) + ->withHost('tfb-database') + ->withDbname('hello_world') + ->withUsername('benchmarkdbuser') + ->withPassword('benchmarkdbpass'); + $pool = new ClientPool(PostgresClientFactory::class, $config, \intdiv(512, Util::getCPUNum() * 2)); +}); + +/** + * The DB test + * + * @param string $database_type + * @param int $queries + * + * @return string + */ +$db_postgres = function (int $queries = 0, $pool): string { + $db = $pool->get(); + // Read number of queries to run from URL parameter + $query_count = 1; + if ($queries > 1) { + $query_count = $queries > 500 ? 500 : $queries; + } + + // Create an array with the response string. + $arr = []; + + $db->prepare('s', 'SELECT id, randomnumber FROM World WHERE id = $1'); + + // For each query, store the result set values in the response array + while ($query_count--) { + $id = mt_rand(1, 10000); + $res = $db->execute('s', [$id]); + $ret = $db->fetchAssoc($res); + // Store result in array. + $arr[] = ['id' => $id, 'randomnumber' => $ret['randomnumber']]; + } + + // Use the PHP standard JSON encoder. + // http://www.php.net/manual/en/function.json-encode.php + if ($queries === -1) { + $arr = $arr[0]; + } + + $pool->put($db); + + return \json_encode($arr, JSON_NUMERIC_CHECK); +}; + +/** + * The Fortunes test + * + * @param string $database_type + * + * @return string + */ +$fortunes_postgres = function ($pool): string { + $db = $pool->get(); + + $fortune = []; + + $db->prepare('f', 'SELECT id, message FROM Fortune'); + $res = $db->execute('f', []); + $arr = $db->fetchAll($res); + + foreach ($arr as $row) { + $fortune[$row['id']] = $row['message']; + } + $fortune[0] = 'Additional fortune added at request time.'; + \asort($fortune); + + $html = ''; + foreach ($fortune as $id => $message) { + $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); + $html .= "{$id}{$message}"; + } + + $pool->put($db); + + return 'Fortunes' + .$html. + '
idmessage
'; +}; + +/** + * The Updates test + * + * @param string $database_type + * @param int $queries + * + * @return string + */ +$updates_postgres = function (int $queries = 0, $pool): string { + $db = $pool->get(); + + $query_count = 1; + if ($queries > 1) { + $query_count = $queries > 500 ? 500 : $queries; + } + + $arr = []; + + $db->prepare('us', 'SELECT id,randomnumber FROM World WHERE id = $1'); + $db->prepare('uu', 'UPDATE World SET randomnumber = $1 WHERE id = $2'); + + while ($query_count--) { + $id = \mt_rand(1, 10000); + $randomNumber = \mt_rand(1, 10000); + $res = $db->execute('us', [$id]); + $ret = $db->fetchAssoc($res); + // Store result in array. + $world = ['id' => $id, 'randomnumber' => $ret['randomnumber']]; + $world['randomnumber'] = $randomNumber; + $res = $db->execute('uu', [$randomNumber, $id]); + $arr[] = $world; + } + + $pool->put($db); + + return \json_encode($arr, JSON_NUMERIC_CHECK); +}; + +/** + * On every request to the (web)server, execute the following code + */ +$server->on('request', function (Request $req, Response $res) use ($db_postgres, $fortunes_postgres, $updates_postgres, &$pool) { + try { + switch ($req->server['request_uri']) { + case '/json': + $res->header('Content-Type', 'application/json'); + $res->header('Server', 'os'); + $res->end(json_encode(['message' => 'Hello, World!'])); + break; + + case '/plaintext': + $res->header('Content-Type', 'text/plain; charset=utf-8'); + $res->header('Server', 'os'); + $res->end('Hello, World!'); + break; + + case '/db': + $res->header('Content-Type', 'application/json'); + $res->header('Server', 'os'); + if (isset($req->get['queries'])) { + $res->end($db_postgres((int)$req->get['queries'], $pool)); + } else { + $res->end($db_postgres(-1, $pool)); + } + break; + + case '/fortunes': + $res->header('Content-Type', 'text/html; charset=utf-8'); + $res->header('Server', 'os'); + $res->end($fortunes_postgres($pool)); + break; + + case '/updates': + $res->header('Content-Type', 'application/json'); + $res->header('Server', 'os'); + if (isset($req->get['queries'])) { + $res->end($updates_postgres((int)$req->get['queries'], $pool)); + } else { + $res->end($updates_postgres(-1, $pool)); + } + break; + + default: + $res->status(404); + $res->end('Error 404'); + + } + + } catch (\Throwable $e) { + $res->status(500); + $res->end('Error 500'); + } +}); + +$server->start(); diff --git a/frameworks/PHP/openswoole/openswoole.dockerfile b/frameworks/PHP/openswoole/openswoole.dockerfile index 84b31e5ca5b6..836b41c31693 100644 --- a/frameworks/PHP/openswoole/openswoole.dockerfile +++ b/frameworks/PHP/openswoole/openswoole.dockerfile @@ -1,18 +1,21 @@ -FROM php:8.1-cli +FROM php:8.2-cli + +RUN apt-get update && apt-get install -y git > /dev/null + +RUN docker-php-ext-install opcache pdo_mysql > /dev/null RUN pecl install openswoole > /dev/null && \ docker-php-ext-enable openswoole -RUN docker-php-ext-install opcache > /dev/null +COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer -WORKDIR /openswoole +COPY php.ini /usr/local/etc/php/ -COPY swoole-server.php swoole-server.php -RUN sed -i "s|DatabasePool('postgres|DatabasePool('mysql|g" swoole-server.php -RUN sed -i "s|_mysql||g" swoole-server.php +ADD ./ /openswoole +WORKDIR /openswoole -COPY php.ini /usr/local/etc/php/ +RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet EXPOSE 8080 -CMD php swoole-server.php +CMD php /openswoole/openswoole-server-mysql.php diff --git a/frameworks/PHP/openswoole/swoole-server.php b/frameworks/PHP/openswoole/swoole-server.php deleted file mode 100644 index 96c982f6c246..000000000000 --- a/frameworks/PHP/openswoole/swoole-server.php +++ /dev/null @@ -1,376 +0,0 @@ -set([ - 'worker_num' => swoole_cpu_num(), - 'log_file' => '/dev/null', - 'log_level' => 5, - 'open_tcp_nodelay' => true, -]); - -$pool = new \DatabasePool('postgres'); - -/** - * On start of the PHP worker. One worker per server process is started. - */ -$server->on('workerStart', function ($srv) use ($pool) { - $pool->init(\intdiv(512, $srv->setting['worker_num'])); -}); - -/** - * The DB test - * - * @param string $database_type - * @param int $queries - * - * @return string - */ -$db_postgres = function (int $queries = 0) use ($pool): string { - $db = $pool->get(); - // Read number of queries to run from URL parameter - $query_count = 1; - if ($queries > 1) { - $query_count = $queries > 500 ? 500 : $queries; - } - - // Create an array with the response string. - $arr = []; - - $db->s ??= $db->prepare('s', 'SELECT id, randomnumber FROM World WHERE id = $1'); - - // For each query, store the result set values in the response array - while ($query_count--) { - $id = mt_rand(1, 10000); - $res = $db->execute('s', [$id]); - $ret = $db->fetchAssoc($res); - // Store result in array. - $arr[] = ['id' => $id, 'randomnumber' => $ret['randomnumber']]; - } - - // Use the PHP standard JSON encoder. - // http://www.php.net/manual/en/function.json-encode.php - if ($queries === -1) { - $arr = $arr[0]; - } - - $pool->put($db); - - return \json_encode($arr, JSON_NUMERIC_CHECK); -}; - -/** - * The Fortunes test - * - * @param string $database_type - * - * @return string - */ -$fortunes_postgres = function () use ($pool): string { - $db = $pool->get(); - - $fortune = []; - - $db->f ??= $db->prepare('f', 'SELECT id, message FROM Fortune'); - $res = $db->execute('f', []); - $arr = $db->fetchAll($res); - - foreach ($arr as $row) { - $fortune[$row['id']] = $row['message']; - } - $fortune[0] = 'Additional fortune added at request time.'; - \asort($fortune); - - $html = ''; - foreach ($fortune as $id => $message) { - $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); - $html .= "{$id}{$message}"; - } - - $pool->put($db); - - return 'Fortunes' - .$html. - '
idmessage
'; -}; - -/** - * The Updates test - * - * @param string $database_type - * @param int $queries - * - * @return string - */ -$updates_postgres = function (int $queries = 0) use ($pool): string { - $db = $pool->get(); - - $query_count = 1; - if ($queries > 1) { - $query_count = $queries > 500 ? 500 : $queries; - } - - $arr = []; - - $db->us ??= $db->prepare('us', 'SELECT id,randomnumber FROM World WHERE id = $1'); - $db->uu ??= $db->prepare('uu', 'UPDATE World SET randomnumber = $1 WHERE id = $2'); - - while ($query_count--) { - $id = \mt_rand(1, 10000); - $randomNumber = \mt_rand(1, 10000); - $res = $db->execute('us', [$id]); - $ret = $db->fetchAssoc($res); - // Store result in array. - $world = ['id' => $id, 'randomnumber' => $ret['randomnumber']]; - $world['randomnumber'] = $randomNumber; - $res = $db->execute('uu', [$randomNumber, $id]); - $arr[] = $world; - } - - $pool->put($db); - - return \json_encode($arr, JSON_NUMERIC_CHECK); -}; - -/** - * The DB test - * - * @param string $database_type - * @param int $queries - * - * @return string - */ -$db_mysql = function (int $queries = 0) use ($pool): string { - $db = $pool->get(); - - // Read number of queries to run from URL parameter - $query_count = 1; - if ($queries > 1) { - $query_count = $queries > 500 ? 500 : $queries; - } - - // Create an array with the response string. - $arr = []; - // Define query - $db->db_test ??= $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?'); - - // For each query, store the result set values in the response array - while ($query_count--) { - $id = \mt_rand(1, 10000); - $ret = $db->db_test->execute([$id]); - - // Store result in array. - $arr[] = ['id' => $id, 'randomNumber' => $ret[0]['randomNumber']]; - } - - // Use the PHP standard JSON encoder. - // http://www.php.net/manual/en/function.json-encode.php - if ($queries === -1) { - $arr = $arr[0]; - } - - $pool->put($db); - - return \json_encode($arr, JSON_NUMERIC_CHECK); -}; - -/** - * The Fortunes test - * - * @param string $database_type - * - * @return string - */ -$fortunes_mysql = function () use ($pool): string { - $db = $pool->get(); - - $fortune = []; - - $db->fortune_test ??= $db->prepare('SELECT id, message FROM Fortune'); - $arr = $db->fortune_test->execute(); - - foreach ($arr as $row) { - $fortune[$row['id']] = $row['message']; - } - $fortune[0] = 'Additional fortune added at request time.'; - \asort($fortune); - - $html = ''; - foreach ($fortune as $id => $message) { - $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); - $html .= "{$id}{$message}"; - } - - $pool->put($db); - - return 'Fortunes' - .$html. - '
idmessage
'; -}; - -/** - * The Updates test - * - * @param string $database_type - * @param int $queries - * - * @return string - */ -$updates_mysql = function (int $queries = 0) use ($pool): string { - $db = $pool->get(); - - $query_count = 1; - if ($queries > 1) { - $query_count = $queries > 500 ? 500 : $queries; - } - - $arr = []; - $db->updates_test_select ??= $db->prepare('SELECT id,randomNumber FROM World WHERE id = ?'); - $db->updates_test_update ??= $db->prepare('UPDATE World SET randomNumber = ? WHERE id = ?'); - - while ($query_count--) { - $id = \mt_rand(1, 10000); - $randomNumber = \mt_rand(1, 10000); - $ret = $db->updates_test_select->execute([$id]); - - // Store result in array. - $world = ['id' => $id, 'randomNumber' => $ret[0]['randomNumber']]; - $world['randomNumber'] = $randomNumber; - $db->updates_test_update->execute([$randomNumber, $id]); - - $arr[] = $world; - } - - $pool->put($db); - - return \json_encode($arr, JSON_NUMERIC_CHECK); -}; - - -/** - * On every request to the (web)server, execute the following code - */ -$server->on('request', function (Request $req, Response $res) use ($db, $fortunes, $updates) { - try { - switch ($req->server['request_uri']) { - case '/json': - $res->header('Content-Type', 'application/json'); - $res->header('Server', 'openswoole'); - $res->end(json_encode(['message' => 'Hello, World!'])); - break; - - case '/plaintext': - $res->header('Content-Type', 'text/plain; charset=utf-8'); - $res->header('Server', 'openswoole'); - $res->end('Hello, World!'); - break; - - case '/db': - $res->header('Content-Type', 'application/json'); - $res->header('Server', 'openswoole'); - - if (isset($req->get['queries'])) { - $res->end($db((int)$req->get['queries'])); - } else { - $res->end($db(-1)); - } - break; - - case '/fortunes': - $res->header('Content-Type', 'text/html; charset=utf-8'); - $res->header('Server', 'openswoole'); - $res->end($fortunes()); - break; - - case '/updates': - $res->header('Content-Type', 'application/json'); - $res->header('Server', 'openswoole'); - - if (isset($req->get['queries'])) { - $res->end($updates((int)$req->get['queries'])); - } else { - $res->end($updates(-1)); - } - break; - - default: - $res->status(404); - $res->end('Error 404'); - - } - - } catch (\Throwable $e) { - $res->status(500); - $res->end('Error 500'); - } -}); - -$server->start(); - -/** - * Class DatabasePool - * - * Deal with the fact that Open Swoole 2.1.3 has no build in database pooling - */ -class DatabasePool -{ - private $server = [ - 'host' => '', - 'user' => 'benchmarkdbuser', - 'password' => 'benchmarkdbpass', - 'database' => 'hello_world' - ]; - - private $pool; - - private $type; - - public function __construct($type) - { - $this->server['host'] = \gethostbyname('tfb-database'); - $this->type = $type; - } - - public function init($capacity) - { - $this->pool=new \Swoole\Coroutine\Channel($capacity); - while($capacity>0){ - $db=$this->createDbInstance(); - if($db!==false){ - $this->pool->push($db); - $capacity--; - } - } - } - - private function createDbInstance() - { - if ($this->type === 'postgres') { - $db = new Swoole\Coroutine\PostgreSql; - if ($db->connect("host={$this->server['host']} port=5432 dbname={$this->server['database']} user={$this->server['user']} password={$this->server['password']}")){ - return $db; - } - } else if($this->type === 'mysql') { - $db = new Swoole\Coroutine\Mysql; - if ($db->connect($this->server)){ - return $db; - } - } - - return false; - } - - public function put($db) - { - $this->pool->push($db); - } - - public function get() - { - return $this->pool->pop(); - } -}