14

Mac下的Web性能压力测试工具:ab(ApacheBench)

 4 years ago
source link: https://xushanxiang.com/2019/10/mac-web-ab.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Mac下的Web性能压力测试工具:ab(ApacheBench)

作者: xusx 分类: HTTP,Web 发布时间: 2019-10-09 11:10

Web开发,少不了的就是压力测试,它是评估一个产品是否合格上线的基本标准。

ab是一种用于测试Apache超文本传输协议(HTTP)服务器的工具。apache自带ab工具,可以测试Apache、IIS、Tomcat、Nginx等Web服务器,因为apache是跨平台的,所以在Mac、Linux、Windows等系统下,只要安装了apache就能使用ab工具。

Ubuntu系统安装测试工具:sudo apt-get install apache2-utils

CentOS系统安装测试工具:yum -y install httpd-tools

Windows系统Apache下载地址:https://www.apachehaus.com/cgi-bin/download.plx

mac自带的apache中的ab是有最大并发限制的,您可以重新下载一个apache并且编译(可以参考这篇文章),安装之前记得卸载原来的哦。

ab是非图形化的并发测试工具,没有Jmeter、Loadrunner那样有各种场景设计、各种图形报告和监控,只需一个命令即可,有输出描述,可以简单的进行一些压力测试。当然,您也可以选择webbench、siege、http_load、Load impact、阿里云PTS等工具进行很方便的测试。

Mac下自带apache,查看版本:

apachectl -v
Server version: Apache/2.4.34 (Unix)
Server built: Feb 22 2019 20:20:11

查看ab版本:

ab -V

ab压力测试工具的用法,查看:

ab --help
或者
man ab
# 按Ctrl+z组合键,或者按q键退出
ab -c 并发数 -n 请求数 URL

ab工具常用参数:
-n :总共的请求执行数,缺省是1;
-c: 并发数,缺省是1;
-t:测试所进行的总时间,秒为单位,缺省50000s
-p:POST时的数据文件
-w: 以HTML表的格式输出结果

ab -n 100 -c 10 -w http://www.baidu.com/ >> baidu.html
# 结果界面和下面例子是一样的,只是以html文件形式保存而已。

下面,我们以请求百度为例:

ab -n 100 -c 10 https://www.baidu.com/

下面是运行结果:

 // apache版本信息
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 www.baidu.com (be patient)…..done
// 请求返回header类型
Server Software: BWS/1.1
// 请求ip或者域名
Server Hostname: www.baidu.com
// 请求端口,当前请求为https所以端口为443,请求https端口80
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
TLS Server Name: www.baidu.com
Document Path: /
// 第一个成功返回的文档的字节大小
Document Length: 227 bytes
// 并发数
Concurrency Level: 10
// 从建立连接到最后接受完成总时间
Time taken for tests: 1.407 seconds
// 完成请求数
Complete requests: 100
// 失败请求数
Failed requests: 0
// 从服务器接收的字节总数
Total transferred: 89300 bytes
// HTML接收字节数,减去了Total transferred中HTTP响应数据中的头信息的长度
HTML transferred: 22700 bytes
// 吞吐率:每秒请求数(总请求数/总时间,相当于LR中的每秒事务数TPS)
Requests per second: 71.07【#/sec】(mean)
// 用户平均请求等待时间
Time per request: 140.709【ms】(mean)
// 服务器处理每个请求平均响应时间,mean表示为平均值
Time per request: 14.071 [ms] (mean, across all concurrent requests)
// 这些请求在单位时间内从服务器获取的数据长度
// 计算公式:Total trnasferred/ Time taken for tests
Transfer rate: 61.98 [Kbytes/sec] received
// 连接消耗时间分解
Connection Times (ms)
min mean[+/-sd] median max
最小值 平均值 标准差 中间值 最大值
Connect: 60 92 16.6 93 167
Processing: 18 32 11.2 30 97
Waiting: 18 30 9.9 30 97
Total: 77 124 21.4 122 208
// 按完成请求的百分比,得出完成请求中花费时间最长的那一个请求的时间,也就是这些请求完成时间的最大值(毫秒)
Percentage of the requests served within a certain time (ms)
// 50%请求完成时间的最大值是122毫秒
50% 122
66% 130
75% 139
80% 140
// 90%请求完成时间的最大值是148毫秒
90% 148
95% 164
98% 182
99% 208
// 100%请求完成时间的最大值是208毫秒(最长请求)
100% 208 (longest request)

ab性能指标

在进行性能测试过程中有几个指标比较重要。

1、吞吐率(Requests per second)

服务器并发处理能力的量化描述,单位是reqs/s,指的是在某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
记住:吞吐率是基于并发用户数的。这句话代表了两个含义:
a、吞吐率和并发用户数相关;
b、不同的并发用户数下,吞吐率一般是不同的。
计算公式:总请求数/处理完成这些请求数所花费的时间,即:
Request per second=Complete requests/Time taken for tests
必须要说明的是,这个数值表示当前机器的整体性能,值越大越好。

2、并发连接数(The number of concurrent connections)

并发连接数指的是某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。

3、并发用户数(Concurrency Level)

要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。在HTTP/1.1下,IE7支持两个并发连接,IE8支持6个并发连接,FireFox3支持4个并发连接,所以相应的,我们的并发用户数就得除以这个基数。

4、用户平均请求等待时间(Time per request)

计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即:Time per request=Time taken for tests/(Complete requests/Concurrency Level)

5、服务器平均请求等待时间(Time per request:across all concurrent requests)

计算公式:处理完成所有请求数所花费的时间/总请求数,即:
Time taken for/testsComplete requests
可以看到,它是吞吐率的倒数。同时,它也等于用户平均请求等待时间/并发用户数,即:
Time per request/Concurrency Level


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK