JMeter函數(shù)和用戶變量
• JMeter 函數(shù)是可以填充任何采樣器或其他元素在測試樹字段的特殊值。一個函數(shù)調(diào)用看起來像這樣:
${__functionName(var1,var2,var3)}
• _functionName 函數(shù)的名稱相匹配。例如 ${__threadNum}.
• 如果一個函數(shù)參數(shù)包含一個逗號,然后轉(zhuǎn)義“”,如下圖所示。
${__time(EEE, d MMM yyyy)}
• 引用變量:
${VARIABLE}
函數(shù)列表
下表列出了一組函數(shù)類型大致分為:
函數(shù)類型 |
名稱 |
注釋 |
---|---|---|
Information |
threadNum |
get thread number |
Information |
samplerName |
get the sampler name (label) |
Information |
machineIP |
get the local machine IP address |
Information |
machineName |
get the local machine name |
Information |
time |
return current time in various formats |
Information |
log |
log (or display) a message (and return the value) |
Information |
logn |
log (or display) a message (empty return value) |
Input |
StringFromFile |
read a line from a file |
Input |
FileToString |
read an entire file |
Input |
CSVRead |
read from CSV delimited file |
Input |
XPath |
Use an XPath expression to read from a file |
Calculation |
counter |
generate an incrementing number |
Calculation |
intSum |
add int numbers |
Calculation |
longSum |
add long numbers |
Calculation |
Random |
generate a random number |
Calculation |
RandomString |
generate a random string |
Calculation |
UUID |
generate a random type 4 UUID |
Scripting |
BeanShell |
run a BeanShell script |
Scripting |
javaScript |
process JavaScript (Mozilla Rhino) |
Scripting |
jexl, jexl2 |
evaluate a Commons Jexl expression |
Properties |
property |
read a property |
Properties |
P |
read a property (shorthand method) |
Properties |
setProperty |
set a JMeter property |
Variables |
split |
Split a string into variables |
Variables |
V |
evaluate a variable name |
Variables |
eval |
evaluate a variable expression |
Variables |
evalVar |
evaluate an expression stored in a variable |
String |
regexFunction |
parse previous response using a regular expression |
String |
escapeOroRegexpChars |
quote meta chars used by ORO regular expression |
String |
char |
generate Unicode char values from a list of numbers |
String |
unescape |
Process strings containing Java escapes (e.g. & ) |
String |
unescapeHtml |
Decode HTML-encoded strings |
String |
escapeHtml |
Encode strings using HTML encoding |
String |
TestPlanName |
Return name of current test plan |
• 有兩種類型的函數(shù):
① 用戶定義的靜態(tài)值(或變量)
② 內(nèi)置函數(shù)
• 用戶定義的靜態(tài)值允許用戶定義變量時被替換為靜態(tài)的值測試樹編譯并提交運行。
• 需要注意的是,變量目前無法嵌套,即${Var${N}}不起作用。
• __ V(變量)函數(shù)(版本2.2后)可用于執(zhí)行此操作: ${__V(Var${N})}.
• 這種類型的替換可能沒有函數(shù),但更方便,更直觀
函數(shù)和變量可以被寫入到任何領(lǐng)域的任何測試部件。
下面的函數(shù)測試計劃應(yīng)確定工作:
• intSum
• longSum
• machineName
• BeanShell
• javaScript
• jexl
• random
• time
• property functions
• log functions
測試計劃使用的功能有一些限制。JMeter的線程變量沒有被完全成立處理功能時,不會設(shè)置變量名作為參數(shù)傳遞,將無法正常工作和變量引用,所以split() 和 regex() 變量賦值函數(shù)不會工作。 threadNum() 函數(shù)將不能工作(沒有任何意義在測試計劃級別)。
• 引用的變量中的測試元件是通過包圍在變量名 '${' and '}'.
• 函數(shù)中引用同樣的方式,但按照慣例,函數(shù)的名稱以“__”開頭,以避免沖突與用戶值的名稱。
• 有些函數(shù)帶參數(shù)的配置,而這些括號中,逗號分隔。如果函數(shù)沒有參數(shù),括號可以省略。對于例如:
${__BeanShell(vars.put("name","value"))}
• 另外,可以定義你的腳本作為一個變量,例如測試計劃:
SCRIPT vars.put("name","value")
• 然后,該腳本可以被引用如下:
${__BeanShell(${SCRIPT})}
函數(shù)輔助對話框
JMeter 選項“tab“可從函數(shù)助手對話框。
• 使用函數(shù)助手,可以選擇一個函數(shù)從拉下來,并指派其參數(shù)值。左邊的表中的列的參數(shù),簡要說明和右列是你寫在該參數(shù)的值。不同函數(shù)的不同參數(shù)。
• 一旦這樣做了,點擊“生成”按鈕,并產(chǎn)生相應(yīng)的字符串復(fù)制粘貼到測試計劃
JMeter 內(nèi)部定義的一些變量。它們分別是:
• COOKIE_cookiename - 包含cookie的值
• JMeterThread.last_sample_ok - 與否的最后一個樣本是確定的 - true/false。注:這是更新后的后處理和斷言已經(jīng)運行。
• START 變量
一些內(nèi)置的的屬性定義JMeter。下面列出了這些。為方便起見,啟動的屬性也被復(fù)制到具有相同名稱的變量。
• START.MS - JMeter 啟動時間(毫秒)
• START.YMD - JMeter 啟動時間為 yyyyMMdd
• START.HMS - JMeter 啟動時間為 HHmmss
• TESTSTART.MS - 測試開始時間(毫秒)
請注意,開始變量/屬性代表 JMeter 的啟動時間,而不是測試開始時間。它們主要適用于使用文件名等。