Jmeter's java request for redevelopment: testing the dubbo interface

From: https://blog.csdn.net/qq_34021712/article/details/78956600

 

Source download: Click here to download

Note: Testing the dubbo interface with JMeter is similar to testing java requests with jmeter, refer to Basic Knowledge.

[Fundamentals] jmeter tests the java interface (secondary development): http://blog.csdn.net/qq_34021712/article/details/78870407

 

1. Create a Maven Project
pom.xml

<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.test.jmeter</groupId>
  <artifactId>TestDubbo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
 
    <!--Unified Management of Project Dependent Version Numbers-->
    <properties>
        <!--dubbo rest-->
        <dubbo.version>2.8.4</dubbo.version>
        <!--spring-->
        <spring.version>4.3.10.RELEASE</spring.version>
        <!--zookeeper-->
        <zookeeper.version>3.4.6</zookeeper.version>
        <zkclient.version>0.10</zkclient.version>
        <!-- Ali FastJson-->
        <fastjson.version>1.2.36</fastjson.version>
    </properties>
 
 
    <dependencies>
 
 
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>
 
 
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
 
 
        <!-- zookeeper Registration Center -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
 
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_core</artifactId>
            <version>3.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-math3</groupId>
                    <artifactId>commons-math3</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-pool2</groupId>
                    <artifactId>commons-pool2</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
 
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>3.3</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-math3</groupId>
                    <artifactId>commons-math3</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-pool2</groupId>
                    <artifactId>commons-pool2</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
 
        <!-- integration dubbox -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.curator</groupId>
                    <artifactId>curator-framework</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.curator</groupId>
                    <artifactId>curator-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.validation</groupId>
                    <artifactId>validation-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
 
        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
 
 
        <!-- Introduce to be tested dubbo Service PAI-->
        <dependency>
            <groupId>com.test.dubbo</groupId>
            <artifactId>MISS.UserCenter.API</artifactId>
            <version>1.0-RELEASE</version>
        </dependency>
 
 
        <!--spring core pom rely on-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <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-framework-bom</artifactId>
            <version>${spring.version}</version>
            <type>pom</type>
        </dependency>
        <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-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring AOP -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
 
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
    </dependencies>
 
 
    <build>
        <!--Packaged project name  -->
        <finalName>TestDubbo</finalName>
        <plugins>
            <!-- java Compile Plugins -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- hit jar Plug-ins for packages -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib</classpathPrefix>
                            <!-- Program Startup Entry -->
                            <mainClass>com.test.jmeter.Application</mainClass>
                        </manifest>
                        <manifestEntries>
                            <!-- take lib Pull packages into a folder above, classpathPrefix Property is package name-->
                            <Class-Path>./</Class-Path>
                        </manifestEntries>
                    </archive>
                    <excludes>
                        <!-- take config/**Pull out -->
                        <exclude>config/**</exclude>
                        <exclude>spring/**</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <!-- not append assembly id in release file name -->
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <!-- Notice the path here -->
                        <descriptor>src/main/build/package.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The above pom file configurations are basic configurations, and you need to add your own dependent jar packages for the interfaces you want to test.

dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
    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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://code.alibabatech.com/schema/dubbo          
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
 
    <!-- Load Profile -->
    <context:property-placeholder location="classpath:config/*.properties" />
 
 
    <dubbo:application name="${dubbo.application.name}"/>
 
 
    <!-- Use zookeeper Registry Exposure Service Address,Use multiple registries -->
    <dubbo:registry protocol="zookeeper" address="${dubbo.zookeeper.address}" />
 
 
    <!-- Configure Global Timeout -->
    <dubbo:consumer timeout="${dubbo.consumer.timeout}"/>
 
 
    <!-- To test dubbo Interface -->
    <dubbo:reference interface="com.service.UserInfoCenterService"
                     id="userInfoCenterService"
                     protocol="dubbo"
                     version="1.0"
                     check="false"/>
</beans>

application.properties

#zk Configuration Center
dubbo.zookeeper.address=127.0.0.1:2181
dubbo.application.name=TestDubbo
dubbo.consumer.timeout=6000

After configuring the above, you start writing code that inherits Jmeter's AbstractJavaSamplerClient class and implements the runTest method

DubboTest.java

package com.test.jmeter;
 
 
import com.ruubypay.miss.returntype.ModelsReturn;
import com.ruubypay.miss.usercenter.interfaces.models.ModelsGetBindingUserRealNameInfoRequest;
import com.ruubypay.miss.usercenter.interfaces.models.ModelsGetBindingUserRealNameInfoResponse;
import com.ruubypay.miss.usercenter.interfaces.service.UserInfoCenterService;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
/**
 * @author: wangsaichao
 * @date: 2017/12/22
 * @description:
 */
public class DubboTest extends AbstractJavaSamplerClient {
 
 
    private static final Logger logger = LoggerFactory.getLogger(DubboTest.class);
 
 
    private static final ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/*.xml");
 
 
    private static UserInfoCenterService userInfoCenterService;
 
 
 
 
    /**
     * This method is used to customize java method input
     * params.addArgument("num1","");Indicates that the name of the entry is num1 and the default value is empty.
     * @return
     */
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        //id
        params.addArgument("userID", "");
        //Cell-phone number
        params.addArgument("phoneNum", "");
 
 
        return params;
    }
 
 
    /**
     * Execute once per thread before testing to do some initialization
     * Gets the input parameter and assigns it to a variable
     * @param arg0
     */
    @Override
    public void setupTest(JavaSamplerContext arg0) {
        userInfoCenterService = (UserInfoCenterService)context.getBean("userInfoCenterService");
    }
 
 
    /**
     * Ways to actually execute logic
     * @param javaSamplerContext
     * @return
     */
    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
 
 
        SampleResult sr = new SampleResult();
 
 
        //Get previous request parameters
        String userID = javaSamplerContext.getParameter("userID");
 
 
        String phoneNum = javaSamplerContext.getParameter("phoneNum");
 
 
 
 
        sr.setSamplerData("Request parameters userID: "+userID+"\n Request parameters phoneNum: "+phoneNum);
 
 
        try {
            // jmeter Start Statistical Response Time Marker
            sr.sampleStart();
 
 
            //This class is a required parameter for the dubbo interface
            ModelsGetBindingUserRealNameInfoRequest modelsGetBindingUserRealNameInfoRequest = new ModelsGetBindingUserRealNameInfoRequest();
            modelsGetBindingUserRealNameInfoRequest.setUserID(userID);
            modelsGetBindingUserRealNameInfoRequest.setPhoneNum(phoneNum);
 
 
            ModelsReturn<ModelsGetBindingUserRealNameInfoResponse> bindingUserRealNameInfo = userInfoCenterService.getBindingUserRealNameInfo(modelsGetBindingUserRealNameInfoRequest);
 
 
 
 
            String json = FastJsonUtil.beanToJson(bindingUserRealNameInfo);
            logger.info(json);
            sr.setResponseData(json,null);
            sr.setDataType(SampleResult.TEXT);
 
 
            //Return correct
            sr.setSuccessful(true);
 
 
        }  catch (Throwable e) {
            sr.setSuccessful(false);
            e.printStackTrace();
        } finally {
            // jmeter End Statistical Response Time Marker
            sr.sampleEnd();
        }
        return sr;
 
 
    }
 
 
 
 
    /**
     * Called after the test ends
     * @param arg0
     */
    @Override
    public void teardownTest(JavaSamplerContext arg0) {
        logger.info("End of method call");
    }
 
 
}

Debugging Code

Use the main method to test before packaging into jmeter

Application.java

package com.test.jmeter;
 
 
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
 
 
/**
 * @author: wangsaichao
 * @date: 2017/12/22
 * @description:
 */
public class Application {
 
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        Arguments params = new Arguments();
        //Setting parameters
        params.addArgument("userID", "");
        //Setting parameters
        params.addArgument("phoneNum", "18555555555");
        JavaSamplerContext arg0 = new JavaSamplerContext(params);
        DubboTest test = new DubboTest();
        test.setupTest(arg0);
        test.runTest(arg0);
        test.teardownTest(arg0);
    }
 
 
}

The following exception was reported:
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 41 more
Solution: The pom.xml above has been added

<dependency>  
    <groupId>xml-apis</groupId>  
    <artifactId>xml-apis</artifactId>  
    <version>1.4.01</version>  
</dependency>

Repeat with the following exception
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
Solution: Add log4j2.xml under classpath for content reference: http://blog.csdn.net/qq_34021712/article/details/78870407

 

Using jmeter test

After debugging, we type the code into a jra package and execute the maven clean package, there are two things below the targer that we need:
TestDubbo.zip and TestDubbo.jar TestDubbo.zip also contains TestDubbo.jar and Lib files, lib contains the jar packages we need

Unzip TestDubbo.zip

Copy the jar file of TestDubbo.jar into jmeter's lib/ext
Then rename the Lib package unzipped from TestDubbo.zip to lib-dependency and copy it to jmeter's lib (the lib-dependency name is whatever you want)
Modify search_paths in jmeter.properties to configure the paths of dependent jar s


 

Start jmeter test

Tags: Spring Dubbo Java Apache

Posted on Mon, 02 Dec 2019 19:27:56 -0800 by Andrew B