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