1、Jmeter简介
Apache JMeter是Apache组织开发的基于Java的压力测试工具。用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域。 它可以用于测试静态和动态资源,例如静态文件、Java 小服务程序、CGI 脚本、Java 对象、数据库、FTP 服务器, 等等。JMeter 可以用于对服务器、网络或对象模拟巨大的负载,来自不同压力类别下测试它们的强度和分析整体性能。另外,JMeter能够对应用程序做功能/回归测试,通过创建带有断言的脚本来验证你的程序返回了你期望的结果。为了最大限度的灵活性,JMeter允许使用正则表达式创建断言。
Apache jmeter 可以用于对静态的和动态的资源(文件,Servlet,Perl脚本,java 对象,数据库和查询,FTP服务器等等)的性能进行测试。它可以用于对服务器、网络或对象模拟繁重的负载来测试它们的强度或分析不同压力类型下的整体性能。你可以使用它做性能的图形分析或在大并发负载测试你的服务器/脚本/对象。
2、我们为什么使用Jmeter
开源免费,基于Java编写,可集成到其他系统可拓展各个功能插件
支持接口测试,压力测试等多种功能,支持录制回放,入门简单
相较于自己编写框架活其他开源工具,有较为完善的UI界面,便于接口调试
多平台支持,可在Linux,Windows,Mac上运行
3.1、创建项目
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.memedai</groupId>
<artifactId>me-jmeter-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- jmeter 必须1-->
<!-- <dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>2.1.3</version>
</dependency>-->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>cn.memedai</groupId>
<artifactId>me-notification-sms-facade</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.memedai.framework</groupId>
<artifactId>me-fm-dubbo-ext</artifactId>
<version>2.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
<!-- spring 必须4-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!-- dubbo 必须5-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1mm</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<!-- https://issues.apache.org/jira/browse/ZOOKEEPER-1371 zookeeper Remove
dependency on log4j in the source code. slf4j-log4j12会与log4j-over-slf4j循环依赖导致冲突 -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
注意:dubbo和facade请根据自己公司使用的进行更换 不然肯定会报错的
package cn.memedai.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author hongwang.zhang
* @version: 1.0
* @date 2018/7/59:39
* @see
**/
public class DubboInit {
private static DubboInit init = null;
private DubboInit(){}
private static ApplicationContext context;
public synchronized static DubboInit getInstance(){
if(init == null){
init = new DubboInit();
}
return init;
}
public DubboInit initApplicationContext(){
context = new ClassPathXmlApplicationContext("classpath:/applicationContext-dubbo.xml");
if(context==null)
{
throw new IllegalArgumentException("Load dubbo-client.xml fail");
}
return this;
}
public Object getBean(String beanName) {
return context.getBean(beanName);
}
public Object getBean(Class beanName) {
return context.getBean(beanName);
}
}
JMeter java Sampler介绍
返回类型 | 方法 |
---|---|
SampleResult | getDefaultParameters() 用于获取界面的参数 |
SampleResult | runTest(JavaSamplerContext context) 类似于LR的Action,result.sampleStart()一个事务开始,result.sampleEnd()一个事务 结束 |
void | setupTest(JavaSamplerContext context) 初始化方法,类似于LR的init和Junit中的setUp() |
void | teardownTest(JavaSamplerContext context) 类似于LR的end和Junit中的tearDown() |
package cn.memedai.jmeter.notiectionsms.notionficationsms;
import cn.memedai.menotification.facade.sms.business.MeSmsBusiness;
import cn.memedai.util.DubboInit;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.Map;
/**
* @author hongwang.zhang
* @version: 1.0
* @date 2018/7/510:10
* @see
**/
public class MeSmsBusinessJmeter extends AbstractJavaSamplerClient {
MeSmsBusiness meSmsBusiness;
private long start = 0;//记录测试开始时间;
private long end = 0;//记录测试结束时间;
private String phoneNo;
private String ext;
/**
* 初始化操作
*
* @param context
*/
@Override
public void setupTest(JavaSamplerContext context) {
DubboInit instance = DubboInit.getInstance().initApplicationContext();
meSmsBusiness = (MeSmsBusiness) instance.getBean("meSmsBusiness");
}
// 这个方法是用来自定义java方法入参的。
// params.addArgument("num1","");表示入参名字叫num1,默认值为空。
// 设置可用参数及的默认值;
@Override
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("phoneNo", phoneNo);//未设默认值
params.addArgument("ext", ext);
return params;
}
public SampleResult runTest(JavaSamplerContext agr0) {
start = System.currentTimeMillis();
SampleResult result = new SampleResult();
phoneNo = agr0.getParameter("phoneNo");
ext = agr0.getParameter("ext");
Map<String, Object> stringObjectMap = null;
try {
stringObjectMap = meSmsBusiness.sendVerificationCode(phoneNo, ext);
result.setSuccessful(true);
result.setResponseData(stringObjectMap.toString(), null);
result.setDataType(SampleResult.TEXT);
} catch (Exception e) {
getLogger().error("LqueryTakeRegNo response error : " + e.getMessage());
result.setSuccessful(false);
}
end = System.currentTimeMillis();
return result;
}
@Override
public void teardownTest(JavaSamplerContext arg0) {
end = System.currentTimeMillis();
getLogger().info(" cost time: " + (end - start) + "ms");
}
}
然后打成jar包 放在D:\tool\apache-jmeter-4.0\lib\ext 目录下
注意最好使用和maven版本一样的 客户端
然后打开jmeter
选择需要测试的类 填写参数
点击运行