ApacheBench(ab)压力测试工具,安装,用法,参数说明

ApacheBench是Apache服务器自带的压力测试工具,用于模拟多用户并发访问URL,评估服务器性能。本文介绍了ApacheBench的安装过程、基本用法及其参数说明,包括-n请求次数,-c并发数,-t测试时间限制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

二、Apache Bench安装
首先需要安装Apache服务器,下载地址:https://www.apachelounge.com/download/,
建议下载最新版本的,因为旧版本的ab不支持-r参数。

三、用法
在Windows系统下,打开cmd命令行窗口,定位到apache安装目录的bin目录下
2 cd C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin
3 键入命令:
4 ab -n 800 -c 800 http://192.168.0.10/
5 (-n发出800个请求,-c模拟800并发,相当800人同时访问,后面是测试url)
6 ab -t 60 -c 100 http://192.168.0.10/
7 在60秒内发请求,一次100个请求。

备注:
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
12POST数据所使用的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而不是服务器的性能。

1).只带一个参数:
ab -n 1000 -c 100 http://123.56.153.48/cname/Buildings?Token=sfddgsf--g
2) 有多个参数:
ab -t 60 -c 100 -T “text/plain” -p p.txt http://192.168.0.10/hello.html
p.txt 是和ab.exe在一个目录 ,若此种方式不可行,则将p.txt放到其他文件夹,但-p p.txt其p.txt须改为完整的文件路径,如: -p D:/param/p.txt
p.txt 中可以写参数,如 p=wdp&fq=78

更为详细操作

C:\xampp\apache\bin>ab-n 100 -c 10 http://www.abc.com/index.html
This is ApacheBench, Version 2.0.40-dev<$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus TechnologyLtd, http://www.zeustech.NET/
Copyright 2006 The Apache SoftwareFoundation, http://www.apache.org/

Benchmarking www.abc.com (bepatient)…done

Server Software: Apache
Server Hostname: www.abc.com
Server Port: 80

Document Path: /index.html
Document Length: 17784 bytes #请求文档大小

Concurrency Level: 10 #并发数
Time taken for tests: 11.898681 seconds #全部请求完成耗时
Complete requests: 100 #全部成功请求数
Failed requests: 0 失败请求数
Write errors: 0 错误请求数
Total transferred: 1805900 bytes #表示所用请求的响应数据总和,包括每个http响应数据的头信息+正文数据的长度(注意:这里不包括http 请求数据的长度)仅仅表示web服务器流向用户pc的应用层数据总长度
HTML transferred: 1778400 bytes == #表示所用请求的响应数据中正文数据的总和,也就是Total transferred-HTML transferred=响应头信息的总长度==
Requests per second: 8.40 [#/sec] (mean) #吞吐率=Complete requests/Time taken for tests
Time per request: 1189.868 [ms] (mean) #用户平均等待时间
Time per request: 118.987 [ms] (mean, across all concurrentrequests #服务器平均等待时间
Transfer rate: 148.17 [Kbytes/sec] received #表示单位时间内从服务器获取的数据
长 度,传输速率=Total transferred/Time taken for tests

Connection Times (ms) #连接时间
min mean[+/-sd] median(中位值) max
Connect: (#连接) 59 73 11.9 72 132
Processing: (#处理) 86 998538.7 938 2288
Waiting: (#等待) 63 135 106.8 94 663
Total: 155 1072540.6 1004 2362

在一定比例的请求服务时间

50% 1004
66% 1260
75% 1452
80% 1492
90% 1923
95% 2078
98% 2352
99% 2362
100% 2362 (longest request)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值