Вы находитесь на странице: 1из 41

JMETER Functions

JMeter functions are special values that can populate fields of any Sampler or other element in a test tree. A function call looks like this: ${__functionName(var !var"!var#$% &here '__functionName' matches the name of a function. (arentheses surround the parameters sent to the function. )he actual parameters vary from function to function. *aria+les are referenced as follo,s: ${*A-.A/01% Information Related Functions: $ threadNum : )he thread num+er function simply returns the num+er of the thread currently +ein2 e3ecuted. ${__threadNum% 44,ill return the thread count )his function does not ,ork in any 5onfi2uration elements (e.2. 6ser 7efined *aria+les$ as these are run from a separate thread. -e8uest:

-esponse:

"$ machineName : )he machineName function returns the local host name. .ts very useful ,hile implementin2 7istri+uted testin2 ${__machineName(Machine_Name$% 44*aria+le name to store value

-e8uest:

-esponse:

#$ time : )he time function returns the current time in various formats. ${__time(yyyyMMdd9::mmss!time $% 44 st ar2ument is format. .t can +e ; <M7 = yyyyMMdd ; :MS = ::mmss ; <M7:MS = yyyyMMdd9::mmss Second parameter is varia+le to store value. ${__time(${__property(time .6S1- $%$% 44Assi2n value to time.6S1- varia+le in the Jmeter property 7efault is <M7.

-e8uest:

-esponse:

>$lo2 : )he lo2 function lo2s a messa2e! and returns its input strin2 ${__lo2(Messa2e$% 44,ill ,rite to the lo2 file as '...thread Name : Messa2e' ${__lo2(Messa2e!?6)$% 44,ill ,rite to console ,indo, ${__lo2(${*A-%!!!*A-=$% 44,ill ,rite to lo2 file as '...thread Name *A-=value' -e8uest:

-esponse:

@$lo2n : )he lo2n function lo2s a messa2e! and returns the empty strin2 ${__lo2n(*A- =${*A- %!?6)$% 44,ill ,rite the value of the varia+le to the console ,indo,

-e8uest:

-esponse:

Input Related Functions: $ Strin2AromAile : )he Strin2AromAile function can +e used to read strin2s from a te3t file. ${__strin2AromAile((.NBC.C7A)!! !"$% 44reads (.N .7A)! (.N".7A) ${_Strin2AromAile((.N.7A)!!!"$% 44 reads (.N.7A) t,ice

-e8uest:

-esponse:

"$Aile to Strin2 : )he Aile)oStrin2 function can +e used to read an entire file. 1ach time it is called it reads the entire file. ${__file)oStrin2(a+c.t3t!!rname$% 44Airst ar2ument specifies file

name!second specifies file encodin2 and third specifies reference varia+le$ -e8uest:

-esponse :

#$ 5S*-ead : )he 5S*-ead function returns a strin2 from a 5S* file.

${__5S*-ead(data.t3t!D$ from first ro,

44,ill return first column first value

${__5S*-ead(data.t3t! $ ${__5S*-ead(data.t3t!ne3t$ 44,ill return second column first value and chan2e the ro, ${__5S*-ead(data.t3t!D$ 44,ill return first column first value from second ro, -e8uest:

-esponse :

>$Epath : )he E(ath function reads an EM0 file and matches the E(ath. 1ach time the function is called! the ne3t match ,ill +e returned. At end of file! it ,ill ,rap around to the start. .f no nodes matched! then the function ,ill return the empty strin2! and a ,arnin2 messa2e ,ill +e ,ritten to the JMeter lo2 file. ${__E(ath(4home4Farvita4+uild.3ml! 44tar2et4Gname$% 44,ill return the contents of the ne3t name attri+ute in +uild.3ml

-e8uest :

-esponse :

Calculation Related Functions : $ counter : )he counter 2enerates a ne, num+er each time it is called! startin2 ,ith and

incrementin2 +y H each time. )he counter can +e confi2ured to keep each simulated userCs values separate! or to use the same counter for all users. A 2lo+al counter is like countin2 ho, many times that re8uest ,as run. ${__counter(true!3$% 44Airst ar2ument specifies ,hether the counter is 2lo+al or not! false =I 2lo+al counter! true =I each user counter to +e kept independent.second ar2ument specifies reference varia+le -e8uest :

-esponse :

"$intSum : )he intSum function can +e used to compute the sum of t,o or more inte2er values. ${__intSum(${3%!@!J$% 44Airst Ar2ument retrieve3 value of varia+le 3! "nd is constant value! #rd ar2ument ,ill store result

-e8uest :

-esponse :

#$lon2Sum : )he lon2Sum function can +e used to compute the sum of t,o or more lon2 values. ${__lon2Sum( DKKKK! DDDDK!J$% 44Airst and second ar2ument specifies value! #rd ,ill store result.

-e8uest :

-esponse :

>$-andom : )he random function returns a random num+er that lies +et,een the 2iven min and ma3 values. ${__random( ! DD!3$ 44Airst ar2ument is minimum value! second ar2ument is ma3imum value and third ar2ument is varia+le to store value.

-e8uest :

-esponse :

Scripting Related Functions : $ LavaScript : )he LavaScript function e3ecutes a piece of JavaScript (not JavaM$ code and returns its value. ${__Lavascript(ne, 7ate$% 44,ill return current date

-e8uest :

-esponse :

"$ /eanShell : )he /eanShell function evaluates the script passed to it! and returns the result. ${__+eanshell( "#;>@N$% 44,ill return @NDOO ${__+eanshell(source('4home4Favita4a+c.+sh'$$% 44if a+c.+sh is present at this location ${__+eanshell(source(a+c.+sh$$ 44if a+c.+sh is present in Lmeter +in directory .f a+c.+sh contains a=OP eval('+=a;"'$P print(+$P 44.t ,ill (rint result to terminal as N

-e8uest :

-esponse :

Properties Related Functions : $ ( : )his is a simplified property function ,hich is intended for use ,ith properties defined on the command line. and if no default value is supplied! it is assumed to +e . )he value of ,as chosen +ecause it is valid for common test varia+les such as loops! thread count! ramp up etc. 7efine the property value: Lmeter 9J2roup .threads=Q 9Jhostname =,,,.realhost.edu Aetch the values: ${__((2roup .threads$% 44return the value of 2roup .threads ${__((2roup .loops$% 44return the value of 2roup .loops ${__((hostname!,,,.dummy.or2$% 44return value of property hostname or ,,,.dummy.or2 if not defined .n the e3amples a+ove! the first function call ,ould return Q! the second ,ould return and the last ,ould return ,,,.dummy.or2 (unless those properties ,ere defined else,hereM$

-e8uest :

-esponse :

"$ (roperty : )he property function returns the value of a JMeter property. .f the property value cannot +e found! and no default has +een supplied! it returns the property name. &hen supplyin2 a default value. Aor e3ample: ${__property(3$% 9 return value of 3 ${__property(3!y$% 9 return value of 3 and save in y ${__property(3!y!a$% 9 return value of property 3 (or 'a' if not defined$ and save in y ${__property(3!!a$% 9 return value of property 3 (= 'a' if not defined$ +ut donCt save it

-e8uest :

-esponse :

#$ set(roperty : )he set(roperty function sets the value of a JMeter property. )he default return value from the function is the empty strin2! so the function call can +e used any,here functions are valid.)he ori2inal value can +e returned +y settin2 the optional #rd parameter to 'true'. ${__set(roperty(total_5ount!@DD!$% 44Airst ar2ument is refenence name for the property! second ar2ument is value!third ar2ument is optional if set true returns the ori2inal value.

-e8uest :

-esponse :

Variables Related Functions: $ Split : )he split function splits the strin2 passed to it accordin2 to the delimiter! and returns the ori2inal strin2. .f any delimiters are adLacent! 'R' is returned as the value. )he split strin2s are returned in the varia+les ${*A-_ %! ${*A-_"% etc. )he count of varia+les is returned in $ {*A-_n%. 13ample: name='FarvitaSSmehtaS' in the test plan. ${__split(${name%!name!S$% 44returns the contents of name! i.e. 'FarvitaSSmehtaS' name_n=> 44,ill return the num+er of splited strin2s name_ =Farvita 44,ill return the first splitted strin2 name_"=R 44 return R as t,o delimiters are adLacent name_#=mehta 44,ill retun the third splited strin2 and so on.

-e8uest :

-esponse :

"$ * : )he * (varia+le$ function returns the result of evaluatin2 a varia+le name e3pression. )his can +e used to evaluate nested varia+le references. ${__*(A${N%$% 44,ill return the value of A ,hen N is defined as N= -e8uest :

-esponse :

#$ eval : )he eval function returns the result of evaluatin2 a strin2 e3pression..t allo,s to interpolate varia+le and function references in a strin2 ,hich is stored in a varia+le. 13ample: name=Farvita column=1mp_.d ta+le=1mployee ST0=select ${column% from ${ta+le% ,here name=C${name%C ${__eval(${ST0%$% 44 ,ill evaluate as 'select 1mp_.d from 1mployee ,here name=CFarvitaC'.

-e8uest :

-esponse :

>$ eval*ar : )he eval function returns the result of evaluatin2 an e3pression stored in a varia+le.)his allo,s one to read a strin2 from a file! and process any varia+le references in it. 13ample: 8uery = 'select ${column% from ${ta+le%' column=name ta+le=customers ${__eval*ar(8uery$% 44,ill evaluate as 'select name from customers'

-e8uest :

-esponse :

Вам также может понравиться