PHP - 《高性能php應用開發》學習筆記

1、基準測試

php網站優化最佳實踐:優化前端(壓縮js/css/images--->程序優化(編碼最佳實踐、opcode緩存、變量/數據緩存)--->數據庫、服務器調優-->操做系統調優php

 

 

1、基準測試實用工具

定義請求/響應生命週期css

典型的http請求包含正在嘗試訪問的主機信息、瀏覽器信息以及對web服務器有用的其餘信息。前端

 

 

1Apache Benchmark

實例:web

D:\Webdev\bin\apache\apache2.2.22\bin>ab -n 10000 -c 50 http://localhost/phpinf數據庫

.phpapache

This is ApacheBench, Version 2.3 <$Revision: 655654 $>瀏覽器

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/緩存

Licensed to The Apache Software Foundation, http://www.apache.org/服務器

 

Benchmarking localhost (be patient)併發

Completed 1000 requests

Completed 2000 requests

Completed 3000 requests

Completed 4000 requests

Completed 5000 requests

Completed 6000 requests

Completed 7000 requests

Completed 8000 requests

Completed 9000 requests

Completed 10000 requests

Finished 10000 requests

 

 

Server Software:        Apache/2.2.8

Server Hostname:        localhost

Server Port:            80

 

Document Path:          /phpinfo.php

Document Length:        52477 bytes      

Concurrency Level:      50

Time taken for tests:   22.932 seconds

Complete requests:      10000

Failed requests:        2158

   (Connect: 0, Receive: 0, Length: 2158, Exceptions: 0)

Write errors:           0

Total transferred:      526424140 bytes    #傳輸的總數據大小(以字節爲單位);

HTML transferred:       524774140 bytes

Requests per second:    436.07 [#/sec] (mean)   #web服務器在模擬流量下每秒能夠支持的請求總數;

Time per request:       114.660 [ms] (mean)   #完成一個請求所花費的最長時間(以毫秒爲單位)

Time per request:       2.293 [ms] (mean, across all concurrent requests) #完成一個請求所花費的最長時間(以毫秒爲單位)

Transfer rate:          22417.81 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    0   1.9      0      16

Processing:    16  114  32.4    109     468

Waiting:        0  109  26.6    109     281

Total:         16  114  32.4    109     468

 

Percentage of the requests served within a certain time (ms)

  50%    109

  66%    109

  75%    125

  80%    125

  90%    140

  95%    172

  98%    203

  99%    218

 100%    468 (longest request)

 

安裝

 

 

②運行Apache Benchmark

 

 

③弄清響應的含義

 

 

關鍵字段:HTML transferred(整個模擬傳輸的內容正文的總大小)Request per second(每秒支持的請求總數)、Time per request(知足一個請求須要花費的總時間)

目標:減小HTML transferred、提升Request per second而且下降Time per request

鏈接指標細目分類:

 

 

ab選項標記

 

 

 

 

 

 

 

 

 

⑤併發測試

 -c的值必須小於等於n的值

 

 

⑥時間測試

 

模擬10個用戶在20秒的時間內同時訪問網站

 

 

 

 

 

 

 

7 ab陷阱

ab - n http://localhost/而不是ab -n http://localhost,加上/

設置用戶代理:

 

 

⑧、修改apache併發數

apache處理併發是經過MPM模塊完成的,MPM有三種處理模式(performworkerwinnt)首先查看MPM的處理模式

 

而後查看httpd.conf,加載MPM模塊

 

 

修改httpd-mpm.conf

 

 

 

2Siege

①安裝Siege

 

 

 

 

 

②運行Siege

 

 

 

 

測試指標

幾個關鍵的指標:

Data transerred:響應數據的總大小(不包含頭數據)

Transaction rate:每秒要響應的事務總數

Longest transaction:知足一個請求所需的最長時間

Shortest transaction:知足一個請求所需的最短期   9.85 trans/sec

③測試多個url

/usr/local/etc/urls.txt

 

測試

 

 

測試結果:

 

 

2、提升客戶端下載和呈現性能

工具:

1.分析響應(FirebugYSlowPage Speed

2.優化響應(YUI compressor,Closure CompilerSmush.it),利用這些工具壓縮JavaScriptCSS以及網頁所需的圖像。

 

 

3、PHP代碼優化

php相關性能說明的列表:

http://talks.php.net/index.php/PHP

(1) requirerequire_once

代碼測試:

 

require_once_test.php內容:

 

幾個類文件只是定義了一個空類

 

 

每秒處理 1077.93個請求,處理時間爲9.277ms

 

測試require:

require_test.php內容:

 

其餘文件仍是定義一個空類

測試結果:

 

 

能夠看出,每秒的請求數從1077個到1612個請求,響應時間從9秒降到6秒。

 

(2)提早計算循環長度

for1.php

 

for2.php:

 

 

連續運行這兩個文件,發現後者比前者快好多

 

 

  



相關文章
相關標籤/搜索