Jmeter test Dubbo interface
Preface
This article introduces two ways to use jmeter to test dubbo interface
- Call through the way of jmeter secondary development (jar) (recommended, with requirements for coding ability)
- Call through JMeter plugins Dubbo
Call through jmeter secondary development (jar)
Millet: testservice addedit() method of target service
Resource allocation
//Introduce related resources under project lib file 1. The dependent resources required by TestService: TestService.jar 2.Jmeter dependent jars: jorphan.jar, ApacheJMeter_java.jar, ApacheJMeter_core.jar 3.dubbo resource: dubbo.xsd //Resource path //1.jorphan.jar (jmeter/lib for main debugging execution) // 2.ApacheJMeter_java.jar,ApacheJMeter_core.jar (jmeter/lib/ext/) //3.dubbo.xsd can download Dubbo resources directly through pom, and copy a dubbo.xsd from Dubbo corresponding to Maven warehouse
- Create a new maven project and add the following dependencies in pom.xml configuration: Dubbo, Spring
- You also need to add the dependent test service corresponding to the test service
<?xml version="1.0" encoding="UTF-8"?> <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.hank</groupId> <artifactId>dubbo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-client</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!-- spring test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- Test Service --> <dependency> <groupId>com.user.test.service</groupId> <artifactId>TestService</artifactId> <!--<version>2.0.0</version>--> </dependency> </dependencies> </project>
- spring configuration of Dubbo service consumers: create consumer.xml configuration under the resources directory (consumers)
For the configuration content of consumer.xml, please refer to the consumer configuration file of TestService (you can find the corresponding consumer profile from the deployment path of the corresponding service in the test environment, such as dubbo_client.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"> <description>Dubbo Consumer To configure</description> <!-- id The name of the interface can be used by@Resource Annotation instantiation. interface Is the full path to the class of the referenced interface. registry For the address of the warehouse id check by spring Check service availability at initialization --> <dubbo:application name="Test-service" owner="hank"/> <dubbo:consumer timeout="5000" check="false" lazy="true" retries="0"/> <dubbo:registry id="regTest" address="zookeeper://192.168.1.60:2020"/> <dubbo:reference id="TestService" interface="com.user.test.service.TestService" registry="regTest" check="false" version="2.0.0"/> </beans>
- Test class writing
package com.hank.api; import com.test.TestServicePlayCriteria; import com.test.service.TestService; import com.test.params.Empty; import com.test.params.Result; 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; /** * Description * * @Author Hank * @Date Created in 2019-12-30 15:53 * Jmeter The statistics time needs to be added, otherwise, when calling from jmeter, the response time that does not correspond to the request cannot be counted */ public class ApiDubbo extends AbstractJavaSamplerClient{ private static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-dubbo-consumer.xml"); private static TestService testService; private static long start = 0; private static long end = 0; public void setupTest(JavaSamplerContext arg0){ testService=(TestService)context.getBean("testService"); start = System.currentTimeMillis(); } public SampleResult runTest(JavaSamplerContext javaSamplerContext) { SampleResult sr = new SampleResult(); sr.setSamplerData("Dubbo Api test"); sr.sampleStart();// jmeter starts counting response time tags long userId = 505009; TestServicePlayCriteria testServicePlayCriteria = new TestServicePlayCriteria(); testServicePlayCriteria.setDrugName("test_dubbo"); testServicePlayCriteria.setDosage("50"); testServicePlayCriteria.setEffect("test"); testServicePlayCriteria.setUserId(userId); Result<Empty> result = testService.addOrEdit(TestServicePlayCriteria); if(result.getCode().equals("00000")){ // Interface call result judgment sr.setDataType(SampleResult.TEXT); sr.setSuccessful(true); //Setting jmeter return status succeeded: true }else{ sr.setSuccessful(false); //Failed to set jmeter return status: false } sr.setSamplerData(result.getCode()); // Set jmeter request to return display content sr.sampleEnd(); return sr; } public void teardownTest(JavaSamplerContext arg0) { end = System.currentTimeMillis(); // Overall time consuming System.err.println("cost time:" + (end - start) + "Millisecond"); } }
- Local debugging
package com.hank.api; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; /** * Description * * @Author Hank * @Date Created in 2019-12-31 17:18 */ public class TestMain { private static JavaSamplerContext ar = new JavaSamplerContext(new Arguments()); public static void main(String args[]){ testApiDubbo(); } public static void testApiDubbo(){ ApiDubbo apiDubbo = new ApiDubbo(); apiDubbo.setupTest(ar); apiDubbo.runTest(ar); } }
- Local debugging results
- Package jar (after local debugging is ok, type jar and put it in jmeter/lib/ext /)
//Packing process // The jar storage path is under the default current project out / artifact / 1.File -> Project Structure -> Artifats -> add Library file 2.Build -> Build Artifats -> Builds
- jmeter add the jar package you typed before and restart jmeter
- Call result
The above is called through the way of jmeter secondary development (jar)
Called through JMeter plugins Dubbo
// Jmeter itself does not support Dubbo interface calls. You need to download a third-party plug-in to support // Download jmeter-plugins-dubbo-1.3.8-jar-with-dependencies.jar place jmeter/lib/ext / restart JMeter // Additional dependent resources need to be added: dubbo-2.5.3.jar javassist-3.15.0-GA.jar zookeeper-3.4.6.jar zkclient-0.1.jar jline-0.9.94.jar netty-3.7.0-Final.jar slf4j-api-1.7.5.jar log4j-over-slf4j-1.7.5.jar // Matters needing attention 1.When used zk,address Fill zk Address (used by cluster address","Separation),Use dubbo Direct connection, address Fill in direct connection address and service port 2.timeout: Service method call timeout(Millisecond) 3.version: Service version, consistent with the service provider's version 4.retries: Retry times of remote service call, excluding the first call, please set to0 5.cluster: Cluster mode, optional: failover/failfast/failsafe/failback/forking 6.group: Service grouping. When an interface has multiple implementations, it can be distinguished by grouping. It must be consistent with the service provider 7.Interface needs to fill in full type name, including package name 8.Parameter supports any type. The wrapper class can directly use java.lang The following packaging classes are used for small types:int,float,shot,double,long,byte,boolean,char,The custom class uses the class full name. 9.Parameter value, basic packing class and basic subtype directly use value, for example:intby1,booleanbytrueClass and List perhaps Map Such use json Format data.
- Jmerter operation interface