Apache Bench - 輸出結果對比



本章將對比使用和不使用標誌時的輸出結果。讓我們看看使用合適的標誌如何提高 Web 應用程式的效能。在此之前,我們需要了解,如果您的應用程式很簡單,您可能不會注意到差異。就像我們的簡單應用程式一樣,使用標誌和不使用標誌的結果差異不大。然後,我們將對https://www.apache.org/ URL 進行相同的測試,並檢視差異。

不使用標誌測試我們的應用程式

在本節中,我們將瞭解如何不使用標誌測試我們的應用程式。

$ ab -n 100 -c 10 http://127.0.0.1:8000/

輸出結果

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:        Rocket
Server Hostname:        127.0.0.1
Server Port:            8000

Document Path:          /
Document Length:        Variable

Concurrency Level:      10
Time taken for tests:   0.244 seconds
Complete requests:      100
Failed requests:        0
Non-2xx responses:      100
Keep-Alive requests:    0
Total transferred:      27700 bytes
HTML transferred:       6600 bytes
Requests per second:    2208.77 [#/sec] (mean)
Time per request:       4.527 [ms] (mean)
Time per request:       0.453 [ms] (mean, across all concurrent requests)
Transfer rate:          597.49 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    2   0.7      2       3
Processing:     0    2   0.7      2       4
Waiting:        0    2   1.0      2       3
Total:          4    4   0.3      4       5

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      5
  80%      5
  90%      5
  95%      5
  98%      5
  99%      5
 100%      5 (longest request)

使用標誌測試我們的應用程式

在本節中,我們將瞭解如何使用標誌測試我們的應用程式。

$ ab -l -r -n 100 -c 10 -k -H "Accept-Encoding: gzip, deflate"  http://127.0.0.1:8000/

輸出結果

...
Requests per second:    2277.07 [#/sec] (mean)
Time per request:       4.392 [ms] (mean)
Time per request:       0.439 [ms] (mean, across all concurrent requests)
Transfer rate:          615.97 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    2   0.7      2       3
Processing:     0    2   0.7      2       4
Waiting:        0    2   1.0      2       3
Total:          4    4   0.2      4       5

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      4
  80%      4
  90%      5
  95%      5
  98%      5
  99%      5
 100%      5 (longest request)

我們可以簡單地注意到輸出統計資料之間沒有太大區別。

不使用標誌測試 Apache 組織網站

現在讓我們看看如何不使用標誌測試 Apache 組織網站。

$ ab -n 100 -c 10 http://www.apache.org/

輸出結果

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.apache.org (be patient).....done

Server Software:        Apache/2.4.7
Server Hostname:        www.apache.org
Server Port:            80

Document Path:          /
Document Length:        58433 bytes

Concurrency Level:      10
Time taken for tests:   1.498 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      5877500 bytes
HTML transferred:       5843300 bytes
Requests per second:    66.74 [#/sec] (mean)
Time per request:       149.840 [ms] (mean)
Time per request:       14.984 [ms] (mean, across all concurrent requests)
Transfer rate:          3830.58 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       12  110 295.2     12    1012
Processing:    37   38   0.5     38      39
Waiting:       12   13   0.3     13      15
Total:         49  147 295.4     50    1051

Percentage of the requests served within a certain time (ms)
  50%     50
  66%     50
  75%     50
  80%     50
  90%    816
  95%   1050
  98%   1051
  99%   1051
 100%   1051 (longest request)

使用標誌測試 Apache 組織網站

現在讓我們使用標誌測試 Apache 組織網站。

$ ab -l -r -n 100 -c 10 -k -H "Accept-Encoding: gzip, deflate"  http://www.apache.org/

輸出結果

...
Document Length:        Variable

Concurrency Level:      10
Time taken for tests:   0.357 seconds
Complete requests:      100
Failed requests:        0
Keep-Alive requests:    100
Total transferred:      1358510 bytes
HTML transferred:       1317700 bytes
Requests per second:    280.28 [#/sec] (mean)
Time per request:       35.678 [ms] (mean)
Time per request:       3.568 [ms] (mean, across all concurrent requests)
Transfer rate:          3718.41 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   3.7      0      12
Processing:    14   17  21.3     15     227
Waiting:       14   17  21.3     14     227
Total:         14   18  21.5     15     227

Percentage of the requests served within a certain time (ms)
  50%     15
  66%     15
  75%     15
  80%     15
  90%     27
  95%     28
  98%     29
  99%    227
 100%    227 (longest request)

您可以簡單地注意到,使用標誌後每秒請求數增加了。在本例中,這尤其歸功於使用了-H "Accept-Encoding: gzip, deflate,因為此標誌告訴 Apache 伺服器以gzip格式提供請求。

Apache Bench 結果考量

在考慮 Apache Bench 結果時,需要考慮一些重要事項。這將幫助我們設計整體策略,以消除應用程式中的瓶頸並提高其效能。

我們需要關注每秒請求數。這讓我們瞭解 Web 伺服器設定的工作情況;數字越大,效能越好。然後是連線時間 (ms) 和已處理請求的百分比。您可能需要調整 Web 伺服器的設定,以將這些指標更改為所需的效能。

檢查 Apache 或使用的 Web 伺服器錯誤日誌或(常規)日誌中是否存在錯誤。隨著負載的增加,事情會開始卡住:記憶體問題會開始出現。如果未考慮併發性編寫,許多 Python 指令碼將開始崩潰。

您需要找出 Web 伺服器崩潰和/或超時前的臨界併發值是多少?通常情況下,這應該發生在相當高的併發級別。如果此值較低,則說明存在問題,您需要調整這些設定(降低/提高)。

結論

在本教程中,我們學習瞭如何使用 Apache Bench 來負載測試任何網站或 Web 應用程式。Apache Bench 是一個非常有價值的工具,可用於確定如何改進 Web 應用程式伺服器設定,以減少瓶頸並提高效能。現在您已經熟悉了 Apache Bench 的基本用法,您可以開始建立新的測試計劃,以在各種場景中衡量應用程式的效能。

廣告
© . All rights reserved.