📄 functions.html
字号:
</p>
<p>
This type of replacement is possible without functions, but was less convenient and less intuitive.
It required users to create default config elements that would fill in blank values of Samplers.
Variables allow one to replace only part of any given value, not just filling in blank values.
</p>
<p>
With built-in functions users can compute new values at run-time based on previous response data, which
thread the function is in, the time, and many other sources. These values are generated fresh for every
request throughout the course of the test.
</p>
<p><table border="1" bgcolor="#bbbb00" width="50%" cellspacing="0" cellpadding="2">
<tr><td>Functions are shared between threads.
Each occurrence of a function call in a test plan is handled by a separate function instance.
</td></tr>
</table></p>
</blockquote>
</td></tr>
<tr><td><br></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#828DA6">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="where"><strong>19.2 Where can functions and variables be used?</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
Functions and variables can be written into any field of any test component (apart from the TestPlan - see below).
Some fields do not allow random strings
because they are expecting numbers, and thus will not accept a function. However, most fields will allow
functions.
</p>
<p>
Functions which are used on the Test Plan have some restrictions.
JMeter thread variables will have not been fully set up when the functions are processed,
so variable names passed as parameters will not be set up, and variable references will not work,
so split() and regex() and the variable evaluation functions won't work.
The threadNum() function won't work (and does not make sense at test plan level).
The following functions should work OK on the test plan:
<ul>
<li>
intSum
</li>
<li>
longSum
</li>
<li>
machineName
</li>
<li>
BeanShell
</li>
<li>
javaScript
</li>
<li>
jexl
</li>
<li>
random
</li>
<li>
time
</li>
<li>
property functions
</li>
<li>
log functions
</li>
</ul>
</p>
<p>
Configuration elements are processed by a separate thread.
Therefore functions such as __threadNum do not work properly in elements such as User Defined Variables.
Also note that variables defined in a UDV element are not available until the element has been processed.
</p>
<p><table border="1" bgcolor="#bbbb00" width="50%" cellspacing="0" cellpadding="2">
<tr><td>When using variable/function references in SQL code (etc),
remember to include any necessary quotes for text strings,
i.e. use
<br>
</br>
SELECT item from table where name='${VAR}'
<br>
</br>
<b>
not
</b>
<br>
</br>
SELECT item from table where name=${VAR}
<br>
</br>
(unless VAR itself contains the quotes)
</td></tr>
</table></p>
</blockquote>
</td></tr>
<tr><td><br></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#828DA6">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="how"><strong>19.3 How to reference variables and functions</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
Referencing a variable in a test element is done by bracketing the variable name with '${' and '}'.
</p>
<p>
Functions are referenced in the same manner, but by convention, the names of
functions begin with "__" to avoid conflict with user value names
<sup>
*
</sup>
. Some functions take arguments to
configure them, and these go in parentheses, comma-delimited. If the function takes no arguments, the parentheses can
be omitted.
</p>
<p>
Argument values that themselves contain commas should be escaped as necessary.
If you need to include a comma in your parameter value, escape it like so: '\,'.
This applies for example to the scripting functions - Javascript, Beanshell, Jexl - where it is necessary to escape any commas
that may be needed in script method calls - e.g.
</p>
<pre>
${__BeanShell(vars.put("name"\,"value"))}
</pre>
<p>
Functions can reference variables and other functions, for example
<code>
${__XPath(${__P(xpath.file),${XPATH})}
</code>
will use the property "xpath.file" as the file name
and the contents of the variable XPATH as the expression to search for.
</p>
<p>
JMeter provides a tool to help you construct
function calls for various built-in functions, which you can then copy-paste.
It will not automatically escape values for you, since functions can be parameters to other functions, and you should only escape values you intend as literal.
</p>
<p>
<b>
The value of a variable or function can be reported
</b>
using the
<a href="#__logn">
__logn()
</a>
function.
The __logn() function reference can be used anywhere in the test plan after the variable has been defined.
Alternatively, the Java Request sampler can be used to create a sample containing variable references;
the output will be shown in the appropriate Listener.
For versions of JMeter later than 2.3, there is a
<a href="../usermanual/component_reference.html#Debug_Sampler">Debug Sampler</a>
that can be used to display the values of variables etc in the Tree View Listener.
</p>
<p><table border="1" bgcolor="#bbbb00" width="50%" cellspacing="0" cellpadding="2">
<tr><td><sup>
*
</sup>
If you define a user-defined static variable with the same name as a built-in function, your static
variable will override the built-in function.
</td></tr>
</table></p>
</blockquote>
</td></tr>
<tr><td><br></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#828DA6">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="function_helper"><strong>19.4 The Function Helper Dialog</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
The Function Helper dialog is available from JMeter's Tools menu.
</p>
<p><table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="../../docs/images/screenshots/function_helper_dialog.png"><br>
<font size="-1">Function Helper Dialog
</font></td></tr></table></p>
<p>
Using the Function Helper, you can select a function from the pull down, and assign
values for its arguments. The left column in the table provides a brief description of the
argument, and the right column is where you write in the value for that argument. Different
functions take different arguments.
</p>
<p>
Once you have done this, click the "generate" button, and the appropriate string is generated
for you to copy-paste into your test plan wherever you like.
</p>
</blockquote>
</td></tr>
<tr><td><br></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#828DA6">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="functions"><strong>19.5 Functions</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<table border="0" cellspacing="0" cellpadding="2">
<tr><td>
<font face="arial,helvetica,sanserif">
<h3><a name="__regexFunction">19.5.1 __regexFunction</h3></a>
</font>
</td></tr>
<tr><td>
<p>
The Regex Function is used to parse the previous response (or the value of a variable) using any regular
expression (provided by user). The function returns the template string with variable values filled
in.
</p>
<p>
The __regexFunction can also store values for future use. In the sixth parameter, you can specify
a reference name. After this function executes, the same values can be retrieved at later times
using the syntax for user-defined values. For instance, if you enter "refName" as the sixth
parameter you will be able to use:
<ul>
<li>
${refName} to refer to the computed result of the second parameter ("Template for the
replacement string") parsed by this function
</li>
<li>
${refName_g0} to refer to the entire match parsed by this function.
</li>
<li>
${refName_g1} to refer to the first group parsed by this function.
</li>
<li>
${refName_g#} to refer to the n
<sup>
th
</sup>
group parsed by this function.
</li>
<li>
${refName_matchNr} to refer to the number of groups found by this function.
</li>
</ul>
</p>
<p>
<b>Parameters</b>
<table border="1" cellspacing="0" cellpadding="2">
<tr><th>Attribute</th><th>Description</th><th>Required</th></tr>
<tr>
<td>First argument</td>
<td>The first argument is the regular expression
to be applied to the response data. It will grab all matches. Any parts of this expression
that you wish to use in your template string, be sure to surround in parentheses. Example:
<a href="(.*)">. This will grab the value of the link and store it as the first group (there is
only 1 group). Another example: <input type="hidden" name="(.*)" value="(.*)">. This will
grab the name as the first group, and the value as the second group. These values can be used
in your template string
</td>
<td>
Yes
</td>
</tr>
<tr>
<td>Second argument</td>
<td>This is the template string that will replace
the function at run-time. To refer to a group captured in the regular expression, use the syntax:
$[group_number]$. Ie: $1$, or $2$. Your template can be any string.
</td>
<td>
Yes
</td>
</tr>
<tr>
<td>Third argument</td>
<td>The third argument tells JMeter which match
to use. Your regular expression might find numerous matches. You have four choices:
<ul>
<li>
An integer - Tells JMeter to use that match. '1' for the first found match, '2' for the
second, and so on
</li>
<li>
RAND - Tells JMeter to choose a match at random.
</li>
<li>
ALL - Tells JMeter to use all matches, and create a template string for each one and then
append them all together. This option is little used.
</li>
<li>
A float number between 0 and 1 - tells JMeter to find the Xth match using the formula:
(number_of_matches_found * float_number) rounded to nearest integer.
</li>
</ul>
</td>
<td>
No, default=1
</td>
</tr>
<tr>
<td>Fourth argument</td>
<td>If 'ALL' was selected for the above argument
value, then this argument will be inserted between each appended copy of the template value.
</td>
<td>
No
</td>
</tr>
<tr>
<td>Fifth argument</td>
<td>Default value returned if no match is found
</td>
<td>
No
</td>
</tr>
<tr>
<td>Sixth argument</td>
<td>A reference name for reusing the values parsed by this function.
<br>
</br>
Stored values are ${refName} (the replacement template string) and ${refName_g#} where "#" is the
group number from the regular expression ("0" can be used to refer to the entire match).
</td>
<td>
No
</td>
</tr>
<tr>
<td>Seventh argument</td>
<td>Input variable name.
If specified, then the value of the variable is used as the input instead of using the previous sample result.
</td>
<td>
No
</td>
</tr>
</table>
</p>
</td></tr>
<tr><td><br></td></tr>
</table>
<hr>
<table border="0" cellspacing="0" cellpadding="2">
<tr><td>
<font face="arial,helvetica,sanserif">
<h3><a name="__counter">19.5.2 __counter</h3></a>
</font>
</td></tr>
<tr><td>
<p>
The counter generates a new number each time it is called, starting with 1
and incrementing by +1 each time. The counter can be configured to keep each simulated user's values
separate, or to use the same counter for all users. If each user's values is incremented separately,
that is like counting the number of iterations through the test plan. A global counter is like
counting how many times that request was run.
</p>
<p>
The counter uses an integer variable to hold the count, which therefore has a maximum of 2,147,483,647.
</p>
<p>
The counter function instances are now completely independent.
[JMeter 2.1.1 and earlier used a fixed thread variable to keep track of the per-user count,
so multiple counter functions operated on the same value.]
The global counter - "FALSE" - is separately maintained by each counter instance.
</p>
<p>
<b>Parameters</b>
<table border="1" cellspacing="0" cellpadding="2">
<tr><th>Attribute</th><th>Description</th><th>Required</th></tr>
<tr>
<td>First argument</td>
<td>TRUE if you wish each simulated user's counter
to be kept independent and separate from the other users. FALSE for a global counter.
</td>
<td>
Yes
</td>
</tr>
<tr>
<td>Second argument</td>
<td>A reference name for reusing the value created by this function.
<br>
</br>
Stored values are of the form ${refName}. This allows you to keep one counter and refer to its value in
multiple places. [For JMeter 2.1.1 and earlier this parameter was required.]
</td>
<td>
No
</td>
</tr>
</table>
</p>
</td></tr>
<tr><td><br></td></tr>
</table>
<hr>
<table border="0" cellspacing="0" cellpadding="2">
<tr><td>
<font face="arial,helvetica,sanserif">
<h3><a name="__threadNum">19.5.3 __threadNum</h3></a>
</font>
</td></tr>
<tr><td>
<p>
The thread number function simply returns the number of the thread currently
being executed. These numbers are independent of ThreadGroup, meaning thread #1 in one threadgroup
is indistinguishable from thread #1 in another threadgroup, from the point of view of this function.
</p>
<p>
There are no arguments for this function.
</p>
<p><table border="1" bgcolor="#bbbb00" width="50%" cellspacing="0" cellpadding="2">
<tr><td>
This function does not work in any Configuration elements (e.g. User Defined Variables) as these are run from a separate thread.
Nor does it make sense to use it on the Test Plan.
</td></tr>
</table></p>
</td></tr>
<tr><td><br></td></tr>
</table>
<hr>
<table border="0" cellspacing="0" cellpadding="2">
<tr><td>
<font face="arial,helvetica,sanserif">
<h3><a name="__intSum">19.5.4a __intSum</h3></a>
</font>
</td></tr>
<tr><td>
<p>
The intSum function can be used to compute the sum of two or more integer values.
</p>
<p><table border="1" bgcolor="#bbbb00" width="50%" cellspacing="0" cellpadding="2">
<tr><td>
JMeter Versions 2.3.1 and earlier required the reference name to be present.
The reference name is now optional, but it must not be a valid integer.
</td></tr>
</table></p>
<p>
<b>Parameters</b>
<table border="1" cellspacing="0" cellpadding="2">
<tr><th>Attribute</th><th>Description</th><th>Required</th></tr>
<tr>
<td>First argument</td>
<td>The first int value.
</td>
<td>
Yes
</td>
</tr>
<tr>
<td>Second argument</td>
<td>The second int value.
</td>
<td>
Yes
</td>
</tr>
<tr>
<td>nth argument</td>
<td>The nth int value.
</td>
<td>
No
</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -