使用jmeter做接口調試、性能測試,都會涉及到編寫部分的腳本。
前置處理器、后置處理器、beanShell、Time等都會使用到
在某個接口下新增一個后置處理器。
jmeter默認會初始化SampleResult類,jmeter里面的類大部分都是從Object 擴展而來。
在beanShell腳本編輯器中,直接通過prev.getUrlAsString();獲取接口的url路徑。
prev代表當前的接口。
以下介紹幾種SampleResult類中方法。SampleResult中的所有方法都可以通過prev.來調用
導入import org.apache.jmeter.assertions.AssertionResult;
1.獲取接口結果code
public String getResponseCode()
使用方法:
String code = prev.getResponseCode();
通過打印日志查看:log.info("code:"+code);
2.獲取連接到服務器的時間
public long getConnectTime()
使用方法:
String ctime = prev.getConnectTime().toString();
通過打印日志查看:log.info("ctime:"+ctime);
3.獲取頭文件中ContentType類型
public String getContentType()
Returns:
the full content type - e.g. text/html [;charset=utf-8 ]
使用方法:
String cType = prev.getContentType();
通過打印日志查看:log.info("cType:"+cType);
4.獲取接口返回的數據
public String getResponseDataAsString()
Gets the responseData of the SampleResult object as a String
Returns:
the responseData value as a String, converted according to the encoding
使用方法:
String rdata = prev.getResponseDataAsString();
通過打印日志查看:log.info("rdata:"+rdata);
5.獲取線程名
String tname = prev.getThreadName();
log.info("ThreadName:"+tname);
6.設置線程名
String tname_1 = “我修改的名字”;
prev.setThreadName(tname_1);
String tname = prev.getThreadName();
日志打印:log.info("tname:"+tname);