Browse Source

[php] With Workerman without worker mode (#7798)

Joan Miquel 2 years ago
parent
commit
826b8e67c8

+ 23 - 0
frameworks/PHP/php/benchmark_config.json

@@ -183,6 +183,29 @@
       "notes": "",
       "versus": "php"
     },
+    "workerman": {
+      "json_url": "/json.php",
+      "plaintext_url": "/plaintext.php",
+      "db_url": "/dbraw.php",
+      "query_url": "/dbquery.php?queries=",
+      "fortune_url": "/fortune.php",
+      "update_url": "/updateraw.php?queries=",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "database": "MySQL",
+      "framework": "PHP",
+      "language": "PHP",
+      "flavor": "PHP8",
+      "orm": "Raw",
+      "platform": "Workerman",
+      "webserver": "none",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "php-workerman",
+      "notes": "Workerman without worker mode",
+      "versus": "php"
+    },
     "eloquent": {
       "db_url": "/eloquent/db-eloquent.php",
       "query_url": "/eloquent/db-eloquent.php?queries=",

+ 16 - 0
frameworks/PHP/php/deploy/workerman/cli-php.ini

@@ -0,0 +1,16 @@
+opcache.enable=1
+opcache.enable_cli=1
+opcache.validate_timestamps=0
+opcache.save_comments=0
+opcache.enable_file_override=1
+opcache.memory_consumption=256
+opcache.interned_strings_buffer=16
+opcache.huge_code_pages=1
+
+mysqlnd.collect_statistics = Off
+memory_limit = 512M
+
+opcache.jit_buffer_size=128M
+opcache.jit=tracing
+
+disable_functions=header,header_remove,headers_sent,http_response_code,setcookie,session_create_id,session_id,session_name,session_save_path,session_status,session_start,session_write_close,set_time_limit

+ 5 - 0
frameworks/PHP/php/deploy/workerman/composer.json

@@ -0,0 +1,5 @@
+{
+    "require": {
+      "joanhey/adapterman": "0.5"
+    }
+  }

+ 77 - 0
frameworks/PHP/php/deploy/workerman/start.php

@@ -0,0 +1,77 @@
+<?php
+
+use Adapterman\Adapterman;
+use Workerman\Worker;
+use Workerman\Lib\Timer;
+
+require_once __DIR__ . '/vendor/autoload.php';
+
+Adapterman::init();
+// WebServer
+$web = new Worker("http://0.0.0.0:8080");
+$web->count = (int) shell_exec('nproc') * 4;
+$web->name = 'workerman';
+
+define('WEBROOT', '/php/');
+
+$web->onWorkerStart = static function () {
+    Header::init();
+};
+
+$web->onMessage = static function ($connection, $request) {
+    $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
+    /* if ($path === '/') {
+        $connection->send(exec_php_file(WEBROOT.'/index.php', $request));
+        return;
+    } */
+
+    $file = realpath(WEBROOT . $path);
+    if (false === $file || !str_ends_with($file, '.php')) {
+        http_response_code(404);
+        $connection->send('<h3>404 Not Found</h3>');
+        return;
+    }
+    // Security check!
+    if (!str_starts_with($file, WEBROOT)) {
+        http_response_code(400);
+        $connection->send('<h3>400 Bad Request</h3>');
+        return;
+    }
+
+    header(Header::$date); // To pass the bench
+    $connection->send(exec_php_file($file));
+};
+
+function exec_php_file($file)
+{
+    ob_start();
+    // Try to include php file.
+    try {
+        include $file;
+    } catch (\Exception $e) {
+        echo $e;
+    }
+    return ob_get_clean();
+}
+
+class Header
+{
+    const NAME = 'Date: ';
+    
+    /**
+     * Date header
+     *
+     * @var string
+     */
+    public static $date;
+
+    public static function init(): void
+    {
+        self::$date = self::NAME . gmdate('D, d M Y H:i:s').' GMT';
+        Timer::add(1, static function() {
+            self::$date = self::NAME . gmdate('D, d M Y H:i:s').' GMT';
+        });
+    }
+}
+
+Worker::runAll();

+ 31 - 0
frameworks/PHP/php/php-workerman.dockerfile

@@ -0,0 +1,31 @@
+FROM ubuntu:22.04
+
+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 
+
+RUN apt-get install -yqq git unzip \
+    php8.2 php8.2-common php8.2-cli php8.2-fpm php8.2-mysql  > /dev/null
+
+RUN apt-get install -y php-pear php8.2-dev libevent-dev > /dev/null
+RUN pecl install event-3.0.8 > /dev/null && echo "extension=event.so" > /etc/php/8.2/cli/conf.d/event.ini
+
+COPY deploy/workerman/cli-php.ini /etc/php/8.2/cli/php.ini
+
+COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
+
+ADD ./ /php
+WORKDIR /php
+
+COPY deploy/workerman/composer.json ./
+RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
+
+COPY deploy/workerman/start.php ./
+
+RUN chmod -R 777 /php
+
+EXPOSE 8080
+
+CMD php start.php start