自动化工具 jmeter 对 dubbo 接口测试

chen · 2017年10月17日 · 最后由 李小巍 回复于 2019年07月02日 · 6499 次阅读
本帖已被设为精华帖!

一.先了解下 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);

    }
}
  1. 打包

点击 OK

点击 OK

Build 文件

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

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

9.重新启动 jmeter

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

创建 java 请求

选择 dubbo 测试接口

添加察看树结果

执行脚本

查看最后的结果

到这里表示已执行成功

共收到 16 条回复 时间 点赞
思寒_seveniruby 将本帖设为了精华贴 10月18日 01:57

学习了。内容很详细

挺好的!

之前用 jmeter 做过公司开放服务平台 sdk 的并发测试,步骤和楼主类似,内容很详细,感谢分享

源码地址已失效,最新的地址是哪里?想参考下,谢谢

chen #8 · 2017年12月08日 Author
onionyao 回复

git@git.coding.net:mgjerome/jmeter_dubbo.git 试试这个

仅楼主可见

感谢楼主分享,准备尝试

源码怎么下载不下来呢?

源码下载不了,麻烦楼主给看看,谢谢

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/avalon/framework/configuration/ConfigurationException
at org.apache.jmeter.protocol.java.sampler.JavaSamplerContext.(JavaSamplerContext.java:49)
at com.dubbo.TestMain.main(TestMain.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.apache.avalon.framework.configuration.ConfigurationException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more,

有人遇到这种情况吗?

缺少 jar 包:

org.apache.avalon.framework
avalon-framework-api
4.3.1

请问谁遇到过这个情况
018/07/16 20:40:06 INFO - jmeter.samplers.SampleResult: sampleresult.nanoThreadSleep=5000
2018/07/16 20:40:06 INFO - jmeter.gui.util.MenuFactory: Skipping org.apache.jmeter.visualizers.SplineVisualizer
2018/07/16 20:40:14 ERROR - jmeter.gui.GuiPackage: Problem retrieving gui for org.apache.jmeter.protocol.java.control.gui.JavaTestSamplerGui java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.jmeter.protocol.java.config.gui.JavaConfigGui.actionPerformed(JavaConfigGui.java:169)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at org.apache.jmeter.protocol.java.config.gui.JavaConfigGui.clearGui(JavaConfigGui.java:278)
at org.apache.jmeter.protocol.java.control.gui.JavaTestSamplerGui.clearGui(JavaTestSamplerGui.java:97)
at org.apache.jmeter.gui.GuiPackage.createTestElement(GuiPackage.java:339)
at org.apache.jmeter.gui.action.AddToTree.doAction(AddToTree.java:68)
at org.apache.jmeter.gui.action.ActionRouter.performAction(ActionRouter.java:80)
at org.apache.jmeter.gui.action.ActionRouter.access$000(ActionRouter.java:40)
at org.apache.jmeter.gui.action.ActionRouter$1.run(ActionRouter.java:62)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 27 more

17楼 已删除
simple 专栏文章:[精华帖] 社区历年精华帖分类归总 中提及了此贴 12月13日 14:44
simple [精彩盘点] TesterHome 社区 2018 年 度精华帖 中提及了此贴 01月07日 12:08

Jmeter 本身的 sampler 里面支持 dubbo sample,楼主为什么没推荐使用这种方式呢,感觉这种方式不需要写代码,更方便一点~

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册