首页 > 代码库 > jmeter java性能测试
jmeter java性能测试
本篇文章主要讲解jmeter如何测试java请求,以项目中某个接口为例,请求数据为post,返回也为post
1:新建maven工程,pom文件为
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.ustcinfo.jmeter</groupId> 5 <artifactId>java-post</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <name>HttpRequestPost</name> 8 <dependencies> 9 <!-- httppost请求依赖jar包 --> 10 <dependency> 11 <groupId>commons-io</groupId> 12 <artifactId>commons-io</artifactId> 13 <version>2.5</version> 14 </dependency> 15 <!-- java jmeter依赖jar包 --> 16 <dependency> 17 <groupId>org.apache.jmeter</groupId> 18 <artifactId>ApacheJMeter_core</artifactId> 19 <version>3.0</version> 20 </dependency> 21 <dependency> 22 <groupId>org.apache.jmeter</groupId> 23 <artifactId>ApacheJMeter_java</artifactId> 24 <version>3.0</version> 25 </dependency> 26 </dependencies> 27 28 <build> 29 <plugins> 30 <plugin> 31 <artifactId>maven-assembly-plugin</artifactId> 32 <configuration> 33 <descriptor>src/main/assembly/assembly.xml</descriptor> 34 </configuration> 35 <executions> 36 <execution> 37 <id>make-assembly</id> 38 <phase>package</phase> 39 <goals> 40 <goal>single</goal> 41 </goals> 42 </execution> 43 </executions> 44 </plugin> 45 </plugins> 46 </build> 47 </project>
2:新建JmeHttpRequestPost.java类
1 package com.ustcinfo.jmeter; 2 3 import java.io.DataOutputStream; 4 import java.io.InputStream; 5 import java.net.URL; 6 import java.net.URLConnection; 7 8 import org.apache.commons.io.IOUtils; 9 import org.apache.jmeter.config.Arguments; 10 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 11 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 12 import org.apache.jmeter.samplers.SampleResult; 13 14 public class JmeHttpRequestPost extends AbstractJavaSamplerClient { 15 16 private String number = "number"; 17 private String number_value = "http://www.mamicode.com/18134536460"; 18 19 20 /* 21 * 参数化,number 22 */ 23 @Override 24 public Arguments getDefaultParameters() { 25 Arguments params = new Arguments(); 26 params.addArgument(number, number_value); 27 return params; 28 } 29 30 /* 31 * 运行方法 32 */ 33 public SampleResult runTest(JavaSamplerContext arg0) { 34 SampleResult sr = new SampleResult(); 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 return sr; 109 } 110 111 /** 112 * doHttpPost:http发送json报文形式的post请求 113 * @param xmlInfo 请求报文 114 * @param URL 请求地址 115 * @return String 响应内容 116 * @exception 117 */ 118 public static String doHttpPost(String xmlInfo, String URL) { 119 byte[] xmlData =http://www.mamicode.com/ xmlInfo.getBytes(); 120 InputStream instr = null; 121 try { 122 URL url = new URL(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 = new DataOutputStream(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 return ResponseString; 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 <assembly> 2 <id>assembly</id> 3 <formats> 4 <format>tar.gz</format> 5 </formats> 6 <includeBaseDirectory>true</includeBaseDirectory> 7 <fileSets> 8 <fileSet> 9 <directory>${project.build.directory}/dubbo/META-INF/assembly/bin</directory> 10 <outputDirectory>bin</outputDirectory> 11 <fileMode>0755</fileMode> 12 </fileSet> 13 <fileSet> 14 <directory>src/main/assembly/conf</directory> 15 <outputDirectory>conf</outputDirectory> 16 <fileMode>0644</fileMode> 17 </fileSet> 18 </fileSets> 19 <dependencySets> 20 <dependencySet> 21 <outputDirectory>lib</outputDirectory> 22 </dependencySet> 23 </dependencySets> 24 </assembly>
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命令行运行-单节点测试或者分布式测试
jmeter java性能测试
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。