yii2通过apache重定向,提高10倍性能


声明:本文转载自https://my.oschina.net/penngo/blog/1794093,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

本测试代码基于yii2框架、apache服务器,apache、mysql、memcache都装在同一机器。
测试业务内容显示文章列表,如下图:


测试一、直接数据库访问方式,测试代码。

class ListController extends Controller {     public $layout = false;     public function actionDb()     {         $specialRecommend = new SpecialRecommend();         $page = empty($_GET['page']) == false ? intval($_GET['page']) : 1;         $pageSize = 10;         $rows = $specialRecommend->find()->offset(($page - 1) * $pageSize)->limit($pageSize)->orderBy("sid desc")->asArray()->all();         $html = $this->render('index', [             'rows' => $rows,         ]);         return $html;     } }


压测代码
ab -n 1000 -c 50  "http://testyii.com/index.php?r=list/db&page=1"
压测结果

吞吐率(rps):159.52[#/sec]

 

测试二、加入缓存,测试代码

public function actionCache() {     $page = empty($_GET['page']) == false ? intval($_GET['page']) : 1;     $pageSize = 10;      $key = "cacke_key";         $data = Yii::$app->cache->get($key);         $rows = json_decode($data, true);         if(empty($rows) == true){             $specialRecommend = new SpecialRecommend();             $rows = $specialRecommend->find()->offset(($page - 1) * $pageSize)->limit($pageSize)->orderBy("sid desc")->asArray()->all();             Yii::$app->cache->set($key, json_encode($rows));         }         $html = $this->render('index', [             'rows' => $rows,         ]);         return $html; }


压测代码
ab -n 1000 -c 50  "http://testyii.com/index.php?r=list/cache&page=1"
压测结果

吞吐率(rps):220.66[#/sec]


测试三、开启yii2伪静态,设置apache重定向时先判断目录是否存在,如果存在则直接访问真实目录
.htaccess

Options +FollowSymLinks     IndexIgnore */*     RewriteEngine on      # if a directory or a file exists, use it directly     RewriteCond %{REQUEST_FILENAME} !-f     RewriteCond %{REQUEST_FILENAME} !-d      # otherwise forward it to index.php     RewriteRule . index.php


    
测试代码    
   

public function actionFile()     {         $specialRecommend = new SpecialRecommend();         $page = empty($_GET['page']) == false ? intval($_GET['page']) : 1;         $pageSize = 10;         $rows = $specialRecommend->find()->offset(($page - 1) * $pageSize)->limit($pageSize)->orderBy("sid desc")->asArray()->all();         $html = $this->render('index', [             'rows' => $rows,         ]);         $path = Yii::$app->basePath . "/web/data/list/file/";         if(file_exists($path) == false){             mkdir($path, 0755, true);         }         $file = $path . "{$page}.html";         file_put_contents($file, $html);         return $html;     }


压测代码
ab -n 1000 -c 50  "http://testyii.com/data/list/file/1.html"
压测结果

吞吐率(rps):1327.14 [#/sec]

测试三在本地文件存在时,apache直接访问静态文件,不再需要php处理,rps比测试一性能提高接近10倍。

 

本文发表于2018年04月11日 22:38
(c)注:本文转载自https://my.oschina.net/penngo/blog/1794093,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1722 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1