一、jmx文件常用参数:
<hashTree>
<SetupThreadGroup guiclass="SetupThreadGroupGui" testclass="SetupThreadGroup" testname="10人线程组" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp> <!--无限循环-->
<stringProp name="LoopController.loops">5</stringProp> <!--循环数-->
</elementProp>
<stringProp name="ThreadGroup.num_threads">10000</stringProp> <!--并发数-->
<stringProp name="ThreadGroup.ramp_time">50</stringProp> <!--50sec完成-->
<boolProp name="ThreadGroup.scheduler">false</boolProp> <!--调度器开关-->
<stringProp name="ThreadGroup.duration">100</stringProp> <!--持续时间,sec为单位--> #循环次数有固定值且 ≠ -1,持续时间不会生效,以循环次数为准循环次数设置为永远或 -1时,持续时间才会生效
<stringProp name="ThreadGroup.delay"></stringProp> <!--启动延时,sec为单位--> #测试计划开始后,线程组的线程将在多少秒后再启动运行
</SetupThreadGroup>
<hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="users_api" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">192.168.96.168</stringProp> <!--服务器名称或者ip-->
<stringProp name="HTTPSampler.port">80</stringProp> <!--请求端口-->
<stringProp name="HTTPSampler.protocol">http</stringProp> <!--请求协议-->
<stringProp name="HTTPSampler.contentEncoding">utf-8</stringProp> <!--内容编码-->
<stringProp name="HTTPSampler.path">web/ab.html</stringProp> <!--请求路径-->
<stringProp name="HTTPSampler.method">GET</stringProp> <!--请求方法-->
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp> <!--使用keepalive开关-->
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp> <!--连接超时-->
<stringProp name="HTTPSampler.response_timeout"></stringProp> <!--响应超时-->
</HTTPSamplerProxy>
二、配置定时器:
统一随机定时器
<hashTree>
<UniformRandomTimer guiclass="UniformRandomTimerGui" testclass="UniformRandomTimer" testname="统一随机定时器" enabled="true">
<stringProp name="ConstantTimer.delay">5000.0</stringProp>
<stringProp name="RandomTimer.range">20000.0</stringProp>
</UniformRandomTimer>
固定定时器:
<ConstantTimer guiclass="ConstantTimerGui" testclass="ConstantTimer" testname="固定定时器" enabled="true">
<stringProp name="ConstantTimer.delay">1000</stringProp>
</ConstantTimer>
三、开启keepalive
java与httpclient4的区别:
Java:选择压测时,链接是复用的
httpclient4:压测时,每请求一次都创建一个新的链接
设置httpclient.reset_state_on_thread_group_iteration = false
jmx文件中
<boolProp name="HTTPSampler.use_keepalive">true</boolProp> 改成true
方法1:Java+keep-alive
vi /home/k5user/apache-jmeter-5.0/bin/jmeter.properties
jmeter.httpsampler=Java
缺点:无法控制长连接保持时间。
方法2:httpclient4+keep-alive
vi /jmeter/bin/jmeter.properties
httpclient.reset_state_on_thread_group_iteration 改成false
httpclient4.time_to_live改成0 (一直保持)
缺点:httpd服务默认Prefork工作模式,一个进程处理一个http请求,长连接不结束就一直占用进程。
四、分布式测压:
当一台jmeter并发数达到瓶颈时,需要多台jmeter一台控制机(master),多台负载机(slave)来进行分布式压测 扩大并发数。
1.slave机配置
vi /jmeter/bin/jmeter-server
# If the client fails with:
# ERROR - jmeter.engine.ClientJMeterEngine: java.rmi.ConnectException: Connection refused to host: 127.0.0.1
# then it may be due to the server host returning 127.0.0.1 as its address
# One way to fix this is to define RMI_HOST_DEF below
#RMI_HOST_DEF=-Djava.rmi.server.hostname=xxx.xxx.xxx.xxx
${DIRNAME}/jmeter ${RMI_HOST_DEF} -Dserver_port=${SERVER_PORT:-1099} -s -j jmeter-server.log "$@"
#RMI_HOST_DEF=-Djava.rmi.server.hostname=xxx.xxx.xxx.xxx改成vm的ip
vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.6 localhost
2.在master机修改jmeter.properties配置
# Remote Hosts - comma delimited
remote_hosts=127.0.0.1
#remote_hosts=localhost:1099,localhost:2010
改为remote_hosts=10.0.4.147:1099,10.0.4.148:1099
3.测压
slave机执行:
sh /jmeter/bin/jmeter-server -Djava.rmi.server.hostname=192.168.100.215
开始:Starting the test on host 192.168.100.215:1099
结束:Finished the test on host 192.168.100.215:1099
master机执行:
jmeter -n -t test.jmx -R 10.1.44.122,10.1.44.123 或 jmeter -n -t test.jmx -r
-R 指定多少个slave参与测试
-r 启动所有slave参与测试
正常情况:
jmeter -n -t xxx.jmx -r
Creating summariser <summary>
Created the tree successfully using xxx.jmx
Configuring remote engine: 192.168.100.50:1099
Configuring remote engine: 192.168.100.25:1099
Starting remote engines
slave机未执行sh /jmeter/bin/jmeter-server -Djava.rmi.server.hostname=192.168.100.215
master报错:
Connection refused to host: 192.168.100.67; nested exception is:
java.net.ConnectException: Connection refused
Failed to configure 192.168.100.67:1099
Configuring remote engine: 192.168.100.61:1099
Connection refused to host: 192.168.100.61; nested exception is:
java.net.ConnectException: Connection refused
Failed to configure 192.168.100.61:1099
Configuring remote engine: 192.168.100.192:1099
Configuring remote engine: 192.168.100.85:1099
Connection refused to host: 192.168.100.85; nested exception is:
java.net.ConnectException: Connection refused
注:报错后需要将所有slave机重新运行脚本
五、常见报错
java.lang.OutOfMemoryError: unable to create new native thread
Exception java.lang.InternalError: java.io.FileNotFoundException: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64/jre/lib/ext/cldrdata.jar (Too many open files). See log file for details.
Linux系统默认open files数目为1024, 有时应用程序会报Too many open files的错误,是因为open files 数目不够。
解决方法
1、永久性:
在/etc/security/limits.conf最后增加如下两行记录
* soft nofile 65535 单个用户可用的最大进程数量(超过会警告);
* hard nofile 65535 单个用户可用的最大进程数量(超过会报错);
重启生效
2、临时性
ulimit -n 102400
ulimit -a 查看 open files
[root@localhost etc]# ulimit -a
open files (-n) 102400
Response code:Non HTTP response code: java.net.NoRouteToHostException端口不够用
解决方法:
vi /etc/sysctl.conf
net.ipv4.tcp_tw_reuse = 1 //减少处于FIN-WAIT-2连接状态的时间,使系统可以处理更多的连接。
net.ipv4.tcp_max_tw_buckets = 5000 //表示允许将TIME_WAIT状态的socket重新用于新的TCP链接,该参数默认为180000,过多的TIME_WAIT套接字会使Web服务器变慢。
net.ipv4.ip_local_port_range = 1024 65535 //增加客户端端口可用范围
执行: sysctl -p ,使设置立即生效