Jmeter学习之-dubbo接口测试
背景:公司的h5和APP都需要调用许多非http的服务,需要对服务的性能和自动化测试
工具:IDEA ,maven,Jmeter
参考文档:
- https://testerhome.com/topics/10474
- https://testerhome.com/topics/9980
- https://blog.csdn.net/qq_34021712/article/details/78956600
第一步:创建一个maven项目,可自己百度,不多做描述,创建完成大概结构如下
第二步:配置相关文件
- 配置pom文件(配置完成后reimport即可)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>JmeterDemo</groupId> 8 <artifactId>JmeterDemo</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <spring.version>3.2.4.RELEASE</spring.version> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 </properties> 15 16 <!--配置镜像,加快下载jar包的速度(这里配置公司的私服,根据公司不同配置)--> 17 <repositories> 18 <repository> 19 <id>public</id> 20 <name>Public Repositories</name> 21 <url> 22 http://nexus.guahao-inc.com/nexus/content/groups/public 23 </url> 24 </repository> 25 </repositories> 26 <pluginRepositories> 27 <pluginRepository> 28 <id>public</id> 29 <name>Public Repositories</name> 30 <url> 31 http://nexus.guahao-inc.com/nexus/content/groups/public 32 </url> 33 </pluginRepository> 34 </pluginRepositories> 35 <!--配置依赖包--> 36 <dependencies> 37 38 <!--dubbo依赖包--> 39 <dependency> 40 <groupId>com.alibaba</groupId> 41 <artifactId>dubbo</artifactId> 42 <version>2.5.3</version> 43 <exclusions> 44 <exclusion> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring</artifactId> 47 </exclusion> 48 </exclusions> 49 </dependency> 50 51 <!--Jmeter需要的jar包,!!!注意,这里的版本号需要和使用的Jmeter的版本号一致,否则到jmeter上会报各种错--> 52 <dependency> 53 <groupId>org.apache.jmeter</groupId> 54 <artifactId>ApacheJMeter_core</artifactId> 55 <version>5.0</version> 56 </dependency> 57 58 <dependency> 59 <groupId>org.apache.jmeter</groupId> 60 <artifactId>ApacheJMeter_java</artifactId> 61 <version>5.0</version> 62 </dependency> 63 64 <!--spring核心pom依赖--> 65 <dependency> 66 <groupId>org.springframework</groupId> 67 <artifactId>spring-beans</artifactId> 68 <version>${spring.version}</version> 69 </dependency> 70 <dependency> 71 <groupId>org.springframework</groupId> 72 <artifactId>spring-expression</artifactId> 73 <version>${spring.version}</version> 74 </dependency> 75 <dependency> 76 <groupId>org.springframework</groupId> 77 <artifactId>spring-core</artifactId> 78 <version>${spring.version}</version> 79 </dependency> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-web</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-context</artifactId> 88 <version>${spring.version}</version> 89 </dependency> 90 <dependency> 91 <groupId>org.springframework</groupId> 92 <artifactId>spring-context-support</artifactId> 93 <version>${spring.version}</version> 94 </dependency> 95 <dependency> 96 <groupId>org.springframework</groupId> 97 <artifactId>spring-orm</artifactId> 98 <version>${spring.version}</version> 99 </dependency> 100 <!-- Spring AOP --> 101 <dependency> 102 <groupId>org.springframework</groupId> 103 <artifactId>spring-aop</artifactId> 104 <version>${spring.version}</version> 105 </dependency> 106 107 <!-- consult-service依赖包,即要测试的接口依赖--> 108 <dependency> 109 <groupId>com.greenline.consult</groupId> 110 <artifactId>greenline-consult-service-share</artifactId> 111 <version>2.3.94-SNAPSHOT</version> 112 </dependency> 113 </dependencies> 114 <!--打包需要的--> 115 <build> 116 <plugins> 117 <!--复制jar包插件,将使用到的jar包,复制到target/lib中--> 118 <plugin> 119 <groupId>org.apache.maven.plugins</groupId> 120 <artifactId>maven-dependency-plugin</artifactId> 121 <executions> 122 <execution> 123 <id>copy-dependencies</id> 124 <phase>prepare-package</phase> 125 <goals> 126 <goal>copy-dependencies</goal> 127 </goals> 128 <configuration> 129 <outputDirectory>${project.build.directory}/lib</outputDirectory> 130 <overWriteReleases>false</overWriteReleases> 131 <overWriteSnapshots>false</overWriteSnapshots> 132 <overWriteIfNewer>true</overWriteIfNewer> 133 </configuration> 134 </execution> 135 </executions> 136 </plugin> 137 138 <plugin> 139 <groupId>org.codehaus.mojo</groupId> 140 <artifactId>build-helper-maven-plugin</artifactId> 141 <version>1.8</version> 142 <executions> 143 <execution> 144 <id>add-resource</id> 145 <phase>generate-resources</phase> 146 <goals> 147 <goal>add-resource</goal> 148 </goals> 149 <configuration> 150 <resources> 151 <resource> 152 <directory>src/main/resources</directory> 153 <includes> 154 <include>*</include> 155 </includes> 156 </resource> 157 </resources> 158 </configuration> 159 </execution> 160 </executions> 161 </plugin> 162 163 <plugin> 164 <artifactId>maven-assembly-plugin</artifactId> 165 <version>2.4</version> 166 <configuration> 167 <descriptorRefs> 168 <descriptorRef>jar-with-dependencies</descriptorRef> 169 </descriptorRefs> 170 <archive> 171 <manifest> 172 <mainClass>TestMain.Main</mainClass> 173 </manifest> 174 </archive> 175 </configuration> 176 <executions> 177 <execution> 178 <id>make-assembly</id> 179 <phase>package</phase> 180 <goals> 181 <goal>single</goal> 182 </goals> 183 </execution> 184 </executions> 185 </plugin> 186 </plugins> 187 </build> 188 </project>
- 配置dubbo-config.xml文件(在resources文件夹下新建一个文件dubbo-config.xml,配置内容如下)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 http://code.alibabatech.com/schema/dubbo 10 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 11 12 <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> 13 <dubbo:application name="consult-service" /> 14 15 <!-- 测试的dubbo服务的信息--> 16 <dubbo:reference id="consultOrderService" 17 interface="com.greenline.consult.hessian.share.consultorder.service.ConsultOrderService" 18 timeout="650000" url="dubbo://192.168.1.103:11006/consultOrder" /> 19 </beans>
这里的http://code.alibabatech.com/schema/dubbo/dubbo.xsd" 已经停止服务了,会导致文件不能读取,需要从网上下载该文件,或者从dubbo-2.5.3.jar META-INF 目录下导出,然后设置一下引用本地资源,参考https://blog.csdn.net/qq_36654870/article/details/80603302dubbo.xsd
第三步:开始写代码,需要继承Jmeter的AbstractJavaSamplerClient 类,并实现runTest方法
TestQueryConsultOrderReplyList
1 import com.greenline.consult.hessian.share.consultorder.request.ConsultOrderReplyPageListReq; 2 import com.greenline.consult.hessian.share.consultorder.response.ConsultOrderReplyListResult; 3 import com.greenline.consult.hessian.share.consultorder.service.ConsultOrderService; 4 import org.apache.jmeter.config.Arguments; 5 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 6 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 7 import org.apache.jmeter.samplers.SampleResult; 8 import org.slf4j.Logger; 9 import org.slf4j.LoggerFactory; 10 import org.springframework.context.ApplicationContext; 11 import org.springframework.context.support.ClassPathXmlApplicationContext; 12 13 public class TestQueryConsultOrderReplyList extends AbstractJavaSamplerClient{ 14 15 private static final ApplicationContext context = new ClassPathXmlApplicationContext("dubbo-config.xml"); 16 private static ConsultOrderService consultOrderService; 17 private static final Logger logger = LoggerFactory.getLogger(TestQueryConsultOrderReplyList.class); 18 19 @Override 20 public Arguments getDefaultParameters() { 21 Arguments params = new Arguments(); 22 params.addArgument("orderKey", ""); 23 return params; 24 } 25 26 /** 27 * 每个线程测试前执行一次,做一些初始化工作 28 * 获取输入的参数,赋值给变量 29 * 30 * @param arg0 31 */ 32 @Override 33 public void setupTest(JavaSamplerContext arg0) { 34 consultOrderService = (ConsultOrderService) context.getBean("consultOrderService"); 35 } 36 37 public SampleResult runTest(JavaSamplerContext javaSamplerContext) { 38 SampleResult sr = new SampleResult(); 39 /*获取之前的请求参数*/ 40 String orderKey = javaSamplerContext.getParameter("orderKey"); 41 sr.setSamplerData("请求参数orderKey:" + orderKey); 42 43 try { 44 // jmeter 开始统计响应时间标记 45 sr.sampleStart(); 46 47 // 该类是dubbo接口需要的参数 48 ConsultOrderReplyPageListReq consultOrderReplyReq = new ConsultOrderReplyPageListReq(); 49 consultOrderReplyReq.setOrderKey(orderKey); 50 51 //该类是dubbo接口的返回 52 ConsultOrderReplyListResult response = consultOrderService.queryConsultOrderReplyList(consultOrderReplyReq); 53 System.out.println("响应结果: " + response); 54 55 if (response != null && response.getResultCode().equals("0")) { 56 // 返回正确 57 sr.setSuccessful(true); 58 sr.setResponseData("code : " + response.getResultCode() + "message: " + response.getResultDesc(), "utf-8"); 59 } else { 60 sr.setSuccessful(false); 61 } 62 // jmeter 结束统计响应时间标记 63 sr.sampleEnd(); 64 65 } catch (Exception e) { 66 e.printStackTrace(); 67 } 68 return sr; 69 } 70 71 public void teardownTest(JavaSamplerContext arg0) { 72 logger.info("方法调用结束"); 73 } 74 }
使用main方法调试,调试成功后再放入Jmeter中运行
1 import org.apache.jmeter.config.Arguments; 2 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 3 4 public class TestMain { 5 public static void main(String[] args) { 6 Arguments params = new Arguments(); 7 // 设置参数 8 params.addArgument("orderKey", "p90xb3z3gy181107090819856"); 9 JavaSamplerContext arg0 = new JavaSamplerContext(params); 10 TestQueryConsultOrderReplyList test = new TestQueryConsultOrderReplyList(); 11 test.setupTest(arg0); 12 test.runTest(arg0); 13 test.teardownTest(arg0); 14 } 15 }
调试过程遇到的报错及解决方案:
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
解决方法:在classpath下(即resources)添加log4j2.xml文件,内容如下,添加log4j2.xml 文件后运行成功
第四步:打jar包,然后在Jmeter下运行 ,
- 打jar包
- 打包完成在target目录下生成两个jar文件,将后缀为-jar-with-dependencies.jar的文件(即下图所示文件)复制到jmeter的 lib/ext路径下
- 运行Jmeter,输出接口的参数,即可运行