本篇文章主要讲解jmeter如何测试java请求,以项目中某个接口为例,请求数据为post,返回也为post
1:新建maven工程,pom文件为
1
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 4.0.0
4 com.ustcinfo.jmeter
5 java-post
6 0.0.1-SNAPSHOT
7 HttpRequestPost
8
9
10
11 commons-io
12 commons-io
13 2.5
14
15
16
17 org.apache.jmeter
18 ApacheJMeter_core
19 3.0
20
21
22 org.apache.jmeter
23 ApacheJMeter_java
24 3.0
25
26
27
28
29
30
31 maven-assembly-plugin
32
33 src/main/assembly/assembly.xml
34
35
36
37 make-assembly
38 package
39
40 single
41
42
43
44
45
46
47
2:新建JmeHttpRequestPost.java类
1 packagecom.ustcinfo.jmeter;2
3 importjava.io.DataOutputStream;4 importjava.io.InputStream;5 importjava.net.URL;6 importjava.net.URLConnection;7
8 importorg.apache.commons.io.IOUtils;9 importorg.apache.jmeter.config.Arguments;10 importorg.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;11 importorg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;12 importorg.apache.jmeter.samplers.SampleResult;13
14 public class JmeHttpRequestPost extendsAbstractJavaSamplerClient {15
16 private String number = "number";17 private String number_value = "18134536460";18
19
20 /*
21 * 参数化,number22 */
23 @Override24 publicArguments getDefaultParameters() {25 Arguments params = newArguments();26 params.addArgument(number, number_value);27 returnparams;28 }29
30 /*
31 * 运行方法32 */
33 publicSampleResult runTest(JavaSamplerContext arg0) {34 SampleResult sr = newSampleResult();35 try{36 /*参数获取*/
37 String number = arg0.getParameter("number");38 sr.sampleStart();39 /*请求地址*/
40 String URL = "http://ip:port/WSSPort/";41 /*请求报文*/
42 String Json = "";43 Json="{"
44 +"\"TcpCont\":{"
45 +"\"ServiceCode\":\"/ServiceBus/custView/cust/cust001\","
46 +"\"SrcSysID\":\"100000\","
47 +"\"TransactionID\":\"3b236e76-4662-4df6-8c61-46a8e3d2df56\","
48 +"\"MsgId\":\"\""
49 +"},"
50 +"\"SvcCont\":{"
51 +"\"SOO\":["
52 +"{"
53 +"\"PUB_REQ\":{"
54 +"\"PAGE_INDEX\":\"1\","
55 +"\"PAGE_SIZE\":\"5\","
56 +"\"SVC_CONT_VER\":\"1.0\","
57 +"\"TYPE\":\"QRY_CUST_INFO\""
58 +"},"
59 +"\"RETURN_OBJECTS\":{"
60 +"\"ACCOUNT\":{"
61 +"\"FIELDS\":\"\""
62 +"},"
63 +"\"CONTACT\":{"
64 +"\"FIELDS\":\"\""
65 +"},"
66 +"\"CUST\":{"
67 +"\"FIELDS\":\"\""
68 +"},"
69 +"\"CUST_BRAND_LABEL\":{"
70 +"\"FIELDS\":\"\""
71 +"},"
72 +"\"CUST_POINT\":{"
73 +"\"FIELDS\":\"\""
74 +"},"
75 +"\"MANAGER\":{"
76 +"\"FIELDS\":\"CHANNEL_MEMBER_ID,MEMBER_NBR,CHANNEL_MEMBER_NAME,PHONE\""
77 +"},"
78 +"\"PARTY_CENTIFICATION\":{"
79 +"\"FIELDS\":\"\""
80 +"},"
81 +"\"TB_PTY_CERTIF_ID_VAR_INFO\":{"
82 +"\"FIELDS\":\"\""
83 +"},"
84 +"\"CUST_SPEC_INFO\":{"
85 +"\"FIELDS\":\"\""
86 +"}"
87 +"},"
88 +"\"CUST_REQ\":{"
89 +"\"CUST_ID\":\":getCustId("+number+",'4','',551)\","
90 +"\"LATN_ID\":\"551\","
91 +"\"PASSWORD\":\"\""
92 +"}"
93 +"}"
94 +"]"
95 +"}"
96 +"}";97 /*调用post请求方法*/
98 String postResult =doHttpPost(Json, URL);99 /*设置响应报文内容*/
100 sr.setResponseData(postResult, null);101 sr.setDataType(SampleResult.TEXT);102 sr.setSuccessful(true);103 sr.sampleEnd();104 } catch(Exception e) {105 e.printStackTrace();106 }107
108 returnsr;109 }110
111 /**
112 * doHttpPost:http发送json报文形式的post请求113 *@paramxmlInfo 请求报文114 *@paramURL 请求地址115 *@returnString 响应内容116 *@exception
117 */
118 public staticString doHttpPost(String xmlInfo, String URL) {119 byte[] xmlData =xmlInfo.getBytes();120 InputStream instr = null;121 try{122 URL url = newURL(URL);123 URLConnection urlCon =url.openConnection();124 urlCon.setDoOutput(true);125 urlCon.setDoInput(true);126 urlCon.setUseCaches(false);127 urlCon.setRequestProperty("Content-Type", "text/xml");128 urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));129 DataOutputStream printout = newDataOutputStream(urlCon.getOutputStream());130 printout.write(xmlData);131 printout.flush();132 printout.close();133 instr =urlCon.getInputStream();134 byte[] bis =IOUtils.toByteArray(instr);135 String ResponseString = new String(bis, "UTF-8");136 if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {137 System.out.println("返回空");138 }139 returnResponseString;140
141 } catch(Exception e) {142 e.printStackTrace();143 return "0";144 } finally{145 try{146 instr.close();147 } catch(Exception ex) {148 return "0";149 }150 }151 }152 }153
3:在maven工程src/main下面新建assembly.xml文件,用作maven打包
1
2 assembly
3
4 tar.gz
5
6 true
7
8
9 ${project.build.directory}/dubbo/META-INF/assembly/bin
10 bin
11 0755
12
13
14 src/main/assembly/conf
15 conf
16 0644
17
18
19
20
21 lib
22
23
24
4:打包该工程,maven install,打包完成后可以看到target下生成了两个文件一个java-post-0.0.1-SNAPSHOT-assembly.tar还有一个java-post-0.0.1-SNAPSHOT.jar
将java-post-0.0.1-SNAPSHOT-assembly.tar中的lib文件夹下所有的jar包拷贝到jmeter的lib目录下,如果有重复的,则不替换用jmeter原生的jar包
将java-post-0.0.1-SNAPSHOT.jar拷贝到jmeter的lib/ext目录下
5:启动jmeter,验证该jar功能是否正常,新建一个java请求,如果有参数化可设置参数化文件
上图为两个线程测试了一下,发现调用通过成功,响应数据正常返回,故该脚本可以正常使用
至于如何用这个jmx脚本做性能测试,我就不在重复了,参考我的博文-jmeter命令行运行-单节点测试或者分布式测试