整个系统架构大概如下:
Worker可以在不同的服务器上,所以叫分布式,也因此可以利用多台电脑的资源来模拟足够的请求。
详细步骤
我将一步步介绍如何使用JMeter分布式测试来做性能测试,大致架构如下:
Controller我放在Mac上跑,Workers我是通过两台Ubuntu服务器来跑,要注意它们的IP是在同一内网上的。目标系统为了省事,我直接请求 Bing, stackoverflow, QQ。
Step 1: 安装JDK
Apache JMeter是用Java写的,运行它需要Java运行环境。在Ubuntu安装如下:
sudo apt install openjdk-11-jdk
Stpe 2: 下载Apache JMeter
去官网的 download page 下载最新的JMeter包apache-jmeter-5.5.zip,然后解压:
unzip apache-jmeter-5.5.zip
所有节点都需要下载安装。
Controller的位置如下:
/Users/larry/Software/apache-jmeter-5.5
Workers的位置如下:
home/larry/software/apache-jmeter-5.5
你不必位置和我一样,我列出来是为了与后面的命令对应上。
Step 3: 准备测试的JMX文件
JMeter把所有的测试信息都存放在jmx
文件中,打开工具写测试案例如下:
然后定义整个测试怎么跑,这里定义的是使用8个线程,60秒的热身和120秒的持续时长:
文件我放在这:
$ ls /Users/larry/Software/apache-jmeter-5.5/projects/pkslow
performance-test.jmx
我的案例只是Controller需要这个jmx
文件,Worker是不需要的。它们可以通过RMI来交互。
Step 4: 启动Workers
要先启动Workers,以服务器模式来启动,启动后不会执行什么测试,它会等待与它连接的Controller和指令:
- Worker 1:
$ /home/larry/software/apache-jmeter-5.5/bin/jmeter-server -Dserver.rmi.ssl.disable=true -Djava.rmi.server.hostname=192.168.50.171
Created remote object: UnicastServerRef2 [liveRef: [endpoint:[192.168.50.171:41709](local),objID:[-2af2edf6:1882245cfde:-7fff, 1980465993472976097]]]
- Worker 2:
$ /home/larry/software/apache-jmeter-5.5/bin/jmeter-server -Dserver.rmi.ssl.disable=true -Djava.rmi.server.hostname=192.168.50.60
Created remote object: UnicastServerRef2 [liveRef: [endpoint:[192.168.50.60:45337](local),objID:[18489452:1882246a88b:-7fff, 4749047852254882466]]]
“注意: 可能会遇到java.rmi.RemoteException,如果你没指定参数:
java.rmi.server.hostname
.
$ ./software/apache-jmeter-5.5/bin/jmeter-server -Dserver.rmi.ssl.disable=true
May 15, 2023 5:43:16 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
Created remote object: UnicastServerRef2 [liveRef: [endpoint:[127.0.1.1:33021](local),objID:[-7155c199:1881ecaf233:-7fff, -6972385484103483700]]]
Server failed to start: java.rmi.RemoteException: Cannot start. ubuntu is a loopback address.
An error occurred: Cannot start. ubuntu is a loopback address.
Step 5: 启动Controller
所有Workers就位后,就等主帅一声令下。启动时要指定jmx文件,报告生成的位置,还有Worker的IP,如下:
$ cd /Users/larry/Software/apache-jmeter-5.5/
$ ./bin/jmeter -n -t projects/pkslow/performance-test.jmx -l projects/pkslow/performance-test.csv -e -o projects/pkslow/Reports -Dserver.rmi.ssl.disable=true -R 192.168.50.171,192.168.50.60
Creating summariser <summary>
Created the tree successfully using projects/pkslow/performance-test.jmx
Configuring remote engine: 192.168.50.171
Configuring remote engine: 192.168.50.60
Starting distributed test with remote engines: [192.168.50.171, 192.168.50.60] @ May 16, 2023 10:37:31 PM CST (1684247851079)
Remote engines have been started:[192.168.50.171, 192.168.50.60]
Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
summary + 1 in 00:00:02 = 0.7/s Avg: 1260 Min: 1260 Max: 1260 Err: 0 (0.00%) Active: 2 Started: 2 Finished: 0
summary + 111 in 00:02:31 = 0.7/s Avg: 4433 Min: 41 Max: 50474 Err: 4 (3.60%) Active: 15 Started: 16 Finished: 1
summary = 112 in 00:02:33 = 0.7/s Avg: 4405 Min: 41 Max: 50474 Err: 4 (3.57%)
summary + 64 in 00:00:19 = 3.3/s Avg: 25706 Min: 62 Max: 66745 Err: 31 (48.44%) Active: 0 Started: 16 Finished: 16
summary = 176 in 00:02:52 = 1.0/s Avg: 12151 Min: 41 Max: 66745 Err: 35 (19.89%)
Tidying up remote @ May 16, 2023 10:40:24 PM CST (1684248024629)
... end of run
更多信息可以查看 jmeter.log:
$ tail -f jmeter.log
2023-05-16 22:40:29,956 INFO o.a.j.r.d.JsonExporter: Found data for consumer statisticsSummary in context
2023-05-16 22:40:29,957 INFO o.a.j.r.d.JsonExporter: Creating statistics for overall
2023-05-16 22:40:29,957 INFO o.a.j.r.d.JsonExporter: Creating statistics for other transactions
2023-05-16 22:40:29,957 INFO o.a.j.r.d.JsonExporter: Checking output folder
2023-05-16 22:40:29,958 INFO o.a.j.r.d.JsonExporter: Writing statistics JSON to /Users/larry/Software/apache-jmeter-5.5/projects/pkslow/Reports/statistics.json
2023-05-16 22:40:30,006 INFO o.a.j.r.d.ReportGenerator: Exporting data using exporter:'html' of className:'org.apache.jmeter.report.dashboard.HtmlTemplateExporter'
2023-05-16 22:40:30,007 INFO o.a.j.r.d.HtmlTemplateExporter: Will generate dashboard in folder: /Users/larry/Software/apache-jmeter-5.5/projects/pkslow/Reports
2023-05-16 22:40:30,142 INFO o.a.j.r.d.HtmlTemplateExporter: Report will be generated in: /Users/larry/Software/apache-jmeter-5.5/projects/pkslow/Reports, creating folder structure
2023-05-16 22:40:30,145 INFO o.a.j.r.d.TemplateVisitor: Copying folder from '/Users/larry/Software/apache-jmeter-5.5/bin/report-template' to '/Users/larry/Software/apache-jmeter-5.5/projects/pkslow/Reports', got message: /Users/larry/Software/apache-jmeter-5.5/projects/pkslow/Reports, found non empty folder with following content [/Users/larry/Software/apache-jmeter-5.5/projects/pkslow/Reports/statistics.json], will be ignored
2023-05-16 22:40:30,472 INFO o.a.j.JMeter: Dashboard generated
Worker收到指令也会开始工作并打印一些简单的日志:
$ /home/larry/software/apache-jmeter-5.5/bin/jmeter-server -Dserver.rmi.ssl.disable=true -Djava.rmi.server.hostname=192.168.50.171
Created remote object: UnicastServerRef2 [liveRef: [endpoint:[192.168.50.171:44621](local),objID:[4d13369d:18824fd059a:-7fff, 2287886479123818682]]]
Starting the test on host 192.168.50.171 @ 2023 May 16 22:37:31 HKT (1684247851980)
Warning: Nashorn engine is planned to be removed from a future JDK release
Finished the test on host 192.168.50.171 @ 2023 May 16 22:40:24 HKT (1684248024679)
Step 6: 结果和报告
整个测试跑完后,可以找到对应的结果:
$ ls -l /Users/larry/Software/apache-jmeter-5.5/projects/pkslow/
total 72
drwxr-xr-x 6 larry staff 192 May 16 22:40 Reports
-rw-r--r-- 1 larry staff 23472 May 16 22:40 performance-test.csv
-rw-r--r-- 1 larry staff 10593 May 16 09:50 performance-test.jmx
在Reports目录下直接打开 index.html:
其它提示
改变Worker的端口
Worker的默认端口是1099,但它是可以改变的,有些时候也必须改变,如设置的防火墙,或者需要在同一台Server启动多个Worker。具体命令如下:
$ /home/larry/software/apache-jmeter-5.5/bin/jmeter-server -Dserver.rmi.ssl.disable=true -Djava.rmi.server.hostname=192.168.50.60 -Dserver_port=1984
当然,Controller也要同步调整自己的命令:
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
erver_port=1984
当然,Controller也要同步调整自己的命令:
[外链图片转存中...(img-Bl3mi09n-1719263396241)]
[外链图片转存中...(img-mZjnw275-1719263396242)]
[外链图片转存中...(img-14DcILVT-1719263396243)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**