作者: whooyun发表于: 2017-03-29 16:14
使用jmeter进行dubbo接口测试前需要完成以下工作:
1、搭建好dubbo服务提供方,并进行Junit测试,确保服务提供方能正常运行;
2、搭建好dubbo服务消费方,并且和服务方进行联调,确保服务消费方能正常调用服务提供方;
因为本篇只介绍使用jmeter测试dubbo接口,所以dubbo服务的搭建不在此深究和介绍。
完成以上俩个步骤后继续做以下的操作:
1、在服务消费方中增加Jmeter测试代码;
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>com.why.dubbotest</groupId>
<artifactId>dubboJemeter</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.qx.thirteen</groupId>
<artifactId>dubboServer</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>3.0</version>
</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>
InitAppContext类
package com.dubbo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by Administrator on 2017/3/29 0029. * 初始化ClassPathXmlApplicationContext */ public class InitAppContext { private volatile static InitAppContext initApplicationContext; private static ClassPathXmlApplicationContext context; private InitAppContext() { } public static InitAppContext getInstance() { if (initApplicationContext == null) { synchronized (InitAppContext.class) { if (initApplicationContext == null) { initApplicationContext = new InitAppContext(); } } } return initApplicationContext; } public void init(){ try { context =new ClassPathXmlApplicationContext(new String[]{"classpath:dubbo-config/dubboConfig.xml"}); context.start(); }catch (Exception e){ e.printStackTrace(); } } public ClassPathXmlApplicationContext getApplicationContext(){ return context; } public Object getBean(String beanName){ return context.getBean(beanName); } }
ClassDemoConsumer接口测试类,它继承了AbstractJavaSamplerClient,里面主要setupTest,teardownTest,getDefaultParameters,还有一个上级接口JavaSamplerClient的方法runTest。
setupTest 用来处理运行接口前需要做的操作
runTest 运行你的业务逻辑,也就是调用接口;
teardownTest 运行完以后,需要对接口做的操作,比如销毁context等等;
getDefaultParameters设置自己的请求参数
package com.dubbo; import com.alibaba.dubbo.config.spring.ServiceBean; import com.why.staff.entity.Staff; import com.why.staff.service.SearchStaffService; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dubbo.InitAppContext; import java.util.Random; /** * Created by Administrator on 2017/3/27 0027. */ public class ClassDemoConsumer extends AbstractJavaSamplerClient { private SearchStaffService demoService = null; InitAppContext cntxt = null; @Override public void setupTest(JavaSamplerContext a) { cntxt = InitAppContext.getInstance(); cntxt.init(); } public SampleResult runTest(JavaSamplerContext arg0) { SampleResult sr = new SampleResult(); try { sr.sampleStart(); System.out.println(cntxt.getBean("searchStaff_lms")); SearchStaffService demoService = (SearchStaffService) cntxt.getBean("searchStaff_lms"); String staffName = demoService.searchOneStaff("admin", "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8").getStaffName(); sr.setResponseData("staff is exist,staff's name is " + staffName, null); sr.setDataType(SampleResult.TEXT); sr.setSuccessful(true); sr.sampleEnd(); } catch (Exception e) { e.printStackTrace(); } return sr; } @Override public void teardownTest(JavaSamplerContext context) { super.teardownTest(context); if(cntxt.getApplicationContext()!=null){ cntxt.getApplicationContext().destroy(); } } }
2、把服务消费方测试程序(java控制台程序,original打头)打包成jar,放到jmeter》lib》ext目录下
3、把服务提供方(java控制台程序)打成jar(包括服务消费方依赖的jar)放到jmeter》lib目录下
注意:记得打包的时候只需要留着消费方依赖的接口和实体类就好了,不然jmeter会当成程序运行(导致无法正常测试),
4、双击jmeter.bat文件(能正常弹出界面,说明jar包没问题),新建jmeter线程组
5、添加java请求,选中自己要测试的接口
把你的测试计划保存下,备查6、新建查看结果树(这样运行的时候就能看到结果了),并点击运行,并双击查看结果树,查看测试结果
另注意:如果你在jmeter的测试代码里面有写System.out.println(),这些信息会在jmeter的控制台输出