Apache服務ab壓力測試

將ab壓力測試之前,先講解一下相關概念:

吞吐率(Requests per second)

服務器併發處理能力的量化描述,單位是reqs/s,指的是某個併發用戶數下單位時間內處理的請求數。某個併發用戶數下單位時間內能處理的最大請求數,稱之爲最大吞吐率。

計算公式:

總請求數 / 處理完成這些請求數所花費的時間,即
Request per second = Complete requests / Time taken for tests

併發連接數(The number of concurrent connections)

某個時刻服務器所接受的請求數目,簡單的講,就是一個會話。

併發用戶數(The number of concurrent users,Concurrency Level)

要注意區分這個概念和併發連接數之間的區別,一個用戶可能同時會產生多個會話,也即連接數。

用戶平均請求等待時間(Time per request)

計算公式:

處理完成所有請求數所花費的時間/ (總請求數 / 併發用戶數),即
Time per request = Time taken for tests /( Complete requests / Concurrency Level)

服務器平均請求等待時間(Time per request: across all concurrent requests)

計算公式:

處理完成所有請求數所花費的時間 / 總請求數,即
Time taken for / testsComplete requests
可以看到,它是吞吐率的倒數。
同時,它也=用戶平均請求等待時間/併發用戶數,即
Time per request / Concurrency Level

ab壓力測試工具

  • Apache自帶壓力測試工具ab,簡單易用,且可以模擬各種條件對Web服務器發起測試請求;

  • ab工具可以直接在Web服務器本地發起測試請求,這對於需要了解服務器的處理性能至關重要,因爲它不包括數據的網絡傳輸時間以及用戶PC本地的計算時間,從而可以通過觀測各種時間指標判斷Web服務器的性能,以便進行參數的優化調整。

  • 在進行性能調整優化過程中,可用ab壓力測試工具進行優化效果的測試

使用ab壓力測試工具優化過程

  • 優化前先使用ab工具進行壓力測試;

  • 優化後,重啓服務,再使用ab進行壓力測試;

  • 對比兩次測試的結果,看優化效果是否明顯;

  • 爲了能更客觀的評價web服務的性能,一般優化前後都要進行多次測試,取測試結果的平均值進行對比。

ab工具的使用

命令格式:

ab [options] 網站網址

常用參數:

-n:總共的請求執行數
-c:併發數
-t:執行測試的總時間,單位是秒
-v:打印多少故障排除信息
-V:顯示版本號並退出

手工編譯安裝Apache服務:

(1)安裝DNS服務的軟件包bind。

[root@localhost ~]# yum install bind -y
......//省略安裝過程
[root@localhost ~]#

(2)對DNS服務的主配置文件進行修改。

[root@localhost ~]# vim /etc/named.conf

options {
        listen-on port 53 { any; };                   //127.0.0.1改爲any
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };               //localhost改爲any

(3)對DNS服務的區域配置文件進行修改。

[root@localhost ~]# vim /etc/named.rfc1912.zones

zone "abc.com" IN {                                 //添加一個域名信息
        type master;
        file "abc.com.zone";
        allow-update { none; };
};

(4)查看一下IP地址。

[root@localhost named]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.52.133  netmask 255.255.255.0  broadcast 192.168.52.255
        inet6 fe80::3e1d:31ba:f66a:6f80  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:27:1c:3f  txqueuelen 1000  (Ethernet)
        RX packets 14532  bytes 20210558 (19.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6054  bytes 399142 (389.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(5)保留權限複製一份DNS服務的區域數據配置文件,進行修改。

[root@localhost ~]# cd /var/named/          //切換目錄
[root@localhost named]# ls                        //查看
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost abc.com.zone    //複製
[root@localhost named]# vim abc.com.zone 

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.52.133             //添加IPv4的域名解析

(6)開啓named服務。

[root@localhost named]# systemctl start named      //開啓服務
[root@localhost named]# systemctl stop firewalld.service      //關閉防火牆
[root@localhost named]# setenforce 0         //關閉增強性安全功能
[root@localhost named]# 

(7)在宿主機將我們所需的工具包共享出去。

Apache服務ab壓力測試

(8)通過Samba服務將工具包掛載到Linux系統。

[root@localhost ~]# smbclient -L //192.168.100.50/   //查看共享
Enter SAMBA\root's password:      //匿名共享,沒有密碼,直接回車
OS=[Windows 10 Enterprise LTSC 2019 17763] Server=[Windows 10 Enterprise LTSC 2019 6.3]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       遠程 IPC
    share           Disk      
    tools           Disk      
    Users           Disk      
Connection to 192.168.100.50 failed (Error NT_STATUS_RESOURCE_NAME_NOT_FOUND)
NetBIOS over TCP disabled -- no workgroup available
[root@localhost ~]# mkdir /mnt/tools    //創建掛載目錄
[root@localhost ~]# mount.cifs //192.168.100.50/tools /mnt/tools/     //掛載
Password for root@//192.168.100.50/tools:  
[root@localhost ~]# cd /mnt/tools/    //進入掛載目錄
[root@localhost tools]# ls     //查看
awstats-7.6.tar.gz                extundelete-0.2.4.tar.bz2  forbid.png                 jdk-8u191-windows-x64.zip  LAMP-C7
cronolog-1.6.2-14.el7.x86_64.rpm  fiddler.exe                intellijideahahau2018.rar  john-1.8.0.tar.gz          picture.jpg
[root@localhost tools]# 

(9)將源碼編譯安裝Apache服務的壓縮包解壓到“/opt/”目錄。

[root@localhost tools]# cd LAMP-C7/       //切換目錄
[root@localhost LAMP-C7]# ls
apr-1.6.2.tar.gz       Discuz_X2.5_SC_UTF8.zip  LAMP-php5.6.txt      php-5.6.11.tar.bz2
apr-util-1.6.0.tar.gz  httpd-2.4.29.tar.bz2     mysql-5.6.26.tar.gz
[root@localhost LAMP-C7]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/     //解壓
......//省略解壓詳情
[root@localhost LAMP-C7]# tar zxvf apr-1.6.2.tar.gz -C /opt/           //解壓
......//省略解壓詳情
[root@localhost LAMP-C7]# tar zxvf apr-util-1.6.0.tar.gz -C /opt/     //解壓
......//省略解壓詳情

(10)進入“/opt/”目錄,將兩個apr包移動到“httpd-2.4.29/srclib/”目錄,並重命名。

[root@localhost LAMP-C7]# cd /opt/    
[root@localhost opt]# ls
apr-1.6.2  apr-util-1.6.0  httpd-2.4.29  rh
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util

(11)進入“httpd-2.4.29/”目錄,然後安裝編譯所需環境包。

[root@localhost opt]# ls
httpd-2.4.29  rh
[root@localhost opt]# cd httpd-2.4.29/
[root@localhost httpd-2.4.29]# ls
ABOUT_APACHE     ap.d          CHANGES         docs         httpd.spec      libhttpd.dep  Makefile.win   README            srclib
acinclude.m4     build         CMakeLists.txt  emacs-style  include         libhttpd.dsp  modules        README.cmake      support
Apache-apr2.dsw  BuildAll.dsp  config.layout   httpd.dep    INSTALL         libhttpd.mak  NOTICE         README.platforms  test
Apache.dsw       BuildBin.dsp  configure       httpd.dsp    InstallBin.dsp  LICENSE       NWGNUmakefile  ROADMAP           VERSIONING
apache_probes.d  buildconf     configure.in    httpd.mak    LAYOUT          Makefile.in   os             server
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# yum -y install \
> gcc \
> gcc-c++ \
> make \
> pcre \
> pcre-devel \
> expat-devel \
> zlib-devel \
> perl
......//省略安裝過程

(12)進行對Apache服務器的配置。

[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \    //安裝路徑
> --enable-so \     //啓用動態加載模塊支持
> --enable-rewrite \     //啓用網頁地址重寫功能
> --enable-charset-lite \    //啓用字符集支持
> --enable-cgi    //啓用CGI腳本程序支持

(13)編譯安裝Apache服務。

[root@localhost httpd-2.4.29]# make && make install
......//省略編譯安裝過程
[root@localhost httpd-2.4.29]#

(14)對Apache服務配置文件進行修改

[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf     //創建軟鏈接,方便使用
[root@localhost httpd-2.4.29]# 

Listen 192.168.50.133:80    //開啓IPv4監聽
#Listen 80      //註釋IPv6監聽
#
ServerName www.abc.com:80      //設置域名

ab壓力測試

[root@localhost named]# ab -n 3000 -c 1000 www.abc.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.abc.com (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests

Server Software:        Apache/2.4.29            //http響應數據的頭信息
Server Hostname:        www.abc.com         //請求的url中的主機名稱
Server Port:            80                                   //web服務器軟件的監聽端口

Document Path:          /index.html              //請求的url根的絕對路徑
Document Length:        45 bytes                //http響應數據的正文長度

Concurrency Level:      1000                     //併發的用戶數
Time taken for tests:   21.061 seconds    //所有這些請求被處理完成所花費的時間總和
Complete requests:      3000         //表示請求總數
Failed requests:        220             //失敗的請求總數
   (Connect: 0, Receive: 0, Length: 220, Exceptions: 0)
Total transferred:      810356 bytes       //請求的響應數據長度總數
HTML transferred:       126180 bytes
Requests per second:    142.44 [#/sec] (mean)      //服務器的吞吐率,每秒處理的請求總數
Time per request:       7020.414 [ms] (mean)    //用戶平均請求等待時間
Time per request:       7.020 [ms] (mean, across all concurrent requests)    //每個請求實際運行時間的平均值
Transfer rate:          37.57 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    9  14.5      4      62
Processing:     9 1389 5135.1     27   21027
Waiting:        0   27  34.4     22     234
Total:         13 1398 5132.8     33   21027

Percentage of the requests served within a certain time (ms)   //描述每個請求處理時間的分佈情況。
  50%     33
  66%     37
  75%     42
  80%     55
  90%     95
  95%  21020
  98%  21027
  99%  21027
 100%  21027 (longest request)
[root@localhost named]#
參數 描述
Server Software http響應數據的頭信息
Server Hostname 請求的url中的主機名稱
Server Port web服務器軟件的監聽端口
Document Path 請求的url根的絕對路徑
Document Length http響應數據的正文長度
Concurrency Level 併發的用戶數
Time taken for tests 所有這些請求被處理完成所花費的時間總和
Complete requests 表示總請求數
Failed requests 失敗的請求總數
Total transferred 請求的響應數據長度總和
Requests per second 服務器的吞吐率,每秒處理的請求數
Time per request 用戶平均請求等待時間
Time per request 每個請求實際運行時間的平均值
Percentage of the requests served within a certain time (ms) 描述每個請求處理時間的分佈情況
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章