国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

提高php程序性能和負載測試

2024-05-04 21:48:45
字體:
供稿:網(wǎng)友

一篇關(guān)于提高php程序性能和負載測試的實例代碼,有需要的朋友可以看看如何提高自己程序的性能.

計算執(zhí)行的時間,通過下面這個簡單的方法可以計算一段程序的執(zhí)行時間(微妙),代碼如下:

  1. $start_time = microtime(true); 
  2.  
  3. //一些需要計算時間的代碼 
  4. //... code here ... 
  5.  
  6. print('代碼的運行時間是:'.getExecTime($start_time)); 
  7.  
  8. function getExecTime($start_time
  9.  return microtime(true)-$start_time
  10. }PEAR的Benchmark模塊提供了更詳細的時間統(tǒng)計功能 
  11.  
  12. require_once 'Benchmark/Timer.php'
  13. $timer =& new Benchmark_Timer(true); 
  14. $timer->start(); 
  15. // 設(shè)置函數(shù) 
  16. $timer->setMarker('setup'); 
  17. // some more code executed here 
  18. $timer->setMarker('middle'); 
  19. // even yet still more code here 
  20. $timer->setmarker('done'); 
  21. // and a last bit of code here 
  22. $timer->stop(); 
  23. $timer->display();通過declare結(jié)構(gòu)和ticks指令可以實現(xiàn)自動記錄每一行PHP代碼執(zhí)行的時間 
  24.  
  25. // A function that records the time when it is called 
  26. function profile($dump = FALSE) 
  27.     static $profile
  28.  
  29.     // Return the times stored in profile, then erase it 
  30.     if ($dump) { 
  31.         $temp = $profile
  32.         unset($profile); 
  33.         return ($temp); 
  34.     } 
  35.  
  36.     $profile[] = microtime(); 
  37.  
  38. // Set up a tick handler 
  39. register_tick_function("profile"); 
  40. //開源代碼Vevb.com 
  41. // Initialize the function before the declare block 
  42. profile(); 
  43.  
  44. // Run a block of code, throw a tick every 2nd statement 
  45. declare(ticks=2) { 
  46.     for ($x = 1; $x < 50; ++$x) { 
  47.         echo similar_text(md5($x), md5($x*$x)), ";"
  48.     } 
  49.  
  50. // Display the data stored in the profiler 

print_r(profile (TRUE));注意:ticks 指令在 PHP 5.3.0 中是過時指令,將會從 PHP 6.0.0 移除.

代碼排錯

主要介紹的是Advanced PHP Debugger(APD),通過設(shè)置可以生成跟蹤文件,對文件進行分析可以得到腳本的詳細信息.

網(wǎng)站壓力測試

人們常混淆壓力測試和基準(zhǔn)測試,基準(zhǔn)測試是一種由單獨的開發(fā)者完成的臨時活動,常用Apache HTTP測試工具——ab,該工具可以測試一臺HTTP服務(wù)器每秒能相應(yīng)的請求數(shù),壓力測試是一種能中斷你WEB應(yīng)用程序的測試技術(shù),通過對斷點測試,能識別并修復(fù)應(yīng)用程序中的弱點,為何時購置新硬件提供依據(jù),常用的開源工具是Siege.

提速技巧

通過安裝PHP加速器可以有效的提供PHP的執(zhí)行速度,常見的三種加速器是Alternative PHP Cache(APC)、eAccelerator和ionCube PHP Accelerator(PHPA),另外需要注意的是加速器的兼容性通常會滯后于新發(fā)布的PHP版本.

另外提速技巧是在能不使用正則的時候盡量不要用,通常可替代的方案會比使用正則效率更高.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 定远县| 九龙城区| 巴青县| 南开区| 绥棱县| 鱼台县| 青岛市| 科技| 寻甸| 松江区| 彭泽县| 郴州市| 佛冈县| 巫山县| 东兰县| 富蕴县| 呼图壁县| 当涂县| 彝良县| 修文县| 东阿县| 介休市| 忻城县| 柯坪县| 江陵县| 双柏县| 右玉县| 蓬莱市| 色达县| 手机| 横山县| 伽师县| 谷城县| 栖霞市| 中阳县| 镇安县| 澎湖县| 商丘市| 丹凤县| 曲松县| 吐鲁番市|