├── README.md └── src └── jmeter └── jmx ├── jmeter.java ├── pojo └── summaryPojo.java └── util ├── execLinux.java ├── parseReport.java └── parseXML.java /README.md: -------------------------------------------------------------------------------- 1 | # JMeter的自动化压测工具 2 | 3 | Jmeter是一个优秀的基于java开发的测试工具,主要用来测试web应用的性能。 4 | 5 | 具体可以参考: 6 | http://www.i3geek.com/archives/1147 7 | 8 | ## 用法 9 | 10 | 1. 利用JMeter录制测试脚本,并设定参数 11 | 2. 生成测试脚本jmx 12 | 3. 在java程序中修改线程数,并引用脚本 13 | 4. 自动化运行,并收集结果(如时间、吞吐量等) 14 | 15 | ## 作用 16 | 17 | 通过自动化脚本,可以方便的画出web应用的并发数与吞吐率的函数关系,制定应用的性能曲线 18 | 19 | ## 源码说明 20 | 21 | jmeter.jmx 22 | |-pojo 23 | | |-summaryPojo.java 24 | | jmeter中汇聚报告的pojo 25 | |-util 26 | | |-execLinux.java 27 | | | linux环境下,脚本的运行辅助类 28 | | |-parseReport.java 29 | | | 解析抓取的Report成pojo的辅助类 30 | | |-parseXML.java 31 | | 解析返回结果的XML的辅助类 32 | |-jmeter.java 33 | 主类,测试运行类 -------------------------------------------------------------------------------- /src/jmeter/jmx/jmeter.java: -------------------------------------------------------------------------------- 1 | package jmeter.jmx; 2 | 3 | import org.omg.CORBA.PUBLIC_MEMBER; 4 | 5 | import jmeter.jmx.pojo.summaryPojo; 6 | import jmeter.jmx.util.execLinux; 7 | import jmeter.jmx.util.parseReport; 8 | 9 | public class jmeter { 10 | public static void main(String args[]) 11 | {//16:46 12 | summaryPojo[] summarys = new summaryPojo[51]; 13 | final String ipadress = "192.168.191.1"; 14 | for(int i=1;i<=1;i+=50) 15 | { 16 | int count = i; 17 | String cmd = "cd /usr/local/apache-jmeter-2.13/bin/ ;sh jmeter -n -t /Users/yangengzhe/Documents/云平台/物流_游客查询.jmx -Jthread="+count+" -Jipadress="+ipadress; 18 | summarys[1] = parseReport.parsePojo(execLinux.exec(cmd).toString()); 19 | System.out.println("当"+count+"个用户并发时,吞吐率="+summarys[1].getThroughput()); 20 | } 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/jmeter/jmx/pojo/summaryPojo.java: -------------------------------------------------------------------------------- 1 | package jmeter.jmx.pojo; 2 | 3 | public class summaryPojo { 4 | private int requests;//请求个数 5 | private long times;//时间,second 6 | private double throughput;//吞吐率,request/sec 7 | private int avg;//ms 8 | private int min;//ms 9 | private int max;//ms 10 | private float error;//percent 11 | 12 | public int getRequests() { 13 | return requests; 14 | } 15 | 16 | public void setRequests(int requests) { 17 | this.requests = requests; 18 | } 19 | 20 | public long getTimes() { 21 | return times; 22 | } 23 | 24 | public void setTimes(long times) { 25 | this.times = times; 26 | } 27 | 28 | public double getThroughput() { 29 | return throughput; 30 | } 31 | 32 | public void setThroughput(double throughput) { 33 | this.throughput = throughput; 34 | } 35 | 36 | public int getAvg() { 37 | return avg; 38 | } 39 | 40 | public void setAvg(int avg) { 41 | this.avg = avg; 42 | } 43 | 44 | public int getMin() { 45 | return min; 46 | } 47 | 48 | public void setMin(int min) { 49 | this.min = min; 50 | } 51 | 52 | public int getMax() { 53 | return max; 54 | } 55 | 56 | public void setMax(int max) { 57 | this.max = max; 58 | } 59 | 60 | public float getError() { 61 | return error; 62 | } 63 | 64 | public void setError(float error) { 65 | this.error = error; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/jmeter/jmx/util/execLinux.java: -------------------------------------------------------------------------------- 1 | package jmeter.jmx.util; 2 | 3 | import java.io.InputStreamReader; 4 | import java.io.LineNumberReader; 5 | 6 | public class execLinux { 7 | //http://blog.csdn.net/xh16319/article/details/17302947 8 | public static Object exec(String cmd) { 9 | try { 10 | String[] cmdA = { "/bin/sh", "-c", cmd }; 11 | Process process = Runtime.getRuntime().exec(cmdA); 12 | LineNumberReader br = new LineNumberReader(new InputStreamReader( 13 | process.getInputStream())); 14 | String result = ""; 15 | String line; 16 | while ((line = br.readLine()) != null) { 17 | // System.out.println(line); 18 | if(parseReport.isSummary(line))//判断是不是Summary 19 | result = line; 20 | } 21 | return result; 22 | } catch (Exception e) { 23 | e.printStackTrace(); 24 | } 25 | return null; 26 | } 27 | 28 | public static void main(String[] args) { 29 | String pwdString = exec("cd /usr/local/apache-jmeter-2.13/bin/ ;sh jmeter -n -t /Users/yangengzhe/Documents/云平台/物流_游客查询.jmx").toString(); 30 | System.out.println(pwdString); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/jmeter/jmx/util/parseReport.java: -------------------------------------------------------------------------------- 1 | package jmeter.jmx.util; 2 | 3 | import jmeter.jmx.pojo.summaryPojo; 4 | 5 | public class parseReport { 6 | public static final int[] time_sec = {1,60,3600}; 7 | public static boolean isSummary(String line){ 8 | return line.contains("summary ="); 9 | } 10 | public static double convertSec(String src){ 11 | String[] con = src.split("\\/"); 12 | double result = Double.parseDouble(con[0]); 13 | switch (con[1]) { 14 | case "s": 15 | break; 16 | case "min": 17 | result = result/60d; 18 | break; 19 | case "hour": 20 | result = result/3600d; 21 | break; 22 | default: 23 | break; 24 | } 25 | return result; 26 | } 27 | //时间转换成秒 28 | //时间格式不对,3.4min 29 | public static long time_convertSec(String src){ 30 | src = src.replaceAll("\\s+", "").replaceAll("[a-zA-Z]+", "|"); 31 | String[] con = src.split("\\|"); 32 | long result = 0; 33 | // for(int i=con.length-1;i>=0;i--) 34 | // result += Long.parseLong(con[i])*time_sec[con.length-i-1]; 35 | return result; 36 | } 37 | public static summaryPojo parsePojo(String line){ 38 | if(line == null || line.equals("")) 39 | return null; 40 | line = line.replaceFirst("[^0-9]*", "").replaceFirst("\\%\\)$", "").replaceAll(" [^0-9]*", "|"); 41 | String[] result = line.split("\\|");//summary = 560 in 9s = 62.7/s Avg: 141 Min: 3 Max: 2369 Err: 0 (0.00%) 42 | summaryPojo summary = new summaryPojo(); 43 | summary.setRequests(Integer.parseInt(result[0])); 44 | summary.setTimes(time_convertSec(result[1])); 45 | summary.setThroughput(convertSec(result[2])); 46 | summary.setAvg(Integer.parseInt(result[3])); 47 | summary.setMin(Integer.parseInt(result[4])); 48 | summary.setMax(Integer.parseInt(result[5])); 49 | summary.setError(Float.parseFloat(result[7])); 50 | return summary; 51 | } 52 | public static void main (String args[]) 53 | { 54 | // System.out.println(isSummary("summary = 560 in 9s = 62.7/s Avg: 141 Min: 3 Max: 2369 Err: 0 (0.00%)")); 55 | // parsePojo("summary = 560 in 9s = 62.7/s Avg: 141 Min: 3 Max: 2369 Err: 0 (0.00%)"); 56 | // System.out.println(convertSec("3600/hour")); 57 | // System.out.println(time_convertSec("1hour 1min 9s")); 58 | summaryPojo a = parsePojo("summary = 560 in 9s = 62.7/min Avg: 141 Min: 3 Max: 2369 Err: 0 (0.00%)"); 59 | System.out.println(a.getThroughput()); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/jmeter/jmx/util/parseXML.java: -------------------------------------------------------------------------------- 1 | package jmeter.jmx.util; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.util.ArrayList; 6 | import java.util.Iterator; 7 | import java.util.List; 8 | 9 | import javax.xml.parsers.DocumentBuilder; 10 | import javax.xml.parsers.DocumentBuilderFactory; 11 | 12 | import org.dom4j.Attribute; 13 | import org.dom4j.Document; 14 | import org.dom4j.DocumentException; 15 | import org.dom4j.DocumentHelper; 16 | import org.dom4j.Element; 17 | import org.dom4j.io.DOMReader; 18 | 19 | 20 | public class parseXML { 21 | public static void main(String[] args) throws Exception 22 | { 23 | String xmlpath = "/Users/yangengzhe/Desktop/云平台/ygz/demo2.jtl"; 24 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 25 | DocumentBuilder db = dbf.newDocumentBuilder(); 26 | org.w3c.dom.Document t_document = db.parse(new File(xmlpath)); 27 | DOMReader domReader = new DOMReader(); 28 | Document document = domReader.read(t_document); 29 | // 获取根元素 30 | Element root = document.getRootElement(); 31 | long t=0,ts=0,startTime=Long.MAX_VALUE,endTime=0,requests=0; 32 | // 迭代输出 33 | for (Iterator iter = root.elementIterator(); iter.hasNext();) 34 | { 35 | Element e = (Element) iter.next(); 36 | t= Long.parseLong(e.attributeValue("t")); 37 | ts= Long.parseLong(e.attributeValue("ts")); 38 | startTime = ts endTime?(t+ts):endTime; 40 | requests++; 41 | } 42 | float throughput = (endTime-startTime)/1000f; 43 | throughput = requests/throughput; 44 | System.out.println("开始时间:"+startTime+",结束时间:"+endTime+",请求数:"+requests+",吞吐量:"+throughput); 45 | 46 | 47 | } 48 | 49 | 50 | } 51 | --------------------------------------------------------------------------------