6、各項實踐,性能評測
下面進入性能評測,評測咱們相對就比較快速一些。直接用ab命令,來測試上面的所說起的一些改進。
如下評測,全部測試頁面,均爲:http://hjvote.app.ucai.cn/index.php 命令行爲:
ab -c 20 -n 1000 http://hjvote.app.ucai.cn/index.phpphp
6.1 、冷啓動
一、冷啓動,好比我新啓動的php-fpm,關掉opcache,關掉xhprofmysql
Concurrency Level: 20 Time taken for tests: 4.981 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 478000 bytes HTML transferred: 109000 bytes Requests per second: 200.78 [#/sec] (mean) Time per request: 99.612 [ms] (mean) Time per request: 4.981 [ms] (mean, across all concurrent requests) Transfer rate: 93.72 [Kbytes/sec] received
6.二、第二次
二、第二次,條件同上。sql
Concurrency Level: 20 Time taken for tests: 4.537 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 478000 bytes HTML transferred: 109000 bytes Requests per second: 220.42 [#/sec] (mean) Time per request: 90.736 [ms] (mean) Time per request: 4.537 [ms] (mean, across all concurrent requests) Transfer rate: 102.89 [Kbytes/sec] received **6.三、打開opcache第一次** Concurrency Level: 20 Time taken for tests: 1.591 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 478000 bytes HTML transferred: 109000 bytes Requests per second: 628.67 [#/sec] (mean) Time per request: 31.813 [ms] (mean) Time per request: 1.591 [ms] (mean, across all concurrent requests) Transfer rate: 293.46 [Kbytes/sec] received
6.四、第二次,條件同上數據庫
Concurrency Level: 20 Time taken for tests: 1.254 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 478000 bytes HTML transferred: 109000 bytes Requests per second: 797.70 [#/sec] (mean) Time per request: 25.072 [ms] (mean) Time per request: 1.254 [ms] (mean, across all concurrent requests) Transfer rate: 372.36 [Kbytes/sec] received
6.五、對比再打開xhprofsegmentfault
Concurrency Level: 20 Time taken for tests: 1.254 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 478000 bytes HTML transferred: 109000 bytes Requests per second: 797.44 [#/sec] (mean) Time per request: 25.080 [ms] (mean) Time per request: 1.254 [ms] (mean, across all concurrent requests) Transfer rate: 372.24 [Kbytes/sec] received
6.六、打開XHprof,關掉Opcache
對於這個簡單的頁面,沒有明顯惡化,咱們去掉Opcache,打開xhprof後端
Concurrency Level: 20 Time taken for tests: 12.103 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 595000 bytes HTML transferred: 226000 bytes Requests per second: 82.62 [#/sec] (mean) Time per request: 242.065 [ms] (mean) Time per request: 12.103 [ms] (mean, across all concurrent requests) Transfer rate: 48.01 [Kbytes/sec] received
6.七、第二次、條件同上瀏覽器
Concurrency Level: 20 Time taken for tests: 9.298 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 595000 bytes HTML transferred: 226000 bytes Requests per second: 107.55 [#/sec] (mean) Time per request: 185.952 [ms] (mean) Time per request: 9.298 [ms] (mean, across all concurrent requests) Transfer rate: 62.50 [Kbytes/sec] received
在沒有opcache的狀況下,XHProf的加入,致使用戶急劇變慢。安全
6.八、關掉xhprof HHVM第一次架構
Concurrency Level: 20 Time taken for tests: 1.142 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 521000 bytes HTML transferred: 109000 bytes Requests per second: 875.84 [#/sec] (mean) Time per request: 22.835 [ms] (mean) Time per request: 1.142 [ms] (mean, across all concurrent requests) Transfer rate: 445.62 [Kbytes/sec] received
6.九、 第二次,條件同上app
Concurrency Level: 20 Time taken for tests: 0.852 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 521000 bytes HTML transferred: 109000 bytes Requests per second: 1173.42 [#/sec] (mean) Time per request: 17.044 [ms] (mean) Time per request: 0.852 [ms] (mean, across all concurrent requests) Transfer rate: 597.03 [Kbytes/sec] received 什麼感受?這種數字的差距是否是能夠用震撼來形容?經過評測對比,咱們對於opcache、hhvm和通常php-fpm性能內心也就有數了。同時也發現,在頁面上開啓XHProf,會致使網頁性能急劇降低,因此不要在生產環境對多人開啓XHProf。或者會帶來很是很差的用戶體驗。
7、自有框架的設計
好的,看完了性能評測,咱們來過了一下若是設計一個自有的框架須要哪些元素,或者說須要哪些內容。
一、首先對目錄結構進行一個劃分,肯定目錄層次結構
–app 命令行應用
–data 存放數據上傳
–lib 庫函數
–template 模板
–conf 各類配置
–doc 文檔,SQL
–log 日誌
–test 測試代碼
–ctemplate 編譯後的模板
–htdocs Web主目錄
你們能看到,htdocs 同其餘目錄,好比 doc、app目錄爲何要並列?這是安全性的考慮,想想,不然的話,你的表定義被人下走了。你的後臺程序,可能會被用戶執行。
二、其次來看一下類的層次結構。
下圖是應用程序各個類的分層。
最基層的應用程序類,其實就是一個骨架。包括了getParam(取得參數,不管命令行,仍是Web訪問,都須要有參數分析)。checkParam(參數檢測)、checkAuth(權限檢測,好比是不是須要登陸的一個頁面)、outputPage等這些方法,全是空方話,可是由一個run方法串起來。以下:
public function run() { $this->getPara(); $this->checkPara(); $this->checkAuth(); $this->main(); $this->outputPage(); $this->exitApp(); }
應用程序基類DCore_BaseApp.php的子類又分爲三個,一個負責命令行程序的處理DCore_ConsoleApp.php,另一個負責像移動應用、開放平臺Api之類的數據處理DCore_ApiApp.php ,而第三個,則是DCore_WebApp.php, 咱們所在瀏覽器上所訪問的應用,由這個應用程序類派生出來頁面類。而DCore_AdminApp.php,是管理後臺應用程序類的基類。由於後端通常有不同的權限認證機制。這樣也是爲了把先後臺的應用程序類區分得更加清楚。
這樣能夠看到,總體結構很是清晰。
好,那最後咱們再來看一下,須要編寫哪些類庫。
在咱們的框架中,編寫了這些。
•一、經常使用工具函數庫
實現字符串的經常使用操做封裝,好比中文取字串,繁簡轉換、編碼轉換
•二、模板引擎
能夠簡化爲包含PHP,在咱們本身開發的這個框架中,支持了模板編譯,其實模板編譯很簡單,就是將一些特定的語法,換成PHP代碼,而後仍是包含PHP
•三、路由控制,靜態化
用戶能夠將路徑改爲搜索引擎更友好的路徑,程序也能解析正確。
•四、後端數據請求控制
用於對後端一些公用操做的封裝,好比數據庫,好比Memcached,這樣的封裝帶來的好處是,若是一旦發生升級或者替換的時候,修改的代碼相對最少。好比你不用處處去改mysql_query,只須要修改當前這個庫文件便可以了。
8、總結 好的,今天的課程就到這裏。總結一下,咱們講了以下幾點: 一、5月課程尤爲是5月份框架課程的總結。總結了框架與架構的區別。 二、站在PHP框架以外,看框架,看框架的共同特徵與功用。 三、以PHP框架爲例,講框架所不能解決或者帶來的問題。 四、因爲框架所帶來的問題,以性能、可擴展問題,相對嚴重,因此分析PHP性能 的改造方向,總結了六大點。 五、分別演示了這六大點的改進實踐。包括Yaf、Phalcon框架介紹,zephir的使用,以及HHVM。 六、彙總六大點的改進,並作了相關的性能評測。能看到,使用不一樣的技術差異巨大,因此咱們要在穩定可靠的狀況下,儘量採用最好的技術。 七、最後講解了開發一個自有框架,通常都是一個什麼樣的思路。