一、Apache Bench简介
ApacheBench 是 Apache 服务器自带的一个web压力测试工具,简称ab。ab又是一个命令行工具,对发起负载的本机要求很低,根据ab命令可以创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问,因此可以用来测试目标服务器的负载压力。总的来说ab工具小巧简单,上手学习较快,可以提供需要的基本性能指标,但是没有图形化结果,不能监控。
二、Apache Bench安装
首先需要安装Apache服务器,下载地址:【https://www.apachelounge.com/download/】
三、Apache Bench的使用
了解参数
参数说明:
格式:ab [options] [http://]hostname[:port]/path
-n requests Number of requests to perform //本次测试发起的总请求数
-c concurrency Number of multiple requests to make //一次产生的请求数(或并发数)
-t timelimit Seconds to max. wait for responses //测试所进行的最大秒数,默认没有时间限制。
-r Don't exit on socket receive errors. // 抛出异常继续执行测试任务 **
-p postfile File containing data to POST //包含了需要POST的数据的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt
-T content-type Content-type header for POSTing
//POST数据所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p)
-v verbosity How much troubleshooting info to print
//设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
-C attribute Add cookie, eg. -C “c1=1234,c2=2,c3=3” (repeatable)
//-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复,用逗号分割。
提示:可以借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如-C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。
-w Print out results in HTML tables //以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-H attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’ Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-h Display usage information (this message)
如下图是各个参数代表的意思
C:\Users\Administrator>ab -n 1000 -c 50 http://localhost:8080/test //-n 代表本次测试请求总数 -c代表本次并发数
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
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 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /test
Document Length: 4 bytes
Concurrency Level: 50 //并发量
Time taken for tests: 0.409 seconds //整个测试完成的时间
Complete requests: 1000 //完整的请求数
Failed requests: 0 //失败的请求书
Total transferred: 136000 bytes //应用层数据总长度,不包括Http请求数据的长度
HTML transferred: 4000 bytes //所有响应数据中正文数据的总和
Requests per second: 2444.31 [#/sec] (mean) //吞吐率
Time per request: 20.456 [ms] (mean) //用户平均等待时间
Time per request: 0.409 [ms] (mean, across all concurrent requests) //服务器平均等待时间
Transfer rate: 324.64 [Kbytes/sec] received //单位时间内从服务器获取的数据长度
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 2
Processing: 0 20 22.2 15 112
Waiting: 0 12 17.4 1 90
Total: 0 20 22.2 15 112
Percentage of the requests served within a certain time (ms)
50% 15
66% 24
75% 29
80% 36
90% 51
95% 70
98% 85
99% 92
100% 112 (longest request)
网友评论