背景:公司的h5和APP都須要調用許多非http的服務,須要對服務的性能和自動化測試html
工具:IDEA ,maven,Jmeterjava
參考文檔:web
第一步:建立一個maven項目,可本身百度,很少作描述,建立完成大概結構以下
第二步:配置相關文件spring
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>
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.xsdexpress
第三步:開始寫代碼,須要繼承Jmeter的AbstractJavaSamplerClient 類,並實現runTest方法apache
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 }
調試過程遇到的報錯及解決方案:app
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 maven
解決方法:在classpath下(即resources)添加log4j2.xml文件,內容以下,添加log4j2.xml 文件後運行成功ide
第四步:打jar包,而後在Jmeter下運行 ,工具