一、环境准备
下载jdk包
地址:https://www.oracle.com/java/technologies/downloads/#java8
centos 下载 x64 Compressed Archive
版本
创建 /usr/local/java
目录并将jdk压缩包解压到该目录下
在 /etc/profile
文件中设置环境变量,它是是所有用户的共用的环境变量,在末尾添加如下配置:
export JAVA_HOME=/usr/local/java/jdk1.8.0_181
export JRE_HOME=/usr/local/java/jdk1.8.0_181/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$PATH
下载jmeter运行包
下载地址:https://jmeter.apache.org/download_jmeter.cgi
将下载的包解压到centos服务器的/app/cachecloud/jmeter
目录中,然后在/etc/profile目录中配置 JMETER有环境变量
export JMETER=/app/cachecloud/jmeter
export CLASSPATH=$JMETER/lib/ext/ApacheJMeter_core.jar:$JMETER/lib/jorphan.jar:$JMETER/lib/logkit-2.0.jar:$CLASSPATH
export PATH=$JMETER/bin/:$PATH
配置jmeter配置文件
[root@migrate_7_59 bin]# vi /app/cachecloud/jmeter/bin/jmeter.properties
server.rmi.ssl.disable
值改为false
remote_hosts=127.0.0.1
,该值设置为slave机器地址,多slave用英文逗号隔开。
如果我只用当前机器做为Jmeter测试服务器, 配置为127.0.0.1,如果我想使用多台Jmeter测试服务器进行测试,则在后面配置slave机器地址和port即可,如remote_hosts=127.0.0.1,10.4.7.58:1099
,port的值配置在jmeter.properties
的server_port
属性中。slave机器jmeter的server_port配置要与master机器jmeter的remote_hosts配置的地址和port相一致。
环境变量生效
修改保存后,运行source命令使改动生效。
source /etc/profile
二、创建jmeter测试计划
打包测试java代码
我要测试的是java的sdk,想在jmeter中能够选取测试的类,首先要将需要测试的java代码打成jar包,然后拷贝到jmeter的lib/ext目录下。如果代码依赖了外部的包,则需要将外部包一同打包到测试java代码的jar包中。pom.xml的plugin配置代码如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
这样打出的java jar包会将依赖的包一并打入jar包中。
启动jmeter GUI界面
jmeter的测试计划是必须得在界面上面创建的,所以首先在我的系统中启动jmeter的gui界面。
bin % ls -1
ApacheJMeter.jar
BeanShellAssertion.bshrc
BeanShellFunction.bshrc
BeanShellListeners.bshrc
BeanShellSampler.bshrc
create-rmi-keystore.bat
create-rmi-keystore.sh
examples
hc.parameters
heapdump.cmd
heapdump.sh
jaas.conf
jmeter
jmeter-n-r.cmd
jmeter-n.cmd
jmeter-server
jmeter-server.bat
jmeter-server.log
jmeter-t.cmd
jmeter.bat
jmeter.log
jmeter.properties
jmeter.sh
jmeterw.cmd
krb5.conf
log4j2.xml
mirror-server
mirror-server.cmd
mirror-server.sh
report-template
reportgenerator.properties
saveservice.properties
shutdown.cmd
shutdown.sh
stoptest.cmd
stoptest.sh
system.properties
templates
threaddump.cmd
threaddump.sh
upgrade.properties
user.properties
utility.groovy
## 在MAC中启动jmeter的GUI
bin % ./jmeter
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/domino/Downloads/apache-jmeter-5.5/lib/log4j-slf4j-impl-2.17.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/domino/Downloads/apache-jmeter-5.5/lib/ext/gcache-jmeter-demo-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
================================================================================
Don't use GUI mode for load testing !, only for Test creation and Test debugging.
For load testing, use CLI Mode (was NON GUI):
jmeter -n -t [jmx file] -l [results file] -e -o [Path to web report folder]
& increase Java Heap to meet your test requirements:
Modify current env variable HEAP="-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m" in the jmeter batch file
Check : https://jmeter.apache.org/usermanual/best-practices.html
================================================================================
启动JMeter GUI
新增线程组
配置线程组配置
Number of Threads
:线程数,即并发数,每个线程可以代表一个用户。Ramp-Up Period (in seconds)
:多长时间内初始化完这些线程。单位是秒。Loop Count
:循环次数。如果要永远循环,刚勾上Infinite(永远)这个单选框,取消勾选并填写循环次数。
新增java测试请求案例
在测试安全中配置需要执行测试的java类
保存配置到jmx文件中,有了这个jmx配置文件,就可以拿到linux服务上进行测试了。
三、运行jmeter压力测试
将刚刚创建的jmeter的jmx配置文件拷贝到linux服务的jmeter目录下,再将java代码jar包拷贝到linux服务器的 jmeter目录的lib/ext目录下。
启动jmeter测试服务
[root@migrate_7_59 bin]# sh /app/cachecloud/jmeter/bin/jmeter-server
运行压力测试
[root@migrate_7_59 bin]# sh /app/cachecloud/jmeter/bin/jmeter.sh -n -t /app/cachecloud/jmeter/gcache-test.jmx -r -e -o /app/cachecloud/jmeter/result/result4 -l /app/cachecloud/jmeter/result/result4/gcache-test.jtl
summary = 6384438 in 01:40:40 = 1057.0/s Avg: 0 Min: 0 Max: 0 Err: 1598327 (25.03%)
summary + 54100 in 00:00:30 = 1803.5/s Avg: 0 Min: 0 Max: 0 Err: 1065 (1.97%) Active: -72 Started: 215 Finished: 287
summary = 6438538 in 01:41:10 = 1060.7/s Avg: 0 Min: 0 Max: 0 Err: 1599392 (24.84%)
summary + 45028 in 00:00:31 = 1431.6/s Avg: 0 Min: 0 Max: 0 Err: 5110 (11.35%) Active: -94 Started: 215 Finished: 309
summary = 6483566 in 01:41:42 = 1062.6/s Avg: 0 Min: 0 Max: 0 Err: 1604502 (24.75%)
summary + 66072 in 00:00:29 = 2317.4/s Avg: 0 Min: 0 Max: 0 Err: 16097 (24.36%) Active: -124 Started: 215 Finished: 339
summary = 6549638 in 01:42:10 = 1068.4/s Avg: 0 Min: 0 Max: 0 Err: 1620599 (24.74%)
summary + 48153 in 00:00:30 = 1583.4/s Avg: 0 Min: 0 Max: 0 Err: 12524 (26.01%) Active: -150 Started: 215 Finished: 365
summary = 6597791 in 01:42:41 = 1071.0/s Avg: 0 Min: 0 Max: 0 Err: 1633123 (24.75%)
summary + 67147 in 00:00:30 = 2242.0/s Avg: 0 Min: 0 Max: 0 Err: 19554 (29.12%) Active: -190 Started: 215 Finished: 405
summary = 6664938 in 01:43:11 = 1076.6/s Avg: 0 Min: 0 Max: 0 Err: 1652677 (24.80%)
summary + 53262 in 00:00:26 = 2015.5/s Avg: 0 Min: 0 Max: 0 Err: 6029 (11.32%) Active: -249 Started: 215 Finished: 464
summary = 6718200 in 01:43:37 = 1080.6/s Avg: 0 Min: 0 Max: 0 Err: 1658706 (24.69%)
Tidying up remote @ August 22, 2022 1:50:26 PM CST (1661147426865)
... end of run
jmeter运行参数如下:
-n 表示使用non-gui模式
-t [JMX测试脚本的名称].
-l [保存JTL 测试结果文件的路径].
-j [JMeter log文件的路径].
-r 在JMeter的远程机器上运行测试,远程机器由 property " remote_hosts "指定
-R [远程机器列表] 在指定的远程机器上运行测试
-g [CSV 文件路径] 通过csv文件来创建dashboard报告
-e 运行结束后创建dashboard报告
-o 在哪个目录创建dashboard报告,该目录必须为空或者不存在
这里注意,-o
参数指定的目录必须是空目录,否则会报错误。
压力测试运行完以后,会在指定的目录中产生测试报告。
[root@migrate_7_59 result4]# ls -1
content
gcache-test.jtl
index.html
sbadmin2-1.0.7
statistics.json