一、简介
使用Jmeter性能测试工具,实现hessian接口的性能测试、接口测试、工具类集成等3个功能。
需要环境:JDK1.8、maven、Jmeter、eclipse或者 IntelliJ IDEA;
二、调试hessian接口测试类,封装Java请求;
1.新建maven项目;
2.配置pom.xml文件;
<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>jmeterApiTest-6</groupId>
<artifactId>jmeter-api-test-6</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jmeter-api-test-6</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 被测接口所依赖jar包,根据需要配置 -->
<dependency>
<groupId>cn.facebank</groupId>
<artifactId>fpmain-api-manage-service</artifactId>
<version>1.0.0.7</version>
</dependency>
<dependency>
<groupId>cn.facebank</groupId>
<artifactId>fpmain-api-model</artifactId>
<version>1.0.0.8</version>
</dependency>
<dependency>
<groupId>cn.facebank</groupId>
<artifactId>fpmain-api-user-service</artifactId>
<version>1.0.0.2</version>
</dependency>
<!-- hessian接口依赖包 -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>jorphan</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>3.1</version>
<exclusions>
<exclusion>
<groupId>d-haven-managed-pool</groupId>
<artifactId>d-haven-managed-pool</artifactId>
</exclusion>
<exclusion>
<artifactId>commons-math3</artifactId>
<groupId>commons-math3</groupId>
</exclusion>
<exclusion>
<artifactId>commons-pool2</artifactId>
<groupId>commons-pool2</groupId>
</exclusion>
</exclusions>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- 依赖jar包 jar-with-dependencies -->
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.新建下单接口测试类AddFpOrderTest,并继承AbstractJavaSamplerClient;
4.下单接口测试代码如下:
package jmeterApiTest_6.jmeter_api_test_6;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
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 com.caucho.hessian.client.HessianProxyFactory;
import cn.facebank.common.exception.PlateformException;
import cn.facebank.fpmain.api.model.FpOrderModel;
import cn.facebank.fpmain.api.user.service.FpOrderService;
public class AddFpOrderTest extends AbstractJavaSamplerClient {
// 创建LGroupon类,继承了AbstractJavaSamplerClient
private long start = 0;
private long end = 0;
private String amount;// BigDecimal
private String path;
private String productId;// Long
private String userId;// Long
private String redPackageId;// String
// 初始化方法,实际运行时每个线程仅执行一次,在测试方法运行前执行
public void setupTest(JavaSamplerContext arg0) {
// 开始时间
System.out.println("开始执行......start");
start = System.currentTimeMillis();
}
// 设置默认值,如果Jmeter中未设置变量,用此默认值
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("productId", "38");
params.addArgument("userId", "572004");
params.addArgument("amount", "1200");
params.addArgument("redPackageId", "");
params.addArgument("path", "http://192.168.1.5:25008");
return params;
}
// 运行runTest程序,调用hessian接口
public SampleResult runTest(JavaSamplerContext arg0) {
System.out.println("调用订单提交接口开始...........start");
productId = arg0.getParameter("productId");
userId = arg0.getParameter("userId");
amount = arg0.getParameter("amount");
redPackageId = arg0.getParameter("redPackageId");
path = arg0.getParameter("path");
SampleResult results = new SampleResult();
results.setSampleLabel("Java请求");
results.sampleStart();// jmeter 开始统计响应时间标记
try {
// 通过下面的操作就可以将被测方法的响应输出到Jmeter的察看结果树中的响应数据里面了。
addFpOrder(Long.valueOf(productId), Long.valueOf(userId),
new BigDecimal(amount).setScale(2,
BigDecimal.ROUND_HALF_EVEN), redPackageId);
results.setSuccessful(true);
System.out.println("调用订单提交接口结束...........over");
} catch (Throwable e) {
results.setSuccessful(false);
e.printStackTrace();
} finally {
results.sampleEnd();// jmeter 结束统计响应时间标记
}
System.out.println("结束执行.....over");
return results;
}
// 测试结束时调用;
public void teardownTest(JavaSamplerContext arg0) {
end = System.currentTimeMillis();
System.out.println(end);
System.out.println("The cost is" + (end - start) + "ms");
}
// 提交订单接口实现方法,一般从开发人员单元测试类中获取
public void addFpOrder(Long productId, Long userId, BigDecimal amount,
String redPackageId) throws PlateformException, IOException {
String url = path + "/remoting/fpOrderService";
FpOrderService fpOrderService = (FpOrderService) getHessianProxyService(
url, FpOrderService.class);
FpOrderModel model = new FpOrderModel();
model.setFpOrderId(0L);
model.setProductId(productId);
model.setUserId(userId);
model.setAmount(amount);
model.setRemark("");
fpOrderService.addFpOrder(model, redPackageId);
}
public Object getHessianProxyService(String url, Class classInstance) {
HessianProxyFactory factory = new HessianProxyFactory();
Object service = null;
try {
service = factory.create(classInstance, url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return service;
}
// main只是为了调试用,最后打jar包的时候注释掉。
public static void main(String[] args) {
// 获取需要变量
Arguments params = new Arguments();
params.addArgument("productId", "159875425");
params.addArgument("userId", "572004");
params.addArgument("amount", "1000");
params.addArgument("redPackageId", "");
params.addArgument("path", "http://192.168.1.5:25008");
JavaSamplerContext arg0 = new JavaSamplerContext(params);
AddFpOrderTest addFpOrderTest = new AddFpOrderTest();
addFpOrderTest.setupTest(arg0);
addFpOrderTest.runTest(arg0);
addFpOrderTest.teardownTest(arg0);
}
}
5.脚本调试通过后,注释main方法;
6.右键项目名称,选择 Run As>Maven assembly:assembly,生成两个jar包;
三、Jmeter集成接口测试jar包;
1.将两个jar包放在Jmeter路径apache-jmeter-3.1libext 下;
2.打开Jmeter,新建测试计划-线程组-Java请求;
3.选择需要进行测试的接口名称,并填写相应变量;
4.配置Jmeter测试计划,进行性能测试/接口测试/工具类使用;
5.如果开发并未提供测试类,可直接对原来的方法进行调用
如例子中的下订单,原来的方法为:
public Long addFpOrder(final FpOrderModel model, String redPackageId) throws PlateformException, IOException
可以自己定义一个model , 并将传入的参数赋值给 model对象即可。
6.添加下单接口脚本