JMeter测试dubbo接口

一、准备

1、  开发环境准备:

需要环境:Jdk1.8、Maven;

开发工具:intellij idea(也可自行选择eclipse);

测试工具:Jmeter3.2;

2、  测试环境准备:

待测试的dubbo服务提供者已经启动,本地可以调用

二、测试代码书写

1、像平时写工程一样新建一个测试项目

(1)打开file -> new-> Project -> maven -> next, 新建maven工程

(2)填写groupId、artifactId、version

GroupId:项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。

ArtifactId:就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。

Version:版本号。

 

(3)填写project name

2、  配置pom.xml文件(IDEA 已经集成完毕maven了)

配置好自己的maven地址,仓库,在pom文件中写好自己需要的所有依赖

build,dubbo接口,dubbo,zk,jmeter,spring等所需依赖,注意自己排除冲突包

<!-- 构建jar的build设置>
<build>
        <finalName>dubbo-test</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>libs/</classpathPrefix>
                            <!-- 此处指定main方法入口的class -->
                            <mainClass>com.main.TestMain</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.basedir}/libs</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <!-- 待测试的dubbo接口 按需排包 -->
    <dependencies>
        <dependency>
            <groupId>com.test.***</groupId>
            <artifactId>***-api</artifactId>
            <version>3.1.92</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-core</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
       
        <!-- dubbo 依赖 按需排包 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>javassist</artifactId>
                    <groupId>org.javassist</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.22.0-GA</version>
        </dependency>

        <!-- dubbo end -->

        <!--zookeeper客户端 按需排包-->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j</artifactId>
                    <groupId>log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--jmeter 依赖 按需排包-->
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>2.8</version>
        </dependency>

        <!--其他所需依赖 按需排包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
    </dependencies>

3.编写dubbo服务consumer代码

 dubbo服务提供者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
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- dubbo配置 -->
    <dubbo:application name="dubbo-test"/>
	<dubbo:monitor protocol="registry"/>

	<!-- zk注册中心配置 -->
	<dubbo:registry id="service-registry" address="zookeeper://zk.goo.com:2181" />

    <!-- 测试服务配置 -->
	<dubbo:reference id="****Service" interface="com.***.***Service"
					 timeout="10000" version="1.0.0" registry="*****-service-registry" check="false" init="true" />

</beans>

 

 dubbo初始化代码,单例模式

package com.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboInit {
    private static DubboInit init = null;
    private DubboInit(){}
    private  static ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/dubbo-client.xml");

    public synchronized static DubboInit getInstance(){
        if(init == null){
            init = new DubboInit();
        }
        return init;
    }

    public  Object getBean(String beanName) {
        return context.getBean(beanName);
    }
}

主测试类,需要继承JMeter的类AbstractJavaSamplerClient ,重写 setupTes、runTest、teardownTest方法。

package com.dubbo;

import ***.ServiceResult;
import ***.***Service;
import ***.JsonUtils;
import ***.StringUtils;
import com.util.DubboInit;
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.List;

public class DubboService extends AbstractJavaSamplerClient {
    //待测试的接口
    private static OpenService openService;
    private static long start = 0;
    private static long end = 0;

    @Override
    public void setupTest(JavaSamplerContext arg0){
        DubboInit init = DubboInit.getInstance();
        openService=(OpenService)init.getBean("openService");
        start = System.currentTimeMillis();
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        //接收Jmeter传入的参数
        String param1= javaSamplerContext.getParameter("param1");
        String param2= javaSamplerContext.getParameter("param2");
        String param3= javaSamplerContext.getParameter("param3");

        //如果没有参数,就使用自己的参数
        if (StringUtils.isEmpty(param1)){
            param1= "1AUU2TV30NM1BJ001GKNO3G1621FL4B5";
        }
        if (StringUtils.isEmpty(param2)){
            param2= "";
        }
        if (StringUtils.isEmpty(param3)){
            param3= "1AUU2TV30NM1BJ001259O3G1621FL4B5";
        }

        SampleResult sr = new SampleResult();
        sr.setSamplerData("dubbo测试接口");
        sr.sampleStart();// jmeter 开始统计响应时间标记
        ServiceResult<List<String>> result = null;
        try {
            //待测试的dubbo接口 调用
            result =  openService.getAddrsByName(orgId,shopEntityId,lowerMethod);
        }catch (Exception e){
            e.printStackTrace();
            sr.setSuccessful(false);
            sr.setResponseData(e.getMessage(), "utf-8");
            sr.setDataType(SampleResult.TEXT);
        }
        if(result!=null){
            sr.setResponseData("结果是:" + JsonUtils.toJson(result), "utf-8");
            sr.setDataType(SampleResult.TEXT);
            sr.setSuccessful(true);
        }else {
            sr.setSuccessful(false);
        }
        sr.sampleEnd();// jmeter 结束统计响应时间标记
        return sr;
    }

    //测试结束时调用;
    @Override
    public void teardownTest(JavaSamplerContext arg0) {
        end = System.currentTimeMillis();
        // 总体耗时
        System.err.println("cost time:" + (end - start) + "毫秒");
    }
}

调用下看看

package com.main;

import com.dubbo.DubboService;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;

public class TestMain {
    public static final void  main(String [] args){
        JavaSamplerContext arg0 = new JavaSamplerContext(new Arguments());
        DubboService test=new DubboService();
        test.setupTest(arg0);
        test.runTest(arg0);
        System.out.print("完成");
    }
}

 4.调用执行没有问题,打包->jar

 4.Jmeter测试

将jar包放入JMeter安装目录\lib\ext中

启动JMeter,JMeter安装目录\bin\jmeter.bat

(双击ApacheJMeter.jar也可以启动,出现崩溃无法找到问题,推荐使用jmeter.bat)

测试计划->右键->添加->threads->线程组

 

线程组->右键->添加->Sampler->Java请求

 

如果点击发现什么反应都没有,考虑下你的jar包是否有依赖没有进来

将依赖包也导入到ext中,重启JMeter后,再次操作,没有问题了,在Java请求中选择刚刚jar包中service

 

 继续,Java请求->右键->添加->监听器->查看结果树

OK,我们可以测试一下调用是否成功。默认线程组中是一个线程,1次请求,直接开始吧

 

点击绿箭头执行,又报错了,解决它。也就是说,JMeter调用过程中,可能因为依赖问题,会在各个环节出错,我们要测试使用JMeter调用一次成功后,再准备配置并发。

 产生这种错误的原因多数是依赖问题,我发现ext包中有两个不同版本的依赖,去掉低版本,成功运行。

 我们再试一下带参数的调用。

 

我们的结果同样没有问题

 我们可以启动测试了,

在线程组下添加常用的3个监听器,jp@gc - Transactions per Second , jp@gc - ResponseTimes Over Time , 聚合报告

设定2个线程,1分钟内达到最高,永远循环(可以手动停下)

好的,跑起来看看这三个监听器的报告吧。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMeter是一款功能强大的性能测试工具,可以用于测试各种类型的接口,包括支付接口。下面是使用JMeter测试支付接口的步骤: 1. 下载和安装JMeter:首先,你需要从JMeter官方网站下载并安装JMeter。安装完成后,你可以启动JMeter。 2. 创建测试计划:在JMeter中,你需要创建一个测试计划来组织你的测试。右键点击"Test Plan",选择"Add" -> "Threads (Users)" -> "Thread Group"来添加一个线程组。线程组用于模拟并发用户。 3. 添加HTTP请求:在线程组下,右键点击"Add" -> "Sampler" -> "HTTP Request"来添加一个HTTP请求。在HTTP请求中,你需要设置请求的URL、请求方法(GET、POST等)以及其他参数。 . 添加参数:如果支付接口需要传递参数,你可以在请求中添加参数。右键点击HTTP请求,选择"Add" -> "Config Element" -> " Request Defaults"来添加HTTP请求的默认参数。然后,在HTTP请求中添加参数。 5. 添加断言:断言用于验证接口的返回结果是否符合预期。右键点击HTTP请求,选择"Add" -> "Assertions" -> "Response Assertion"来添加一个断言。你可以根据接口的返回结果设置断言条件。 6. 配置线程组:在线程组中,你可以设置并发用户的数量、循环次数等参数。右键点击线程组,选择"Add" -> "Config Element" -> "HTTP Cookie Manager"来添加一个Cookie管理器,以便在测试过程中保持会话状态。 7. 运行测试:点击JMeter工具栏上的"运行"按钮来运行你的测试JMeter将模拟并发用户发送请求到支付接口,并记录响应时间、吞吐量等性能指标。 8. 查看结果:在JMeter的结果树中,你可以查看每个请求的响应结果和性能指标。你可以使用图表和报告来分析测试结果。 这是一个简单的示例,你可以根据你的具体需求进行更详细的配置和测试

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值