jmeter 一些測試用的方法 【不定時補充】
阿新 • • 發佈:2018-11-13
目錄
1、獲取當前執行緒的名稱 ctx.getThread().getThreadName()
1、獲取當前執行緒的名稱 ctx.getThread().getThreadName()
有兩種方法,可以直接用一個函式:
${__BeanShell(ctx.getThread().getThreadName().toString(),)}
也可以在beanshell裡寫指令碼:
import org.apache.jmeter.util.JMeterUtils; String threadinfo = ctx.getThread().getThreadName().toString(); log.info("threadinfo------------"+threadinfo);
2、儲存變數到檔案中
下面以儲存cookies為例,其它變數是類似的
import java.lang.String; import java.io.*; String str = "${COOKIE_xxx},${COOKIE_xxx}\r\n"; //拼接cookie字串 log.info("cookieStr: " + str); FileWriter writer = null; writer = new FileWriter("cookies0722.csv", true); //若檔案不存在則建立,若檔案存在則追加 writer.write(str); writer.close(); //log.info("save cookie in file success!");
3、儲存變數到全域性屬性/變數中
# props.put() 儲存變數到屬性中
props.put("access_token","${access_token}");
# props.get() 或 ${__P()} 從屬性中取出變數值
String access_token = props.get("access_token");
${__P(access_token,)}
# 兩種方式都可以從屬性中取出access_token
4、在beanshell中通過CookieManager新增cookie資訊
import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache.jmeter.protocol.http.control.Cookie; CookieManager manager = sampler.getCookieManager(); String s="\""+"${COOKIE_user_id}"+"\""; log.info("s------------------------------------------"+s); Cookie cookie = new Cookie("user_id",s,"${ip}","/",false,0); manager.add(cookie); Cookie cookie = new Cookie("wb_token","${COOKIE_wb_token}","${ip}","/",false,0); manager.add(cookie); String s1="\""+"${COOKIE_wb_token_sign}"+"\""; Cookie cookie = new Cookie("wb_token_sign",s1,"${ip}","/",false,0); manager.add(cookie); Cookie cookie = new Cookie("wb_dcn","${COOKIE_wb_dcn}","${ip}","/",false,0); manager.add(cookie); Cookie cookie = new Cookie("wb_app_id","${app_id}","${ip}","/",false,0); manager.add(cookie);