Jmeter有兩種類型的函數:
-
用戶定義的靜態值(或變量)
-
內置函數
用戶定義的靜態值允許用戶定義變量時被替換為靜態的值測試樹編譯並提交運行。需要注意的是,變量目前無法嵌套,即${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 內部定義的一些變量。它們分別是:
-
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 的啟動時間,而不是測試開始時間。它們主要適用於使用文件名等。
表11-1 JMeter內置函數列表
| 函數類型 | 名稱 | 注釋 |
|---|---|---|
| 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 |
