欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > jmeter 对 dubbo 接口测试是怎么实现的?有哪几个步骤

jmeter 对 dubbo 接口测试是怎么实现的?有哪几个步骤

2024/10/24 8:33:02 来源:https://blog.csdn.net/weixin_67553250/article/details/142927046  浏览:    关键词:jmeter 对 dubbo 接口测试是怎么实现的?有哪几个步骤

 目录

前言

一.先了解下 dubbo 的原理,最好自己搭建一个案例可参考以下方式搭建

http://09792bb8.wiz03.com/share/s/09uiKU3j2kR120MIpT2AdLm70pfBmE1zFApv2jiDZ01GhE8j

二.编写 dubbo 测试脚本


前言

 最近使用工作中使用jmeter调用dubbo接口进行接口测试,在实际尝试中遇到了一些问题,这里把这些问题整理了出来,特编写此文档,用作记录,同时分享给有需要的童鞋。

      从我最近一段时间的测试来看,Jmeter调用dubbo接口主要有两种方式(可能存在我不知道的方式,如哪位知道,欢迎指点),一种是通过java调用实现;一种是通过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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://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);}
}
  1. 打包

点击 OK

点击 OK

Build 文件

第一次 先 Clean
第二次在 Build 生成

  1. 把生成的 jar 放到 apache-jmeter-3.2\lib\ext 目录下

9.重新启动 jmeter

10.jmeter GUI 界面使用方式
创建线程组

创建 java 请求

选择 dubbo 测试接口

添加察看树结果

执行脚本

查看最后的结果

到这里表示已执行成功


衷心感谢每一个阅读我文章的人      要是觉得可以的话请动手点个关注吧

 【已更新】B站讲的最详细的Python接口自动化测试实战教程全集(实战最新版)
》,以及配套的接口文档/项目实战【网盘资源】,需要的朋友可以下方视频的置顶评论获取。肯定会给你带来帮助和方向。

【已更新】B站讲的最详细的Python接口自动化测试实战教程全集(实战最新版)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com