jmeter 对 dubbo 接口测试
Posted TesterHome
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jmeter 对 dubbo 接口测试相关的知识,希望对你有一定的参考价值。
一.先了解下dubbo的原理,最好自己搭建一个案例
可参考以下方式搭建
http://09792bb8.wiz03.com/share/s/09uiKU3j2kR120MIpT2AdLm70pfBmE1zFApv2jiDZ01GhE8j
二.编写dubbo测试脚本
源码:
https://git.coding.net/mgjerome/jmeter_dubbo.git
1.创建jmeter_dubbo Maven项目
直接Next创建项目
2.添加pom.xml 配置
<properties>
<spring.version>3.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo注册中心-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--zookeeper客户端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
3.resources目录下创建lib文件夹存放, dubbo接口jar
ApacheJMeter_core.jar ApacheJMeter_java.jar (apache-jmeter-3.2\lib\ext 目录下)
jorphan.jar(apache-jmeter-3.2\lib目录下 用于main调试执行)
dubbo.xsd(由于http://code.alibabatech.com/schema/dubbo/dubbo.xsd 服务已停用 从网上下载该文件,或者从dubbo-2.5.3.jar META-INF 目录下导出
)
4.创建dubbo消费者xml文件 consumer.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
./dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="hello-consumer" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="helloService" interface="com.service.HelloService" /></beans>
5.编写jmeter脚本
package com.buddo;
import com.service.HelloService;
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 org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by CWGJ008 on 2017/10/13.
*/
public class Dubbo_port extends AbstractJavaSamplerClient {
private static ApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
private static HelloService helloService;
private static long start = 0;
private static long end = 0;
public void setupTest(JavaSamplerContext arg0){
helloService=(HelloService)context.getBean("helloService");
start = System.currentTimeMillis();
}
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sr = new SampleResult();
sr.setSamplerData("dubbo测试案例");
sr.sampleStart();// jmeter 开始统计响应时间标记
String result = helloService.speakHello("chen");
System.out.println(result);
if(result.contains("chen")){
sr.setResponseData("结果是:" + result, null);
sr.setDataType(SampleResult.TEXT);
sr.setSuccessful(true);
}else {
sr.setSuccessful(false);
}
sr.sampleEnd();// jmeter 结束统计响应时间标记
return sr;
}
//测试结束时调用;
public void teardownTest(JavaSamplerContext arg0) {
end = System.currentTimeMillis();
// 总体耗时
System.err.println("cost time:" + (end - start) + "毫秒");
}
}
6.编写测试类 , 测试执行完 打包时记得注解
package com.buddo;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
/**
* Created by CWGJ008 on 2017/10/13.
*/
public class TestMain {
public static final void main(String [] args){
JavaSamplerContext arg0 = new JavaSamplerContext(new Arguments());
Dubbo_port test=new Dubbo_port();
test.setupTest(arg0);
test.runTest(arg0);
}
}
打包
点击OK
点击OK
Build文件
第一次 先 Clean
第二次在Build生成
把生成的jar放到 apache-jmeter-3.2\lib\ext 目录下
9.重新启动jmeter
10.jmeter GUI界面使用方式
创建线程组
创建java请求
选择dubbo测试接口
添加察看树结果
执行脚本
查看最后的结果
到这里表示已执行成功
以上是关于jmeter 对 dubbo 接口测试的主要内容,如果未能解决你的问题,请参考以下文章