JMeter函數和用戶變量
• JMeter 函數是可以填充任何采樣器或其他元素在測試樹字段的特殊值。一個函數調用看起來像這樣:
${__functionName(var1,var2,var3)}
• _functionName 函數的名稱相匹配。例如 ${__threadNum}.
• 如果一個函數參數包含一個逗號,然后轉義“”,如下圖所示。
${__time(EEE, d MMM yyyy)}
• 引用變量:
${VARIABLE}
函數列表
下表列出了一組函數類型大致分為:
函數類型 |
名稱 |
注釋 |
---|---|---|
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 |
• 有兩種類型的函數:
① 用戶定義的靜態值(或變量)
② 內置函數
• 用戶定義的靜態值允許用戶定義變量時被替換為靜態的值測試樹編譯并提交運行。
• 需要注意的是,變量目前無法嵌套,即${Var${N}}不起作用。
• __ V(變量)函數(版本2.2后)可用于執行此操作: ${__V(Var${N})}.
• 這種類型的替換可能沒有函數,但更方便,更直觀
函數和變量可以被寫入到任何領域的任何測試部件。
下面的函數測試計劃應確定工作:
• intSum
• longSum
• machineName
• BeanShell
• javaScript
• jexl
• random
• time
• property functions
• log functions
測試計劃使用的功能有一些限制。JMeter的線程變量沒有被完全成立處理功能時,不會設置變量名作為參數傳遞,將無法正常工作和變量引用,所以split() 和 regex() 變量賦值函數不會工作。 threadNum() 函數將不能工作(沒有任何意義在測試計劃級別)。
• 引用的變量中的測試元件是通過包圍在變量名 '${' and '}'.
• 函數中引用同樣的方式,但按照慣例,函數的名稱以“__”開頭,以避免沖突與用戶值的名稱。
• 有些函數帶參數的配置,而這些括號中,逗號分隔。如果函數沒有參數,括號可以省略。對于例如:
${__BeanShell(vars.put("name","value"))}
• 另外,可以定義你的腳本作為一個變量,例如測試計劃:
SCRIPT vars.put("name","value")
• 然后,該腳本可以被引用如下:
${__BeanShell(${SCRIPT})}
函數輔助對話框
JMeter 選項“tab“可從函數助手對話框。
• 使用函數助手,可以選擇一個函數從拉下來,并指派其參數值。左邊的表中的列的參數,簡要說明和右列是你寫在該參數的值。不同函數的不同參數。
• 一旦這樣做了,點擊“生成”按鈕,并產生相應的字符串復制粘貼到測試計劃
JMeter 內部定義的一些變量。它們分別是:
• COOKIE_cookiename - 包含cookie的值
• JMeterThread.last_sample_ok - 與否的最后一個樣本是確定的 - true/false。注:這是更新后的后處理和斷言已經運行。
• START 變量
一些內置的的屬性定義JMeter。下面列出了這些。為方便起見,啟動的屬性也被復制到具有相同名稱的變量。
• START.MS - JMeter 啟動時間(毫秒)
• START.YMD - JMeter 啟動時間為 yyyyMMdd
• START.HMS - JMeter 啟動時間為 HHmmss
• TESTSTART.MS - 測試開始時間(毫秒)
請注意,開始變量/屬性代表 JMeter 的啟動時間,而不是測試開始時間。它們主要適用于使用文件名等。