一、在jemter中,在后置處理器bean shell中可以通過引入org.apache.jmeter.samplers.SampleResult;
從而對取樣器結果分析處理,現在說明一下最常用的幾個函數:
import org.apache.jmeter.samplers.SampleResult; String samplerData= prev.getSamplerData(); log.info("getSamplerData=======:"+samplerData); //響應結果 String threadName = prev.getThreadName(); log.info("ThreadName:"+threadName ); //得到threadname String getCookies = prev.getCookies(); log.info("getCookies:"+ getCookies); String getDataEncodingWithDefault = prev.getDataEncodingWithDefault(); log.info("getDataEncodingWithDefault:"+ getDataEncodingWithDefault); String getHTTPMethod = prev.getHTTPMethod(); log.info("getHTTPMethod:"+ getHTTPMethod); int getBodySize = prev.getBodySize(); //獲取返回報問題的大小 log.info("getBodySize:"+ getBodySize); int getTime = prev.getTime();//獲取該筆請求的耗時
log.info("getTime:"+ getTime);
腳本執行結果如下:
2020-07-01 16:15:31,936 INFO o.a.j.u.BeanShellTestElement: ThreadName:認購書發起合同 1-1 2020-07-01 16:15:31,936 INFO o.a.j.u.BeanShellTestElement: getCookies: 2020-07-01 16:15:31,936 INFO o.a.j.u.BeanShellTestElement: getDataEncodingWithDefault:UTF-8 2020-07-01 16:15:31,936 INFO o.a.j.u.BeanShellTestElement: getHTTPMethod:POST 2020-07-01 16:15:31,936 INFO o.a.j.u.BeanShellTestElement: getRedirectLocation:null 2020-07-01 16:15:31,936 INFO o.a.j.u.BeanShellTestElement: getBodySize:248966 2020-07-01 16:15:31,936 INFO o.a.j.u.BeanShellTestElement: getTime:2994
結果樹的結果如圖
尤其是 getBodySize()和getTime()兩個函數,在做大並發場景下,輸出超時的請求,方便對這筆業務做進一步分析,分析定位響應超時的問題;
if(getTime> 2000){ log.info("該筆請求的traceid 是"+ "${traceId}"); log.info("Respnse is " + response); log.info(code); }
這樣就可以輸出流水號,方便進行問題定位分析。
另外http://svn.apache.org/repos/asf/jmeter/branches/docs-2.9/docs/api/org/apache/jmeter/protocol/http/sampler/HTTPSampleResult.html可以查看所有的SampleResult包含的方法。
大致如下: