Browse Source

[yii2] Add workerman variant (#7356)

Joan Miquel 3 years ago
parent
commit
3cf66934fd

+ 9 - 1
frameworks/PHP/yii2/app/index.php

@@ -51,4 +51,12 @@ $config = [
     ],
     ],
 ];
 ];
 
 
-(new yii\web\Application($config))->run();
+(new  yii\web\Application($config))->run();
+
+function handleWorkerman()
+{
+    global $config;
+    ob_start();
+    (new yii\web\Application($config))->run();
+    return ob_get_clean();
+}

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

@@ -43,6 +43,29 @@
       "display_name": "yii2-raw",
       "display_name": "yii2-raw",
       "notes": "",
       "notes": "",
       "versus": "php"
       "versus": "php"
+    },
+    "workerman": {
+      "json_url": "/site/json",
+      "db_url": "/site/db",
+      "query_url": "/site/queries?queries=",
+      "fortune_url": "/site/fortunes",
+      "update_url": "/site/updates?queries=",
+      "plaintext_url": "/site/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MySQL",
+      "framework": "yii2",
+      "language": "PHP",
+      "flavor": "PHP8.1",
+      "orm": "Full",
+      "platform": "workerman",
+      "webserver": "none",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "yii2-workerman",
+      "notes": "",
+      "versus": "workerman"
     }
     }
   }]
   }]
 }
 }

+ 2 - 1
frameworks/PHP/yii2/composer.json

@@ -1,6 +1,7 @@
 {
 {
 	"require": {
 	"require": {
 		"yidas/yii2-composer-bower-skip": "~2.0.13",
 		"yidas/yii2-composer-bower-skip": "~2.0.13",
-		"yiisoft/yii2": "~2.0.43"
+		"yiisoft/yii2": "~2.0.43",
+		"joanhey/adapterman": "dev-master"
 	}
 	}
 }
 }

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

@@ -0,0 +1,16 @@
+#zend_extension=opcache.so
+opcache.enable=1
+opcache.enable_cli=1
+opcache.validate_timestamps=0
+opcache.save_comments=0
+opcache.enable_file_override=1
+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

+ 44 - 0
frameworks/PHP/yii2/server.php

@@ -0,0 +1,44 @@
+<?php
+require_once __DIR__ . '/vendor/autoload.php';
+
+use Adapterman\Adapterman;
+use Workerman\Worker;
+use Workerman\Lib\Timer;
+use Workerman\Protocols\Http;
+
+Adapterman::init();
+
+require_once __DIR__.'/app/index.php';
+
+$http_worker                = new Worker('http://0.0.0.0:8080');
+$http_worker->count         = (int) shell_exec('nproc') * 4;
+$http_worker->name          = 'AdapterMan';
+$http_worker->onWorkerStart = function () {
+    WorkerTimer::init();
+    //init();
+};
+
+$http_worker->onMessage = static function ($connection, $request) {
+ 
+    $_SERVER['SCRIPT_FILENAME'] = '/app/index.php';
+    $_SERVER['SCRIPT_NAME'] = '/index.php';
+    Http::header(WorkerTimer::$date);
+    $connection->send(
+        handleWorkerman()
+    );
+};
+
+class WorkerTimer
+{
+    public static $date;
+
+    public static function init()
+    {
+        self::$date = 'Date: '.gmdate('D, d M Y H:i:s').' GMT';
+        Timer::add(1, function() {
+            WorkerTimer::$date = 'Date: '.gmdate('D, d M Y H:i:s').' GMT';
+        });
+    }
+}
+
+Worker::runAll();

+ 21 - 0
frameworks/PHP/yii2/yii2-workerman.dockerfile

@@ -0,0 +1,21 @@
+FROM php:8.1-cli
+
+RUN docker-php-ext-install opcache pcntl pdo_mysql
+COPY deploy/conf/cli-php.ini /usr/local/etc/php/php.ini
+
+RUN apt-get update -yqq > /dev/null && \
+    apt-get install -yqq git
+
+ADD ./ /yii2
+WORKDIR /yii2
+
+RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer 
+
+RUN composer install --optimize-autoloader --classmap-authoritative --no-dev
+
+RUN sed -i 's|(new  yii\\web\\Application|//(new  yii\\web\\Application|' app/index.php
+RUN sed -i 's|(headers_sent($file, $line))|(headers_sent())|g' vendor/yiisoft/yii2/web/Response.php
+
+RUN chmod -R 777 /yii2
+
+CMD php server.php start