zoukankan      html  css  js  c++  java
  • Apache Bench安装与使用

    转载:ApacheBench(ab)使用详解

    一、Apache Bench简介

    ApacheBench 是 Apache 服务器自带的一个web压力测试工具,简称ab。ab又是一个命令行工具,对发起负载的本机要求很低,根据ab命令可以创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问,因此可以用来测试目标服务器的负载压力。它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。总的来说ab工具小巧简单,上手学习较快,可以提供需要的基本性能指标,但是没有图形化结果,不能监控。

    ab命令对发出负载的计算机要求很低,既不会占用很高CPU,也不会占用很多内存,但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也须注意,否则一次上太多的负载,可能造成目标服务器因资源耗完,严重时甚至导致死机。

    二、Apache Bench安装

    首先需要安装Apache服务器,下载地址:https://www.apachelounge.com/download/

    建议下载最新版本的,因为旧版本的ab不支持-r参数。

    三、Apache Bench使用

    1.使用例子:

     步骤1:找到安装Apache Bench文件的bin目录下,输入ab.exe 可以看到提示信息如下:

     步骤2:

    输入 ab -n 100 -c 20 http://localhost:8080/jsrWeb/index.html(-n发出100个请求,-c模拟20并发,相当100人同时访问,后面是测试url)

    输入 ab -n 100 -c 20 http://localhost:8080/jsrAdminWeb/test?str=AA (传入一个参数)

    输入 ab -n 100 -c 20 -T "text/plain" -p test.txt http://localhost:8080/jsrAdminWeb/test (传入多个参数,test.txt文件与ab.exe在同目录下,test.txt文件里写str1=AA&str2=BB)

    输入 ab -n 100 -c 20 -T "text/plain" -p E:DOWNLOADhttpd-2.4.33-Win64-VC15Apache24 est.txt http://localhost:8080/jsrAdminWeb/test (传入多个参数,test.txt文件与ab.exe不在同目录下,写入test.txt文件的全路径)

    输入 ab -t 60 -c 20 http://localhost:8080/jsrWeb/index.html 在60秒内发请求,一次20个请求)

    结果参数解释(转载): 

    Server Software:        Microsoft-HTTPAPI/2.0 
    Server Hostname:        192.168.0.10 
    Server Port:            80

    Document Path:          / 
    Document Length:        315 bytes       HTTP响应数据的正文长度

    Concurrency Level:      800 
    Time taken for tests:   0.914 seconds    所有这些请求处理完成所花费的时间 
    Complete requests:      800             完成请求数 
    Failed requests:        0                失败请求数 
    Write errors:           0                
    Non-2xx responses:      800 
    Total transferred:      393600 bytes     网络总传输量 
    HTML transferred:       252000 bytes     HTML内容传输量 
    Requests per second:    875.22 [#/sec] (mean) 吞吐量-每秒请求数 
    Time per request:       914.052 [ms] (mean)  服务器收到请求,响应页面要花费的时间 
    Time per request:       1.143 [ms] (mean, across all concurrent requests) 并发的每个请求平均消耗时间 
    Transfer rate:          420.52 [Kbytes/sec] received 平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题


    网络上消耗的时间的分解: 
    Connection Times (ms) 
                  min  mean[+/-sd] median   max 
    Connect:        0    1   0.5      1       3 
    Processing:   245  534 125.2    570     682 
    Waiting:       11  386 189.1    409     669 
    Total:        246  535 125.0    571     684

    整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间 
    其中 50% 的用户响应时间小于 571 毫秒 
    80 % 的用户响应时间小于 652 毫秒 
    最大的响应时间小于 684 毫秒 
    Percentage of the requests served within a certain time (ms) 
      50%    571 
      66%    627 
      75%    646 
      80%    652 
      90%    666 
      95%    677 
      98%    681 
      99%    682 
    100%    684 (longest request)

    2.ApacheBench的参数说明

     1 格式:ab [options] [http://]hostname[:port]/path
     2 参数说明:
     3 -n requests Number of requests to perform
     4 //在测试会话中所执行的请求个数(本次测试总共要访问页面的次数)。默认时,仅执行一个请求。
     5 -c concurrency Number of multiple requests to make
     6 //一次产生的请求个数(并发数)。默认是一次一个。
     7 -t timelimit Seconds to max. wait for responses
     8 //测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
     9 -p postfile File containing data to POST
    10 //包含了需要POST的数据的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt 。 (配合-T)
    11 -T content-type Content-type header for POSTing
    12 //POST数据所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p)
    13 -v verbosity How much troubleshooting info to print
    14 //设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
    15 -w Print out results in HTML tables
    16 //以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
    17 -i Use HEAD instead of GET
    18 // 执行HEAD请求,而不是GET。
    19 -x attributes String to insert as table attributes
    20 -y attributes String to insert as tr attributes
    21 -z attributes String to insert as td or th attributes
    22 -C attribute Add cookie, eg. -C “c1=1234,c2=2,c3=3″ (repeatable)
    23 //-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复,用逗号分割。
    24 提示:可以借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如
    25 -C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。
    26 -H attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’ Inserted after all normal header lines. (repeatable)
    27 -A attribute Add Basic WWW Authentication, the attributes
    28 are a colon separated username and password.
    29 -P attribute Add Basic Proxy Authentication, the attributes
    30 are a colon separated username and password.
    31 //-P proxy-auth-username:password 对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。
    32 -X proxy:port Proxyserver and port number to use
    33 -V Print version number and exit
    34 -k Use HTTP KeepAlive feature
    35 -d Do not show percentiles served table.
    36 -S Do not show confidence estimators and warnings.
    37 -g filename Output collected data to gnuplot format file.
    38 -e filename Output CSV file with percentages served
    39 -h Display usage information (this message)
    40 //-attributes 设置属性的字符串. 缺陷程序中有各种静态声明的固定长度的缓冲区。另外,对命令行参数、服务器的响应头和其他外部输入的解析也很简单,这可能会有不良后果。它没有完整地实现 HTTP/1.x; 仅接受某些’预想’的响应格式。 strstr(3)的频繁使用可能会带来性能问题,即你可能是在测试ab而不是服务器的性能。
  • 相关阅读:
    第二阶段第二次spring会议
    第一次冲刺阶段的改进方案
    第二阶段第一次spring会议
    第七次spring会议
    第六次spring会议
    第五次spring会议
    第四次spring会议
    第三次spring会议
    第二次spring会议
    第一次spring会议
  • 原文地址:https://www.cnblogs.com/nananana/p/8748941.html
Copyright © 2011-2022 走看看