Przeglądaj źródła

Add hyperf-raw and hyperf-micro (#5181)

* Use Task mode to render the view, and inject the dependecies via annotation.

* Removed useless abstract model

* Adjust the database connections

* Adjust the server config

* Add WaitGroup to wait the result of child coroutine

* Fixed queries and updates

* Fixed render

* Added hyperf-raw and hyperf-micro

* Remove useless config and add dockerfile

* Upgraded Hyperf to v1.1

* Fixed response format
黄朝晖 6 lat temu
rodzic
commit
dac0a39907

+ 70 - 0
frameworks/PHP/hyperf/app/Controller/IndexController.php

@@ -15,7 +15,9 @@ namespace App\Controller;
 use App\Model\Fortune;
 use App\Model\World;
 use App\Render;
+use Hyperf\DbConnection\Db;
 use Hyperf\Di\Annotation\Inject;
+use Hyperf\HttpMessage\Stream\SwooleStream;
 use Hyperf\HttpServer\Annotation\Controller;
 use Hyperf\HttpServer\Annotation\GetMapping;
 use Hyperf\HttpServer\Contract\ResponseInterface;
@@ -54,6 +56,14 @@ class IndexController
         return $this->response->json(World::find(random_int(1, 10000)));
     }
 
+    /**
+     * @GetMapping(path="/raw-db")
+     */
+    public function rawDb()
+    {
+        return $this->response->json(Db::select('SELECT id, randomNumber FROM World WHERE id = ?', [random_int(1, 10000)]));
+    }
+
     /**
      * @GetMapping(path="/queries/[{queries}]")
      */
@@ -70,6 +80,22 @@ class IndexController
         return $this->response->json($rows);
     }
 
+    /**
+     * @GetMapping(path="/raw-queries/[{queries}]")
+     */
+    public function rawQueries($queries = 1)
+    {
+        $queries = $this->clamp($queries);
+
+        $rows = [];
+
+        while ($queries--) {
+            $rows[] = Db::selectOne('SELECT id, randomNumber FROM World WHERE id = ?', [random_int(1, 10000)]);
+        }
+
+        return $this->response->json($rows);
+    }
+
     /**
      * @GetMapping(path="/fortunes")
      */
@@ -87,6 +113,30 @@ class IndexController
         return $this->render->render('fortunes', ['rows' => $rows]);
     }
 
+    /**
+     * @GetMapping(path="/micro-fortunes")
+     */
+    public function microFortunes()
+    {
+        $rows = Db::select('SELECT id, message FROM Fortune');
+
+        $fortune = [];
+        foreach ($rows ?? [] as $row) {
+            $fortune[$row->id] = $row->message;
+        }
+        $fortune[0] = 'Additional fortune added at request time.';
+        asort($fortune);
+
+        $html = '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>';
+        foreach ($fortune as $id => $message) {
+            $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
+            $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
+        }
+
+        $html .= '</table></body></html>';
+        return $this->response->withAddedHeader('content-type', 'text/html; charset=utf-8')->withBody(new SwooleStream($html));
+    }
+
     /**
      * @GetMapping(path="/updates/[{queries}]")
      */
@@ -106,6 +156,26 @@ class IndexController
         return $this->response->json($rows);
     }
 
+    /**
+     * @GetMapping(path="/raw-updates/[{queries}]")
+     */
+    public function rawUpdates($queries = 1)
+    {
+        $queries = $this->clamp($queries);
+
+        $rows = [];
+
+        while ($queries--) {
+            $row = Db::selectOne('SELECT id, randomNumber FROM World WHERE id = ?', [$id = random_int(1, 10000)]);
+            $rand = random_int(1, 10000);
+            $row->randomNumber = $rand;
+            Db::update('UPDATE World SET randomNumber = ? WHERE id = ?', [$rand, $id]);
+            $rows[] = $row;
+        }
+
+        return $this->response->json($rows);
+    }
+
     /**
      * @GetMapping(path="/plaintext")
      */

+ 38 - 0
frameworks/PHP/hyperf/benchmark_config.json

@@ -24,6 +24,44 @@
                 "display_name": "Hyperf",
                 "notes": "",
                 "versus": "swoole"
+            },
+            "raw": {
+                "db_url": "/raw-db",
+                "query_url": "/raw-queries/",
+                "update_url": "/raw-updates/",
+                "port": 9501,
+                "approach": "Realistic",
+                "classification": "Fullstack",
+                "database": "MySQL",
+                "framework": "Hyperf",
+                "language": "PHP",
+                "flavor": "None",
+                "orm": "Raw",
+                "platform": "swoole",
+                "webserver": "None",
+                "os": "Linux",
+                "database_os": "Linux",
+                "display_name": "Hyperf",
+                "notes": "",
+                "versus": "swoole"
+            },
+            "micro": {
+                "fortune_url": "/micro-fortunes",
+                "port": 9501,
+                "approach": "Realistic",
+                "classification": "Micro",
+                "database": "MySQL",
+                "framework": "Hyperf",
+                "language": "PHP",
+                "flavor": "None",
+                "orm": "Raw",
+                "platform": "swoole",
+                "webserver": "None",
+                "os": "Linux",
+                "database_os": "Linux",
+                "display_name": "Hyperf",
+                "notes": "",
+                "versus": "swoole"
             }
         }
     ]

+ 18 - 18
frameworks/PHP/hyperf/composer.json

@@ -14,24 +14,24 @@
     "require": {
         "php": ">=7.2",
         "ext-swoole": ">=4.2",
-        "hyperf/command": "~1.0.0",
-        "hyperf/config": "~1.0.0",
-        "hyperf/contract": "~1.0.0",
-        "hyperf/database": "~1.0.0",
-        "hyperf/db-connection": "~1.0.0",
-        "hyperf/di": "~1.0.0",
-        "hyperf/dispatcher": "~1.0.0",
-        "hyperf/event": "~1.0.0",
-        "hyperf/exception-handler": "~1.0.0",
-        "hyperf/framework": "~1.0.0",
-        "hyperf/http-server": "~1.0.0",
-        "hyperf/logger": "~1.0.0",
-        "hyperf/paginator": "~1.0.0",
-        "hyperf/pool": "~1.0.0",
-        "hyperf/utils": "~1.0.0",
-        "hyperf/view": "~1.0.0",
+        "hyperf/command": "~1.1.0",
+        "hyperf/config": "~1.1.0",
+        "hyperf/contract": "~1.1.0",
+        "hyperf/database": "~1.1.0",
+        "hyperf/db-connection": "~1.1.0",
+        "hyperf/di": "~1.1.0",
+        "hyperf/dispatcher": "~1.1.0",
+        "hyperf/event": "~1.1.0",
+        "hyperf/exception-handler": "~1.1.0",
+        "hyperf/framework": "~1.1.0",
+        "hyperf/http-server": "~1.1.0",
+        "hyperf/logger": "~1.1.0",
+        "hyperf/paginator": "~1.1.0",
+        "hyperf/pool": "~1.1.0",
+        "hyperf/utils": "~1.1.0",
+        "hyperf/view": "~1.1.0",
         "duncan3dc/blade": "^4.6",
-        "hyperf/task": "~1.0.0"
+        "hyperf/task": "~1.1.0"
     },
     "require-dev": {
         "swoft/swoole-ide-helper": "^4.2",
@@ -40,7 +40,7 @@
         "mockery/mockery": "^1.0",
         "doctrine/common": "^2.9",
         "phpstan/phpstan": "^0.11.2",
-        "hyperf/testing": "~1.0.0"
+        "hyperf/testing": "~1.1.0"
     },
     "suggest": {
         "ext-openssl": "Required to use HTTPS.",

+ 0 - 2
frameworks/PHP/hyperf/config/dependencies.php → frameworks/PHP/hyperf/config/autoload/dependencies.php

@@ -11,6 +11,4 @@ declare(strict_types=1);
  */
 
 return [
-    'dependencies' => [
-    ],
 ];

+ 7 - 17
frameworks/PHP/hyperf/config/container.php

@@ -1,7 +1,4 @@
 <?php
-/**
- * Initial a dependency injection container that implemented PSR-11 and return the container.
- */
 
 declare(strict_types=1);
 /**
@@ -13,24 +10,17 @@ declare(strict_types=1);
  * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
  */
 
-use Hyperf\Config\ProviderConfig;
-use Hyperf\Di\Annotation\Scanner;
 use Hyperf\Di\Container;
-use Hyperf\Di\Definition\DefinitionSource;
+use Hyperf\Di\Definition\DefinitionSourceFactory;
 use Hyperf\Utils\ApplicationContext;
+use Psr\Container\ContainerInterface;
 
-$configFromProviders = ProviderConfig::load();
-$definitions = include __DIR__ . '/dependencies.php';
-$serverDependencies = array_replace($configFromProviders['dependencies'] ?? [], $definitions['dependencies'] ?? []);
-
-$annotations = include __DIR__ . '/autoload/annotations.php';
-$scanDirs = $configFromProviders['scan']['paths'];
-$scanDirs = array_merge($scanDirs, $annotations['scan']['paths'] ?? []);
-
-$ignoreAnnotations = $annotations['scan']['ignore_annotations'] ?? ['mixin'];
-$container = new Container(new DefinitionSource($serverDependencies, $scanDirs, new Scanner($ignoreAnnotations)));
+/**
+ * Initial a dependency injection container that implemented PSR-11 and return the container.
+ */
+$container = new Container((new DefinitionSourceFactory(true))());
 
-if (! $container instanceof \Psr\Container\ContainerInterface) {
+if (! $container instanceof ContainerInterface) {
     throw new RuntimeException('The dependency injection container is invalid.');
 }
 return ApplicationContext::setContainer($container);

+ 57 - 0
frameworks/PHP/hyperf/hyperf-micro.dockerfile

@@ -0,0 +1,57 @@
+# Default Dockerfile
+#
+# @link     https://www.hyperf.io
+# @document https://doc.hyperf.io
+# @contact  [email protected]
+# @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
+
+FROM hyperf/hyperf:7.2-alpine-cli
+LABEL maintainer="Hyperf Developers <[email protected]>" version="1.0" license="MIT"
+
+##
+# ---------- env settings ----------
+##
+# --build-arg timezone=Asia/Shanghai
+ARG timezone
+
+ENV TIMEZONE=${timezone:-"America/Los_Angeles"} \
+    COMPOSER_VERSION=1.8.6 \
+    APP_ENV=prod
+
+# update
+RUN set -ex \
+    # install composer
+    && cd /tmp \
+    && wget https://github.com/composer/composer/releases/download/${COMPOSER_VERSION}/composer.phar \
+    && chmod u+x composer.phar \
+    && mv composer.phar /usr/local/bin/composer \
+    # show php version and extensions
+    && php -v \
+    && php -m \
+    #  ---------- some config ----------
+    && cd /etc/php7 \
+    # - config PHP
+    && { \
+        echo "upload_max_filesize=100M"; \
+        echo "post_max_size=108M"; \
+        echo "memory_limit=1024M"; \
+        echo "date.timezone=${TIMEZONE}"; \
+    } | tee conf.d/99-overrides.ini \
+    # - config timezone
+    && ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
+    && echo "${TIMEZONE}" > /etc/timezone \
+    # ---------- clear works ----------
+    && rm -rf /var/cache/apk/* /tmp/* /usr/share/man \
+    && echo -e "\033[42;37m Build Completed :).\033[0m\n"
+
+COPY . /opt/www
+
+WORKDIR /opt/www
+
+RUN composer install --no-dev \
+    && composer dump-autoload -o \
+    && php /opt/www/bin/hyperf.php di:init-proxy
+
+EXPOSE 9501
+
+ENTRYPOINT ["php", "/opt/www/bin/hyperf.php", "start"]

+ 57 - 0
frameworks/PHP/hyperf/hyperf-raw.dockerfile

@@ -0,0 +1,57 @@
+# Default Dockerfile
+#
+# @link     https://www.hyperf.io
+# @document https://doc.hyperf.io
+# @contact  [email protected]
+# @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
+
+FROM hyperf/hyperf:7.2-alpine-cli
+LABEL maintainer="Hyperf Developers <[email protected]>" version="1.0" license="MIT"
+
+##
+# ---------- env settings ----------
+##
+# --build-arg timezone=Asia/Shanghai
+ARG timezone
+
+ENV TIMEZONE=${timezone:-"America/Los_Angeles"} \
+    COMPOSER_VERSION=1.8.6 \
+    APP_ENV=prod
+
+# update
+RUN set -ex \
+    # install composer
+    && cd /tmp \
+    && wget https://github.com/composer/composer/releases/download/${COMPOSER_VERSION}/composer.phar \
+    && chmod u+x composer.phar \
+    && mv composer.phar /usr/local/bin/composer \
+    # show php version and extensions
+    && php -v \
+    && php -m \
+    #  ---------- some config ----------
+    && cd /etc/php7 \
+    # - config PHP
+    && { \
+        echo "upload_max_filesize=100M"; \
+        echo "post_max_size=108M"; \
+        echo "memory_limit=1024M"; \
+        echo "date.timezone=${TIMEZONE}"; \
+    } | tee conf.d/99-overrides.ini \
+    # - config timezone
+    && ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
+    && echo "${TIMEZONE}" > /etc/timezone \
+    # ---------- clear works ----------
+    && rm -rf /var/cache/apk/* /tmp/* /usr/share/man \
+    && echo -e "\033[42;37m Build Completed :).\033[0m\n"
+
+COPY . /opt/www
+
+WORKDIR /opt/www
+
+RUN composer install --no-dev \
+    && composer dump-autoload -o \
+    && php /opt/www/bin/hyperf.php di:init-proxy
+
+EXPOSE 9501
+
+ENTRYPOINT ["php", "/opt/www/bin/hyperf.php", "start"]