Browse Source

Add imi Caching test (#5860)

* Optimization config

* add cached_query_url test

* fix init

* fix init

* fix

* fix

* Remove non-essential redis

* fix

* fix
Yurun 5 years ago
parent
commit
e9db49ebbd

+ 2 - 0
frameworks/PHP/imi/.env-with-redis

@@ -0,0 +1,2 @@
[email protected]=16
+WITH_REDIS=1

+ 29 - 3
frameworks/PHP/imi/ApiServer/Controller/IndexController.php

@@ -1,15 +1,17 @@
 <?php
 namespace ImiApp\ApiServer\Controller;
 
+use Imi\Db\Db;
+use Imi\RequestContext;
 use ImiApp\Model\World;
 use ImiApp\Model\Fortune;
+use Imi\Redis\RedisManager;
+use Imi\Util\Stream\MemoryStream;
 use Imi\Controller\HttpController;
-use Imi\Db\Db;
-use Imi\RequestContext;
 use Imi\Server\View\Annotation\View;
+use Imi\Server\Route\Annotation\Route;
 use Imi\Server\Route\Annotation\Action;
 use Imi\Server\Route\Annotation\Controller;
-use Imi\Util\Stream\MemoryStream;
 
 /**
  * @Controller("/")
@@ -281,4 +283,28 @@ class IndexController extends HttpController
         return $list;
     }
 
+    /**
+     * @Action
+     * @Route("cached-worlds")
+     *
+     * @return void
+     */
+    public function cachedWorlds($count)
+    {
+        if($count > 1)
+        {
+            $queryCount = min($count, 500);
+        }
+        else
+        {
+            $queryCount = 1;
+        }
+        $ids = [];
+        while ($queryCount--)
+        {
+            $ids[] = 'world:' . mt_rand(1, 10000);
+        }
+        return RedisManager::getInstance()->mget($ids);
+    }
+
 }

+ 39 - 0
frameworks/PHP/imi/Listener/AppInit.php

@@ -0,0 +1,39 @@
+<?php
+namespace ImiApp\Listener;
+
+use Imi\Db\Db;
+use Imi\Event\EventParam;
+use Imi\Redis\RedisManager;
+use Imi\Event\IEventListener;
+use Imi\Bean\Annotation\Listener;
+
+/**
+ * @Listener("IMI.APP.INIT")
+ */
+class AppInit implements IEventListener
+{
+    /**
+     * 事件处理方法
+     * @param EventParam $e
+     * @return void
+     */
+    public function handle(EventParam $e)
+    {
+        if(getenv('WITH_REDIS') ?? false)
+        {
+            $redis = RedisManager::getInstance();
+            $page = 1;
+            while($list = Db::query()->from('world')->page($page, 1000)->select()->getArray())
+            {
+                $redisList = [];
+                foreach($list as $row)
+                {
+                    $redisList['world:' . $row['id']] = $row;
+                }
+                $redis->mset($redisList);
+                ++$page;
+            }
+        }
+    }
+
+}

+ 1 - 0
frameworks/PHP/imi/benchmark_config.json

@@ -9,6 +9,7 @@
         "query_url": "/queryModel?queries=",
         "fortune_url": "/fortunes",
         "update_url": "/updateModel?queries=",
+        "cached_query_url": "/cached-worlds?count=",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Fullstack",

+ 0 - 3
frameworks/PHP/imi/config/beans.php

@@ -4,8 +4,5 @@ return [
         'status'    =>    false, // 关闭热更新去除注释,不设置即为开启,建议生产环境关闭
     ],
     'Logger'    =>    [
-        'coreHandlers'    =>    [
-            
-        ],
     ],
 ];

+ 34 - 16
frameworks/PHP/imi/config/config.php

@@ -15,7 +15,9 @@ return [
     ],
 
     // 扫描目录
-    'beanScan'    =>    [],
+    'beanScan'    =>    [
+        'ImiApp\Listener',
+    ],
 
     // 组件命名空间
     'components'    =>  [],
@@ -31,30 +33,21 @@ return [
             'worker_num'        => swoole_cpu_num(),
             'open_tcp_nodelay'  => true,
             'tcp_fastopen'      => true,
+            'http_parse_post'   => false,
+            'http_parse_cookie' => false,
         ],
     ],
 
     'db'    => [
         'defaultPool'   => 'db', // 默认连接池
     ],
+    'redis'    =>    [
+        'defaultPool'               =>    'redis', // 默认连接池
+        'quickFromRequestContext'   =>    true, // 从当前上下文中获取公用连接
+    ],
     'pools' => [
         // 连接池名称
         'db' => [
-            'sync' => [
-                'pool'    =>    [
-                    'class'        =>    \Imi\Db\Pool\SyncDbPool::class,
-                    'config'    =>    [
-                        // 池子中最多资源数
-                        'maxResources' => 512,
-                        // 池子中最少资源数
-                        'minResources' => 0,
-                        'gcInterval'   => null,
-                        'checkStateWhenGetResource' =>  false,
-                    ],
-                ],
-                // resource也可以定义多个连接
-                'resource'    =>    $dbResourceConfig,
-            ],
             // 异步池子,worker进程使用
             'async' => [
                 'pool'    =>    [
@@ -72,5 +65,30 @@ return [
                 'resource'    =>    $dbResourceConfig,
             ],
         ],
+        'redis' =>  [
+            'pool' => [
+                // 协程池类名
+                'asyncClass'    =>    \Imi\Redis\CoroutineRedisPool::class,
+                'config' => [
+                    // 池子中最多资源数
+                    'maxResources' => 512,
+                    // 池子中最少资源数
+                    'minResources' => 0,
+                    'gcInterval'   => null,
+                    'checkStateWhenGetResource' =>  false,
+                ],
+            ],
+            // 数组资源配置
+            'resource' => [
+                'host'      =>  '127.0.0.1',
+                'port'      =>  6379,
+                // 是否自动序列化变量
+                'serialize' =>  true,
+                // 密码
+                'password'  =>  null,
+                // 第几个库
+                'db'        =>  0,
+            ],
+        ],
     ],
 ];

+ 3 - 1
frameworks/PHP/imi/imi-query-builder.dockerfile

@@ -8,11 +8,13 @@ RUN docker-php-ext-install pdo_mysql > /dev/null
 RUN apt -yqq update > /dev/null && \
     apt -yqq install git unzip > /dev/null
 
-WORKDIR /imi
+RUN echo "zend_extension=opcache.so" >> /usr/local/etc/php/php.ini
 
 COPY . /imi
 COPY php.ini /usr/local/etc/php/
 
+WORKDIR /imi
+
 RUN chmod -R ug+rwx /imi/.runtime
 
 RUN curl -sSL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

+ 3 - 1
frameworks/PHP/imi/imi-raw.dockerfile

@@ -8,11 +8,13 @@ RUN docker-php-ext-install pdo_mysql > /dev/null
 RUN apt -yqq update > /dev/null && \
     apt -yqq install git unzip > /dev/null
 
-WORKDIR /imi
+RUN echo "zend_extension=opcache.so" >> /usr/local/etc/php/php.ini
 
 COPY . /imi
 COPY php.ini /usr/local/etc/php/
 
+WORKDIR /imi
+
 RUN chmod -R ug+rwx /imi/.runtime
 
 RUN curl -sSL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

+ 10 - 2
frameworks/PHP/imi/imi.dockerfile

@@ -5,18 +5,26 @@ RUN pecl install swoole > /dev/null && \
 
 RUN docker-php-ext-install pdo_mysql > /dev/null
 
+RUN pecl install redis > /dev/null && \
+    docker-php-ext-enable redis
+
 RUN apt -yqq update > /dev/null && \
     apt -yqq install git unzip > /dev/null
 
-WORKDIR /imi
+RUN apt -yqq install redis-server > /dev/null
+
+RUN echo "zend_extension=opcache.so" >> /usr/local/etc/php/php.ini
 
 COPY . /imi
 COPY php.ini /usr/local/etc/php/
 
+WORKDIR /imi
+COPY .env-with-redis .env
+
 RUN chmod -R ug+rwx /imi/.runtime
 
 RUN curl -sSL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
 RUN composer install --no-dev --classmap-authoritative --quiet > /dev/null
 RUN composer dumpautoload -o
 
-CMD php vendor/bin/imi server/start
+CMD ./run-with-redis.sh

+ 3 - 0
frameworks/PHP/imi/run-with-redis.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+service redis-server start
+php vendor/bin/imi server/start