记录之前使用jmeter中一些常用的函数
常用函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| log.info("Request: " + prev.getSamplerData());
log.info("Response: " + prev.getResponseDataAsString());
log.info("Thread Name: " + prev.getThreadName());
log.info("Sample Start: " + (new Date(prev.getStartTime())).toString());
log.info("Load Time: " + prev.getTime().toString());
log.info("Connect Time: " + prev.getConnectTime().toString());
log.info("Latency: " + prev.getLatency().toString());
log.info("Size in bytes: " + prev.getBytesAsLong().toString());
log.info("Sent bytes: " + prev.getSentBytes().toString());
log.info("Header size in bytes: " + prev.getHeadersSize().toString());
log.info("Body size in bytes: " + prev.getBodySizeAsLong().toString());
log.info("Sample Count: " + prev.getSampleCount().toString());
log.info("Error Count: " + prev.getErrorCount().toString());
log.info('Data type("text"|"bin"|""): ' + prev.getDataType().toString());
log.info("Response Code: " + prev.getResponseCode().toString());
log.info("Response Message: " + prev.getResponseMessage().toString());
log.info("Response Header: " + prev.getResponseHeaders().toString());
log.info("ContentType: " + prev.getContentType().toString());
log.info("DataEncoding: " + prev.getDataEncodingNoDefault().toString());
|
获取变量并计算
JmeterBeanShell获取变量并计算传出
1 2 3 4 5 6 7 8 9 10 11
| String aa = vars.get("runningsumtime");
String cc = vars.get("portsize");
int bb = Integer.parseInt(aa);
int dd = Integer.parseInt(cc);
int ee = bb/dd;
vars.put("runningtime",String.valueOf(ee));
|
打印Response至log,并断言
打印Jmeter响应内容(Response)至log,并断言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| String clue_msg="login_info"; String Str = "{\"code\":1000";
String response = ""; response = prev.getResponseDataAsString();
if(response == ""){ Failure = true; FailureMessage = "系统无响应,获取不到响应数据!"; log.info(FailureMessage); } else if(response.contains(Str) == false){ Failure = true; String Msg = "\n ---------------------------------------------"+clue_msg+"---------------------------------------------"; FailureMessage = Msg + "\n" + "期望结果: " + Str + "\n响应内容: " + response + "\n"; log.info(FailureMessage); }
|
如果未指定log存放地址,运行完成后会在jmeter文件夹中的bin目录下jmeter.log文件中如下图所示
打印响应时间至log,并断言
打印Jmeter响应时间(Response)至log,并断言
1 2 3 4 5 6 7 8 9 10 11
| String clue_msg="login_info"; int d = 100; int c; String q1=""; q1=prev.getTime().toString(); c = Integer.parseInt(q1); if(c>d){ String timeMsg = "\n ---------------------------------------------"+clue_msg+"---------------------------------------------"; time_log = timeMsg + "\n" + "期望响应时间: " + d + "ms \n实际响应时间: " + c + "ms\n"; log.info(time_log); }
|
效果如下图所示,当然也可以将断言响应内容整合在一起
这里有个点需要注意,如果是进行的分布式压测 log会存放在slave机中的bin目录下 jmeter-server.log中
变量自增
1 2 3 4 5 6 7 8 9 10 11 12 13
| # 传入参数 while_count 并获取值 while_count=vars.get("while_count");
# java是强类型 说明下 格式 int while_count_int= new Long(while_count).intValue();
# 执行 i++ 自加一操作 while_count_int++;
# 将 +1 后的参数传出 vars.put("while_count",while_count_int+"")
# while_count++
|
操作本地文件
有时我们需要将响应内容或者部分变量写入到一些指定文件,如txt,csv excel等等。
1 2 3 4 5 6 7 8 9 10
| FileWriter fstream = new FileWriter("./testresult/${__time(yyyyMMdd,)}.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(vars.get("group_id")+"\n");
out.close(); fstream.close();
|
Unicode转UTF-8
部分API响应内容 Response 会被压缩为Unicode编码,断言UTF-8的内容会出现错误的判定。
因此应首先利用BeanShell将Unicode,同一编码为UTF-8,再进行断言。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| BeanShell--UTF-8
String s2=new String(prev.getResponseData(),"UTF-8");
char aChar; int len= s2.length(); StringBuffer outBuffer=new StringBuffer(len); for(int x =0; x <len;){ aChar= s2.charAt(x++); if(aChar=='\\'){ aChar= s2.charAt(x++); if(aChar=='u'){ int value =0; for(int i=0;i<4;i++){ aChar= s2.charAt(x++); switch(aChar){ case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7': case'8': case'9': value=(value <<4)+aChar-'0'; break; case'a': case'b': case'c': case'd': case'e': case'f': value=(value <<4)+10+aChar-'a'; break; case'A': case'B': case'C': case'D': case'E': case'F': value=(value <<4)+10+aChar-'A'; break; default: throw new IllegalArgumentException( "Malformed \\uxxxx encoding.");}} outBuffer.append((char) value);}else{ if(aChar=='t') aChar='\t'; else if(aChar=='r') aChar='\r'; else if(aChar=='n') aChar='\n'; else if(aChar=='f') aChar='\f'; outBuffer.append(aChar);}}else outBuffer.append(aChar);}
prev.setResponseData(outBuffer.toString());
|