php 效能測試
最後更新:2016-06-23
來源:互聯網
上載者:User
PHP測試方案
一、 效能測試(xhprof)
1)、安裝
Wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xzvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension
/usr/local/php-5.3.5/bin/phpize
./configure ?with-php-config=/usr/local/php-5.3.5/bin/php-config
make && make install
2)、配置PHP
在php.ini檔案中加入xhprof.so模組
extension = /usr/local/php-5.3.6/lib/php/extension/xxxx/xhprof.so
儲存並關閉,然後重啟PHP
把xhprof_lib和xhprof_html兩個目錄拷貝到網站的根目錄”xxx.xxxx.com/xhprof/”下,
2、使用
在需要做效能測試的地方加入
開始:
// XHPROF_FLAGS_CPU
// XHPROF_FLAGS_MEMORY
// XHPROF_FLAGS_NO_BUILTINS 跳過內建函數
// 要設定要忽略的函數列表,可以在分析時給xhprof_enable函數 指定第二個參數[是個選擇性參數], 如:
xhprof_enable(XHPROF_FLAGS_MEMORY,
array(
'ignored_functions' => array(
'call_user_func',
'call_user_func_array'
)
)
);
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
結束:
$xhprofData = xhprof_disable();
define('XHPROF_ROOT', Yii::getPathOfAlias('webroot.xhprof'));
include_once(XHPROF_ROOT . '/xhprof_lib/utils/xhprof_lib.php');
include_once(XHPROF_ROOT . '/xhprof_lib/utils/xhprof_runs.php');
$xhprofRuns = new XHProfRuns_Default();
$runId = $xhprofRuns->save_run($xhprofData, ' ');
echo "http://xxx.xxx.xxx/xhprof/xhprof_html/index.php?run={$runId}&source= ";
然後就可以根據上面的地址查看
這樣就可以查看到每個函數調用所消耗的CPU、記憶體、執行時間等
查看單一報告:
http://xxx.xxx.xxx/xhprof/xhprof_html/index.php?run={$runId}&source=
查看對比報告:
http://xxx.xxx.xxx/xhprof/xhprof_html/index.php?run1=xxxx&run2=xxx&source=
查看匯總報告:
http://xxx.xxx.xxx/xhprof/xhprof_html/index.php?run=1,2,3&source=
加權匯總 :進一步假設,上述3個運特分別對應三種程式,p1.php,p2.php和p3.php ,通常以20%,30%,50%機率混合:要查看匯總報告所對應的加權平均數這些運行使用:
http://xxx.xxx.xxx/xhprof/xhprof_html/index.php?run=1,2,3 &wts=20,30,50&source=
二、 壓力測試
命令:
ab ?c 256 ?n 100000 http://iploc.kuaibo.com/ip/weather
-c 並發數
-n 總請求次數
-t 總請求時間,超過5萬次結束
伺服器壓力測試參考資料:
1、 CPU (nginx、PHP、MYSQL) top
2、 記憶體(nginx、PHP、MYSQL) free
3、 隊列
4、 I/O
5、 網路流量
兩種測試結合進行, 壓力測試由低到高, 並且觀察伺服器的資源佔用情況。 在壓力測試的同時, 使用效能測試工具xhprof結合起來測試, 看看瓶頸是在哪裡, 什麼地方比較佔用資源等。