jmeter beanshell 變數傳遞
如果寫成這樣會報錯:
${__BeanShell(${__threadNum}*2,ToatlAmount)};
${__BeanShell(${__Random(1,99999,)},DayNum)};
//${__BeanShell(vars.put("BXReason"\,"${__UUID}"))};
${__BeanShell(${__threadNum},BXReason)};
報錯資訊:
2018-12-20 01:42:30,820 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``StandardJMeterEngine*2;'' : illegal use of undefined variable, class, or 'void' literal
2018-12-20 01:42:30,820 WARN o.a.j.f.BeanShell: Error running BSH script
org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``StandardJMeterEngine*2;'' : illegal use of undefined variable, class, or 'void' literal
at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:183) ~[ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:189) ~[ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.functions.BeanShell.execute(BeanShell.java:100) [ApacheJMeter_functions.jar:5.0 r1840935]
at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:136) [ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:111) [ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.testelement.property.FunctionProperty.getStringValue(FunctionProperty.java:92) [ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.testbeans.TestBeanHelper.unwrapProperty(TestBeanHelper.java:128) [ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.testbeans.TestBeanHelper.prepare(TestBeanHelper.java:83) [ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.engine.StandardJMeterEngine.notifyTestListenersOfStart(StandardJMeterEngine.java:212) [ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.engine.StandardJMeterEngine.run(StandardJMeterEngine.java:384) [ApacheJMeter_core.jar:5.0 r1840935]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_172]
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_172]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_172]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_172]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_172]
at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:166) ~[ApacheJMeter_core.jar:5.0 r1840935]
... 10 more
import java.text.DecimalFormat;
String strThreadNum = "${__threadNum}"; //取得當前的虛擬使用者ID int thNum = Integer.parseInt(strThreadNum); String str = "${iterNO}"; //取得該虛擬使用者當前的迴圈次數 int i = Integer.parseInt(str); int mobileNumLastFive = thNum * 10000 + i; DecimalFormat df = new DecimalFormat( "0000000000" ); String fullNum = 4 + df.format(mobileNumLastFive); //格式化成4開頭的11位手機號碼 System.out.println(fullNum); vars.put("mobile", fullNum); //將手機號碼存入名為mobile的變數,該變數可以在“HTTP請求”中用到 BeanShell PreProcessor指令碼:String a0=props.get("newtoken");
vars.put("token2",a0);
String a0=props.get("newcompanyId");
vars.put("companyId2",a0);
String a2=props.get("newuserId");
vars.put("userId2",a2);
String a3=props.get("newuserName");
vars.put("userName2",a3);
String a4=props.get("neworgCode");
vars.put("orgCode2",a4);
String a5=props.get("newcostCenterName");
vars.put("costCenterName2",a5);
String a6=props.get("newprofitCenterName");
vars.put("profitCenterName2",a6);
String a7=props.get("newcostCenterID");
vars.put("costCenterID2",a7);
String ToatlAmount="${__BeanShell(${__threadNum}*2,)}";
vars.put("ToatlAmount",ToatlAmount);
String DayNum="${__Random(1,99999,)}";
vars.put("DayNum",DayNum);
String BXReason="${__threadNum}";
vars.put("BXReason",BXReason);
4、Bean Shell常用內建變數
JMeter在它的BeanShell中內建了變數,使用者可以通過這些變數與JMeter進行互動,其中主要的變數及其使用方法如下:
log:用來記錄日誌檔案,寫入到jmeber.log檔案,使用方法:log.info(“This is log info!”);
ctx(JmeterContext)通過它來訪問context,使用方法可參考:org.apache.jmeter.threads.JMeterContext。
vars - (JMeterVariables):操作jmeter變數,提供讀取/寫入訪問變數的方法。這個變數實際引用了JMeter執行緒中的區域性變數容器(本質上是Map),它是測試用例與BeanShell互動的橋樑,常用方法:
a) vars.get(String key):從jmeter中獲得變數值
b) vars.put(String key,String value):資料存到jmeter變數中
c) vars.putObject("OBJ1",new Object());
更多方法可參考:org.apache.jmeter.threads.JMeterVariables
props - (JMeterProperties - class Java.util.Properties):操作jmeter屬性,該變數引用了JMeter的配置資訊,可以獲取Jmeter的屬性,它的使用方法與vars類似,但是隻能put進去String型別的值,而不能是一個物件。對應於java.util.Properties。
a) props.get("START.HMS"); 注:START.HMS為屬性名,在檔案jmeter.properties中定義
b) props.put("PROP1","1234");
prev - (SampleResult):獲取前面的sample取樣的結果,常用方法:
a) getResponseDataAsString():獲取響應資訊
b) getResponseCode() :獲取響應code
更多方法可參考:org.apache.jmeter.samplers.SampleResult
sampler - (Sampler):gives access to the current sampler 訪問當前取樣
BeanShell PostProcessor指令碼:
String newtoken=bsh.args[0];
print(newtoken);
${__setProperty(newtoken,${token},)};
String newcompanyId=bsh.args[1];
print(newcompanyId);
${__setProperty(newcompanyId,${companyId},)};
String newuserId=bsh.args[2];
print(newuserId);
${__setProperty(newuserId,${userId},)};
String newuserName=bsh.args[3];
print(newuserName);
${__setProperty(newuserName,${userName},)};
String neworgCode=bsh.args[4];
print(neworgCode);
${__setProperty(neworgCode,${orgCode},)};
String newcostCenterName=bsh.args[5];
print(newcostCenterName);
${__setProperty(newcostCenterName,${costCenterName},)};
String newprofitCenterName=bsh.args[6];
print(newprofitCenterName);
${__setProperty(newprofitCenterName,${profitCenterName},)};
String newcostCenterID=bsh.args[7];
print(newcostCenterID);
${__setProperty(newcostCenterID,${costCenterID},)};