详解JMeter函数和变量

JMeter函数可以被认为是某种特殊的变量,它们可以被采样器或者其他测试元件所引用。函数调用的语法如下:

1 ${__functionName(var1,var2,var3)} 

其中,__functionName匹配被调用的函数名称。用圆括号包含函数的形参,例如${__time(YMD)},不同函数要求的参数也不同。有些JMeter函数不要求参数,则可以不使用圆括号,例如${__threadNum}。

如果一个函数的参数中包含逗号,那么必须对逗号进行转义(使用""),否则JMeter会把逗号当成参数分隔符。例如:

  1. ${__time(EEE\, d MMM yyyy)} 

变量引用的语法如下:

  1. ${VARIABLE} 

如果测试计划中引用了未定义的变量或者函数,那么JMeter并不会报告/记录错误信息,引用返回的值就是引用自身。例如,假设字符串UNDEF没有被定义为变量,那么${UNDEF}返回的值就是${UNDEF}。变量、函数(包括属性)都是大小写敏感的。JMeter 2.3.1及其后续版本会剔除参数名中的空格,例如,${__Random(1,63, LOTTERY )}中的"LOTTERY "会被"LOTTERY"所代替。

属性不同于变量。变量对线程而言是局部的,所有线程都可以访问属性,就使用__P或者__property函数。

如表11-1所示为JMeter内置函数的列表(按类型划分)。

表11-1  JMeter内置函数列表

函数类型

函数名称

注释

Information

threadNum

get thread number

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

Scripting

BeanShell

run a BeanShell script

Scripting

javaScript

process JavaScript (Mozilla Rhino)

Scripting

jexl

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

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.使用函数可以做些什么

  目前有两种类型的函数:用户定义的静态值(或者变量)和JMeter内置函数。

  当需要编译测试树或者提交运行时,用户可以使用自定义变量来代替常用的静态值。这种替换只在测试的开始阶段执行一次。一个典型的应用就是使用自定义变量来替换所有HTTP请求的DOMAIN域,例如,做出轻微改动,就可以让同一个测试脚本适配多个服务器。

  需要注意,目前变量不支持嵌套;例如${Var${N}}不能正常工作。但是在JMeter 2.2及其以后版本中,可以借助函数__V (variable)来达成嵌套变量的目的(如${__V(Var${N})})。在早期的JMeter版本中可以使用${__BeanShell(vars.get("Var${N}")}。

     

原文地址:https://www.cnblogs.com/xiaoluosun/p/4962748.html