Browse Source

Optimise imi (#6046)

* Use swoole db driver

* Fix

* Optimise

* Optimise

* Optimise
Yurun 4 years ago
parent
commit
69b5ba56fe

+ 24 - 27
frameworks/PHP/imi/ApiServer/Controller/IndexController.php

@@ -46,7 +46,7 @@ class IndexController extends HttpController
      */
     public function dbModel()
     {
-        return World::find(mt_rand(1, 10000));
+        return World::find(\mt_rand(1, 10000));
     }
 
     /**
@@ -56,7 +56,7 @@ class IndexController extends HttpController
      */
     public function dbQueryBuilder()
     {
-        return Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', mt_rand(1, 10000))->select()->get();
+        return Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', \mt_rand(1, 10000))->select()->get();
     }
 
     /**
@@ -68,7 +68,7 @@ class IndexController extends HttpController
     {
         $db = Db::getInstance();
         $stmt = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
-        $stmt->execute([mt_rand(1, 10000)]);
+        $stmt->execute([\mt_rand(1, 10000)]);
         return $stmt->fetch();
     }
 
@@ -81,7 +81,7 @@ class IndexController extends HttpController
     {
         if($queries > 1)
         {
-            $queryCount = min($queries, 500);
+            $queryCount = \min($queries, 500);
         }
         else
         {
@@ -90,7 +90,7 @@ class IndexController extends HttpController
         $list = [];
         while ($queryCount--)
         {
-            $list[] = World::find(mt_rand(1, 10000));
+            $list[] = World::find(\mt_rand(1, 10000));
         }
         return $list;
     }
@@ -104,7 +104,7 @@ class IndexController extends HttpController
     {
         if($queries > 1)
         {
-            $queryCount = min($queries, 500);
+            $queryCount = \min($queries, 500);
         }
         else
         {
@@ -113,7 +113,7 @@ class IndexController extends HttpController
         $list = [];
         while ($queryCount--)
         {
-            $list[] = Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', mt_rand(1, 10000))->select()->get();
+            $list[] = Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', \mt_rand(1, 10000))->select()->get();
         }
         return $list;
     }
@@ -127,7 +127,7 @@ class IndexController extends HttpController
     {
         if($queries > 1)
         {
-            $queryCount = min($queries, 500);
+            $queryCount = \min($queries, 500);
         }
         else
         {
@@ -138,7 +138,7 @@ class IndexController extends HttpController
         $stmt = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
         while ($queryCount--)
         {
-            $stmt->execute([mt_rand(1, 10000)]);
+            $stmt->execute([\mt_rand(1, 10000)]);
             $list[] = $stmt->fetch();
         }
         return $list;
@@ -162,7 +162,7 @@ class IndexController extends HttpController
             $rows[$item->id] = $item->message;
         }
         $rows[0] = 'Additional fortune added at request time.';
-        asort($rows);
+        \asort($rows);
         return [
             'rows'  =>  $rows,
         ];
@@ -182,7 +182,7 @@ class IndexController extends HttpController
             $rows[$item['id']] = $item['message'];
         }
         $rows[0] = 'Additional fortune added at request time.';
-        asort($rows);
+        \asort($rows);
 
         $html = '';
         foreach ($rows as $id => $message)
@@ -204,7 +204,7 @@ class IndexController extends HttpController
     {
         if($queries > 1)
         {
-            $queryCount = min($queries, 500);
+            $queryCount = \min($queries, 500);
         }
         else
         {
@@ -213,8 +213,8 @@ class IndexController extends HttpController
         $list = [];
         while ($queryCount--)
         {
-            $list[] = $row = World::find(mt_rand(1, 10000));
-            $row->randomNumber = mt_rand(1, 10000);
+            $list[] = $row = World::find(\mt_rand(1, 10000));
+            $row->randomNumber = \mt_rand(1, 10000);
             $row->update();
         }
         return $list;
@@ -229,7 +229,7 @@ class IndexController extends HttpController
     {
         if($queries > 1)
         {
-            $queryCount = min($queries, 500);
+            $queryCount = \min($queries, 500);
         }
         else
         {
@@ -238,9 +238,9 @@ class IndexController extends HttpController
         $list = [];
         while ($queryCount--)
         {
-            $id = mt_rand(1, 10000);
+            $id = \mt_rand(1, 10000);
             $row = Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', $id)->select()->get();
-            $row['randomNumber'] = mt_rand(1, 10000);
+            $row['randomNumber'] = \mt_rand(1, 10000);
             Db::query()->from('World')->where('id', '=', $row['id'])->update([
                 'randomNumber'  =>  $row['randomNumber'],
             ]);
@@ -258,7 +258,7 @@ class IndexController extends HttpController
     {
         if($queries > 1)
         {
-            $queryCount = min($queries, 500);
+            $queryCount = \min($queries, 500);
         }
         else
         {
@@ -267,17 +267,14 @@ class IndexController extends HttpController
         $list = [];
         $db = Db::getInstance();
         $stmtSelect = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
-        $stmtUpdate = $db->prepare('UPDATE World SET randomNumber = :randomNumber WHERE id = :id');
+        $stmtUpdate = $db->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
         while ($queryCount--)
         {
-            $id = mt_rand(1, 10000);
+            $id = \mt_rand(1, 10000);
             $stmtSelect->execute([$id]);
             $row = $stmtSelect->fetch();
-            $row['randomNumber'] = mt_rand(1, 10000);
-            $stmtUpdate->execute([
-                'id'            =>  $row['id'],
-                'randomNumber'  =>  $row['randomNumber'],
-            ]);
+            $row['randomNumber'] = \mt_rand(1, 10000);
+            $stmtUpdate->execute([$row['randomNumber'], $row['id']]);
             $list[] = $row;
         }
         return $list;
@@ -293,7 +290,7 @@ class IndexController extends HttpController
     {
         if($count > 1)
         {
-            $queryCount = min($count, 500);
+            $queryCount = \min($count, 500);
         }
         else
         {
@@ -302,7 +299,7 @@ class IndexController extends HttpController
         $ids = [];
         while ($queryCount--)
         {
-            $ids[] = 'world:' . mt_rand(1, 10000);
+            $ids[] = 'world:' . \mt_rand(1, 10000);
         }
         return RedisManager::getInstance()->mget($ids);
     }

+ 1 - 0
frameworks/PHP/imi/Main.php

@@ -9,6 +9,7 @@ class Main extends AppBaseMain
     public function __init()
     {
         // 这里可以做一些初始化操作,如果需要的话
+        ini_set('memory_limit', -1);
         App::setDebug(true);
     }
 

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

@@ -4,6 +4,7 @@ $dbResourceConfig = [
     'username'    => 'benchmarkdbuser',
     'password'    => 'benchmarkdbpass',
     'database'    => 'hello_world',
+    'dbClass'     => \Imi\Db\Drivers\Swoole\Driver::class,
 ];
 return [
     // 项目根命名空间
@@ -59,6 +60,7 @@ return [
                         'minResources' => 16,
                         'gcInterval'   => null,
                         'checkStateWhenGetResource' =>  false,
+                        'requestResourceCheckInterval' => 30,
                     ],
                 ],
                 // resource也可以定义多个连接
@@ -76,6 +78,7 @@ return [
                     'minResources' => 0,
                     'gcInterval'   => null,
                     'checkStateWhenGetResource' =>  false,
+                    'requestResourceCheckInterval' => 30,
                 ],
             ],
             // 数组资源配置