Apache JMeter5.2基础入门实践详解
JMeter官网:https://jmeter.apache.org/。关于JMeter是什么、能干什么,JMeter官网首页说明得很清楚,可以自己查看。在这个地址 https://archive.apache.org/dist/jmeter/binaries/ ,可以下载JMeter的所有版本,目前最新版本是apache-jmeter-5.2.1.zip,这个在首页即可下载:JMete...
JMeter官网:https://jmeter.apache.org/。
关于JMeter是什么、能干什么,JMeter官网首页说明得很清楚,可以自己查看。
在这个地址 https://archive.apache.org/dist/jmeter/binaries/ ,可以下载JMeter的所有版本,目前最新版本是apache-jmeter-5.2.1.zip,这个在首页即可下载:
JMeter是运行在JVM上的,所以需要先配置好Java环境。JMeter支持远程运行,是以线程方式运行。JMeter通过线程组来驱动多个线程运行测试脚本对被测试服务器发起负载,每一个负载机上都可以运行多个线程组。JMeter运行场景不仅可以再GUI方式中完成,还可以使用命令行,而且命令行运行的方式对于负载机的资源消耗会更小。
【1】JMeter基本介绍
将下载的zip解压并进入bin目录启动jmeter.bat即可,默认界面如下所示(本文使用版本5.2):
① 更换主题
默认主题如下:
在Options-Look and Feel步骤下可以修改默认主题,这里修改为System(修改完会提示重启):
③ 更换语言
对英语功底差的同学来说,更偏向于汉化。JMeter不需要汉化,它自身可以选择语言。Options-Choose Language-chinese(Simplified):
④ 工作区介绍
如下图所示,这里分为三部分:
区域①是一个目录树,存放测试设计过程中使用到的元件;执行过程中默认从根节点开始顺序遍历树上的元件。什么是元件?比如我们要向服务器发送一个http post请求,这个请求是由一个http请求取样器来完成的,这个叫“HTTP请求”的取样器就是元件。在区域①中添加进来的都是元件。
区域②是测试计划编辑区域,在【用户定义的变量】区域我们可以定义整个测试计划共用的全局变量,这些变量对多个线程组有效。我们还可以对线程组的运行进行设置:比如“独立运行每个线程组”,“Run teardown Thread Groups after shutdown of main threads”等。另外我们还可以在此添加测试计划依赖的jar包,比如JDBC方式连接数据库的驱动。
区域③是菜单栏,图标是菜单快捷方式,快捷菜单功能从左到右依次是:
- 新建测试计划
- 选择测试计划模板创建一个新的测试计划
- 选择已经存在的测试计划并打开;
- 保存测试计划
- 剪切选定的元件及子元件
- 复制选定的元件及子元件
- 粘贴复制的元件及子元件
- 展开目录树
- 收起目录树
- 禁用或启用元件,禁用元件的子元件也会被禁用
- 本机开始运行当前测试计划,按线程组的设置来启动
- 立即开始在本机运行当前测试计划
- 停止运行状态的测试计划,当前线程执行完成后停止
- 停止运行测试计划,立即终止,类似于杀进程
- 清除运行过程中元件显示的响应数据,比如查看结果树中的内容,聚合报告中的内容,但不能清除日志控制台中的内容
- 清除所有元件的响应数据,包括日志
- 查找
- 重置查找
- 函数助手对话框,这些函数在做参数化时会用到
- 帮助文档
⑤ Summary Report(汇总报告)
首先添加一个线程组:
添加监听器元件:
界面如下所示:
字段说明:
- Label:取样器别名
- #Samples:取样器运行次数
- Average:请求(事务)的评价响应时间
- Min:请求的最小响应时间
- Max:请求的最大响应时间
- Std.Dev:响应时间的标准偏差
- Error%:事务错误率
- Throughout:吞吐率,即常说的TPS
- Received KB/sec:每秒接收数据包流量,单位KB
- Sent KB/sec:每秒发送数据包流量,单位KB
- Avg.Bytes:平均数据流量,但是Byte
⑥ Aggregate Report(聚合报告)
在线程组添加监听器元件:
字段说明:
- Label:请求别名
- #Samples:取样器运行次数
- Average:请求(事务)的评价响应时间,单位毫秒。这个平均值是所有请求的响应时间取平均值
- Median:响应时间中间值
- 90%Line:90%事务平均响应时间
- 95%Line:95%事务平均响应时间
- 99%Line:99%事务平均响应时间
- Min:请求的最小响应时间
- Max:请求的最大响应时间
- Error%:事务错误率
- Throughout:吞吐率,即常说的TPS
- Received KB/sec:每秒接收数据包流量,单位KB
- Sent KB/sec:每秒发送数据包流量,单位KB
【2】Windows下JMeter GUI运行测试
也就是Windows下使用JMeter图形界面进行测试。
① 在测试计划上右键添加线程组
② 在线程组上右键添加请求和监听器元件
③ 编辑线程组
线程组界面如下:
解释如下:
- 线程数:加载多少线程
- Ramp-Up时间:加载线程时间。如上图即为一秒内加载1000个线程
- 循环次数:如上图循环十次则表示1000个线程每个执行10次总样本数为10000
④ 编辑HTTP请求
格式如下图所示:
这里是基本HTTP请求,至于高级格式后续更新。
编辑完线程组和HTTP请求后,就可以执行测试计划了。
⑤ 查看结果树
如下图所示,每个请求都详细记录了请求参数、请求头、响应结果、响应头以及取样器基本信息。
⑥ Aggregate Graph(聚合图)和Aggregate Report(聚合报告)
Aggregate Graph如下:
Aggregate Report只是没有图表,其他与Aggregate Graph一样。
⑦ 汇总报告(Summary Report)
汇总报告如下:
【3】Linux下JMeter 非GUI运行测试-命令执行
非GUI运行测试会减少资源消耗,JMeter同样支持命令方式执行测试计划,命令实例如下:
java -jar %JMETER_HOME%/bin/ApacheJMeter.jar -n -t %JMETER_HOME%/script/Test Plan.jmx -r -l result.jtl
or
%JMETER_HOME%/bin/jmeter -n -t %JMETER_HOME%/script/Test Plan.jmx -l %JMETER_HOME%/result/results.jtl
这两种方式都可以运行测试计划,JMeter运行测试计划实际上是通过运行ApacheJMeter.jar来完成的。
① 在Windows下cmd打开命令窗口执行jmeter -help
结果
可以看到JMeter提供了几种常用命令执行测试计划的方式:
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\softinstall\apache-jmeter-5.2-me\bin>jmeter -help
_ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____
/ \ | _ \ / \ / ___| | | | ____| | | \/ | ____|_ _| ____| _ \
/ _ \ | |_) / _ \| | | |_| | _| _ | | |\/| | _| | | | _| | |_) |
/ ___ \| __/ ___ \ |___| _ | |___ | |_| | | | | |___ | | | |___| _ <
/_/ \_\_| /_/ \_\____|_| |_|_____| \___/|_| |_|_____| |_| |_____|_| \_\ 5.2
Copyright (c) 1999-2019 The Apache Software Foundation
To list all command line options, open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -?
--------------------------------------------------
To run Apache JMeter in GUI mode, open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) [-p property-file]
--------------------------------------------------
To run Apache JMeter in NON_GUI mode:
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file]
--------------------------------------------------
To run Apache JMeter in NON_GUI mode and generate a report at end :
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file] -e -o [Path to output folder]
--------------------------------------------------
To generate a Report from existing CSV file:
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -g [csv results file] -o [path to output folder (empty or not existing)]
--------------------------------------------------
To tell Apache JMeter to use a proxy server:
Open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -H [your.proxy.server] -P [your proxy server port]
---------------------------------------------------
To run Apache JMeter in server mode:
Open a command prompt and type:
jmeter-server.bat(Windows)/jmeter-server(Linux)
---------------------------------------------------
E:\softinstall\apache-jmeter-5.2-me\bin>
② window下查看所有命令
E:\softinstall\apache-jmeter-5.2-me\bin>jmeter.bat -?
_ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____
/ \ | _ \ / \ / ___| | | | ____| | | \/ | ____|_ _| ____| _ \
/ _ \ | |_) / _ \| | | |_| | _| _ | | |\/| | _| | | | _| | |_) |
/ ___ \| __/ ___ \ |___| _ | |___ | |_| | | | | |___ | | | |___| _ <
/_/ \_\_| /_/ \_\____|_| |_|_____| \___/|_| |_|_____| |_| |_____|_| \_\ 5.2
Copyright (c) 1999-2019 The Apache Software Foundation
--?
print command line options and exit
-h, --help
print usage information and exit
-v, --version
print the version information and exit
-p, --propfile <argument>
the jmeter property file to use
-q, --addprop <argument>
additional JMeter property file(s)
-t, --testfile <argument>
the jmeter test(.jmx) file to run. "-t LAST" will load last
used file
-l, --logfile <argument>
the file to log samples to
-i, --jmeterlogconf <argument>
jmeter logging configuration file (log4j2.xml)
-j, --jmeterlogfile <argument>
jmeter run log file (jmeter.log)
-n, --nongui
run JMeter in nongui mode
-s, --server
run the JMeter server
-E, --proxyScheme <argument>
Set a proxy scheme to use for the proxy server
-H, --proxyHost <argument>
Set a proxy server for JMeter to use
-P, --proxyPort <argument>
Set proxy server port for JMeter to use
-N, --nonProxyHosts <argument>
Set nonproxy host list (e.g. *.apache.org|localhost)
-u, --username <argument>
Set username for proxy server that JMeter is to use
-a, --password <argument>
Set password for proxy server that JMeter is to use
-J, --jmeterproperty <argument>=<value>
Define additional JMeter properties
-G, --globalproperty <argument>=<value>
Define Global properties (sent to servers)
e.g. -Gport=123
or -Gglobal.properties
-D, --systemproperty <argument>=<value>
Define additional system properties
-S, --systemPropertyFile <argument>
additional system property file(s)
-f, --forceDeleteResultFile
force delete existing results files and web report folder if
present before starting the test
-L, --loglevel <argument>=<value>
[category=]level e.g. jorphan=INFO, jmeter.util=DEBUG or com
.example.foo=WARN
-r, --runremote
Start remote servers (as defined in remote_hosts)
-R, --remotestart <argument>
Start these remote servers (overrides remote_hosts)
-d, --homedir <argument>
the jmeter home directory to use
-X, --remoteexit
Exit the remote servers at end of test (non-GUI)
-g, --reportonly <argument>
generate report dashboard only, from a test results file
-e, --reportatendofloadtests
generate report dashboard after load test
-o, --reportoutputfolder <argument>
output folder for report dashboard
命令参数说明:
- -n:非GUI方式运行
- -t:指定运行的测试脚本地址与名称
- -p:指定读取JMeter属性文件,比如jmeter.properties文件中设置的
- -l:记录测试结果到文件,指定文件地址与名称
- -s:以服务器方式运行(就是我们说的远程方式,启动Agent)
- -H:设置代理,一般填写代理IP
- -P:设置代理端口
- -u:代理账号
- -a:代理口令
- -J:定义JMeter属性,等同于在jmeter.properties中进行设置
- -G:定义JMeter全局属性,等同于在Global.properties中进行设置,线程间可以共享
- -D:定义系统属性,等同于在system.properties中进行设置
- -S:加载系统属性文件,可以通过此参数指定加载一个系统属性文件,此文件可以用户自己定义
- -L:定义JMeter日志基本,比如DEBUG、INFO、ERROR等
- -r:开启远程负载机(非GUI方式),远程机器列表在jmeter.properties中指定
- -R:开启远程负载机,-R可以指定负载机IP,此选项会覆盖jmeter.properties中remote_hosts的设置
- -d:指定JMeter Home目录
- -X:停止远程执行
③ Linux下执行测试计划
这里需要一些前提步骤:
- 将测试项目(测试计划对应的测试目标项目)部署在linux上;
- 保存刚才window下测试计划脚本并放到linux上;
- 将jmeter部署在linux上;
- linux上必须安装好java环境以及项目环境
将项目在linux上运行起来后,如下图所示,将jmeter和测试计划脚本传到linux上:
将其解压到当前文件路径下:
tar -xvf apache-jmeter-5.2.tgz
执行命令进行测试并同时监控服务器负载状况:
./apache-jmeter-5.2/bin/jmeter -n -t ./TestPlan.jmx -l ./results.jtl
将生成的结果文件下载下来用jmeter打开:
在执行测试计划时用top命令监控服务器负载状况如下所示:
修改测试计划进行多次测试,从而逼近在当前服务器环境下该系统最大负荷。
更多推荐
所有评论(0)