Jmerte分布式压测
sarah0619 人气:01.本地基于jmeter创建压测项目
(1)pom中依赖jmeter包:
<dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_java</artifactId> <version>5.2.1</version> </dependency>
(2)pom中增加打包方式
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <fork>true</fork> <verbose>true</verbose> <encoding>UTF-8</encoding> <compilerArguments> <extdirs>${project.basedir}/lib</extdirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResources> <resource> <!--配置本地jar包在项目中的存放路径--> <directory>${project.basedir}/lib</directory> <!--配置打包时jar包的存放路径--> <targetPath>${project.basedir}/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- 忽略测试 --> <skip>false</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifestEntries> <Rose>*</Rose> </manifestEntries> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
(3)编写压测脚本:
继承AbstractJavaSamplerClient,并实现runTest方法。
public class StessTest extends AbstractJavaSamplerClient { @Override public void setupTest(JavaSamplerContext javaSamplerContext) { } @Override @SneakyThrows public SampleResult runTest(JavaSamplerContext javaSamplerContext) { SampleResult sampleResult = new SampleResult(); try { sampleResult.sampleStart(); String result= 请求接口; sampleResult.setResponseData(result, "UTF-8"); sampleResult.setSuccessful(false); TimeUnit.SECONDS.sleep(1L); } catch (Exception e) { e.printStackTrace(); sampleResult.setResponseData(e.getLocalizedMessage(), "UTF-8"); sampleResult.setResponseCode(e.getMessage()); sampleResult.setSuccessful(false); } finally { sampleResult.sampleEnd(); } return sampleResult; } @Override public void teardownTest(JavaSamplerContext javaSamplerContext) { super.teardownTest(javaSamplerContext); } @Override public Arguments getDefaultParameters() { return null; }
2.将项目打包
将本地maven测试项目打成jar包:
mvn -U clean insall package
打包完后,有两个地方的包:target\lib、target下面的包
首先本地调试压测脚本是否生效
(1)创建一个线程组:
这里可以配置线程组名称,线程数,准备时长、循环次数,调度器等参数
(2)创建java请求
选好测试的方法,本地执行一下,看可以跑通后,去远程机器压测。
确定好Master和Slave机器,及被压测服务所在的机器。且master机器 及slave机器上都配置相同版本的jmeter。
将项目依赖的jar包都放到slave机器 jmeter\lib\ext路径下面。
3.Master配置
编辑$JMETER_HOME/bin/jmeter.properties文件
分别配置remote_hosts,和server.rmi.ssl.disable,端口号
server.rmi.ssl.disable=true server.rmi.localport=1099 server_port=1099 remote_hosts=ip1,ip2,ip3,ip4,ip5,ip6
4.Slave配置
(1)所有的slave机器上对应修改 $JMETER_HOME/bin/jmeter.properties文件
server.rmi.ssl.disable=true
(2)所有的slave机器需要在$JMETER_HOME/lib/ext下放压测脚本依赖的jar包
5.启动Slave
每台slave机器上都启动jmeter
直接启动
$JMETER_HOME/bin/jmeter-server
或者指定机器的ip启动(建议指定ip启动)
jmeter-server -Djava.rmi.server.hostname=xxx (机器的ip地址)
6.master启动压测
onlinePressTest.jmx是用界面版jmeter生成的一个线程组,保存下来,放到master机器上
运行压测脚本:
jmeter -n -r -t onlinePressTest.jmx -l stressTest.jtl -e -o webStressTest
7查看报告
stressTest.jtl 是压测结果,可以导出到本地,使用jmeter打开,获取报告
加载全部内容