Feather Background Waves Background
Skip to main content
Feather Background Waves Background
Feather Background Waves Background
  1. JMeter in English/

Native Functions of JMeter (Part 2)

Intermediate - This article is part of a series.
Part 12: This Article

funciones

Welcome to the second part of JMeter’s native functions. I hope this publication will conclude describing the functions that can support us in resolving complex logic for our testing requirements without more, leaving the table of functions we will cover in this publication:

funciones

4.- Programming Functions>

4.- Programming Functions #

This is probably the most controversial set of functions in JMeter because they are too broad and can lead to an infinite number of possibilities. We cannot delve into this, as there are countless ways to use them, but I would like to show some cases where these functions work well with IF and WHILE controllers.

The options for programming languages are: Beanshell, Groovy, JavaScript and JEXL. That’s why we can choose our preferred weapon to deal with any complex or simple logic. However, probably we will only use a couple of lines of code at most. Why? Because if you want to use more than that, I recommend you to better use the pre-processor, moustreador or post-processor.

Beanshell>

Beanshell #

Beanshell is an Java interpreter created 20 years ago and the last known update was released in 2003. Obviously, it is obsolete but still functional. I learned JMeter paired with Beanshell at the beginning of 2012 until 2017 when Groovy took over and its efficiency and speed quickly surpassed Beanshell, especially if the compilation code is cached. For more than 5 years, I used Beanshell to handle almost all logic requirements in flows, primarily as a preprocessor or postprocessor, rarely as a mule, since it did not like its effect on my results.

Groovy>

Groovy #

It may seem strange, but Groovy was released in the same year as the last stable version of Beanshell, which was launched in 2003. Since then, Groovy has become the most popular scripting language for JMeter. Some of its key advantages include:

  • It is an object-oriented language.
  • Compiles directly into bytecode as fast as Java code.
  • Caches the compiled code.
  • Maintains and updates constantly.

Today there is no place for comparison between Groovy and BeanShell because BeanShell has not been updated in more than 16 years. However, the only scenario where I would recommend using BeanShell would be when dealing with legacy scripts of JMeter that cannot be migrated to newer versions of JMeter. In such cases, I would recommend migrating to Groovy since it is faster and does not require changing much code; because Groovy recognizes almost all of Beanshell’s syntax.

JavaScript>

JavaScript #

JavaScript or ECMAScript is another interpreted language that can be used in JMeter. Basically, the IF and WHILE controllers evaluate the statement with this language to allow threads to continue, exit loops, or jump to some branches. Although it’s a great language, the JSR223 sampler only allows caching for Groovy, making JavaScript less efficient for large repetitions. In my opinion, it is better to use it for small evaluations but not for complex tasks.

JEXL>

JEXL #

JEXL stands for Java Expression Language. It is the most limited programming language but that’s exactly what makes it so popular; its simplicity. The code is small and fast, evaluating a boolean expression, and probably the most common use of JEXL in JMeter, although it can also be used in load models or evaluation statements.

examples of programming languages functions:

FunctionExampleExpected Result
groovy${__groovy(props.get(“START.MS”).substring(0\,2),)}Prints the substring 0-2 of the property START.MS
BeanShell${__BeanShell(123*456,)}Prints the result of multiplying 123x456
javaScript${__javaScript(new Date(),)}Prints a new date in extended format
jexl2${__jexl2(1 < 1,)}Prints the boolean result of evaluation: False
jexl3${__jexl2(1 >= 1,)}Prints the boolean result of evaluation: True

All programming languages have access to the resources of JMeter. The following table describes the relationship between resources and programming languages:

lenguajes

Popular Uses:

If you are using IF or WHILE controls, these controls require a statement based on a boolean value to decide whether to continue or discard, so the best options for this task would be JavaScript and JEXL, although we could use any. However, always be careful when selecting the option to evaluate the statement as an expression variable. Some popular uses would be:

if-while-controller

Here is an example of the WebTours script that uses a load model with IF Controllers. The script for JMeter with examples of using IF controller to model and distribute loads requires Java 1.8+, JMeter 5+ and JMeter plugins.

Why use If Controller instead of Throughput Controller?>

Why use If Controller instead of Throughput Controller? #

This is a good question, but answering it isn’t easy. Personally, I think the Throughput Controller performs many calculations to determine whether a part of the flow should or should not be executed, based on previous executions, counting iterations through properties and evaluations to correct values. This combined consumption of CPU and memory resources is much greater if there are multiple Throughput Controllers in the same script for modeling load. On the other hand, the If Controller will only perform an evaluation using JEXL to calculate and decide whether the generated percentage value by the random variable falls within the range or not. Additionally, while the Throughput Controller requires a base number of executions to correct the trend if you stop the test for any reason before expected, it is unable to correct itself. In contrast, with an If Controller at any time, the randomly generated numbers should either satisfy or not based on their uniform distribution.

funciones

5.- Property Functions>

5.- Property Functions #

In JMeter, properties are like variables but in a global environment. This means that all threads can access these values and also update them. Most of these “properties” are set from the .properties files located in the bin directory as user.properties and jmeter.properties. Remember to restart JMeter if you update some of these values.

Do not recommend modifying any parameter unless you have the precise knowledge of what you are doing, as it could negatively affect the behavior of JMeter and consequently its results. Although there is a latent danger in modifying these properties, the advantage of using these parameters is quite broad, one of the most common uses being configuring the number of threads, ramp-up or script duration, as we can appreciate in this article.

jmeter.sh -n -t TestPlan.jmx -Jthreads=100 -Jrampup=100 -Jduration=600 -l TestPlan.jtl

propiedades

Another great example is configuring objects globally, and I’m not just talking about strings, integers, or booleans. We can also configure matrices, vectors, queues, lists, and any type of data structure you might need for your test logic. I’ve been publishing articles on Grafana for a long time, where I was using a list to collect multiple results and send them to InfluxDB in one HTTP request, here you can consult the article.

Examples of property functions: properties threads: 1 rampup: 1 duration: 60

FunctionExampleExpected Result
isPropDefined${__isPropDefined(threads)}Prints the boolean value if property is defined, in this case true
property${__property(threads)}Prints the value of the property threads: 1
P${__P(duration)}Prints the value of the property: 60
P${__P(duration,90)}Updates the value of the property to 90
setProperty${__setProperty(propname,propvalue,)}Assigns a value to a property

Similarly, we include images of the examples and you can download below:

propiedades-setup

propiedades-response

The script of JMeter with examples of properties functions requires Java 1.8+, JMeter 5+, and JMeter plugins.

6.- Variables’ Functions>

6.- Variables’ Functions #

In JMeter, variables are simply local variables. This means they are created and destroyed during each thread iteration. Most of the data we use in a thread’s execution (such as correlations) is stored in properties that will disappear once the thread finishes and starts a new iteration. If you need to save any value, the best option is to store it in a property; however, values in properties also disappear when the test stops, so it’s better to save them in an archive.

Examples of variable functions: variables Test_1: Flood Test_2: io Vars: 12345|67890|10293848576|5647382910 Variable: Test_1 es “${Test_1}” y Test_2 es “${Test_2}” Numero: 2

FunctionExampleExpected Result
split${__split(${Vars},splitVar,|)}Divide the variable Vars with the delimiter pipe, and the results are stored in splitVar
eval${__eval(Test_1 es “${Test_1}” y Test_2 es “${Test_2}”)}Prints the string: Test_1 is “Flood” and Test_2 is “io”
evalVar${__evalVar(Variable)}Prints the evaluation of Variable: Test_1 is “Flood” and Test_2 is “io”
isVarDefined${__isVarDefined(Variable)}Prints the boolean value in case the variable is defined, in this case true
V${__V(Test_${Numero})}Prints the string: io that is the content of the variable Test_2
V${__V(splitVar_${Numero})}Prints the string: 67890 that is the content of the variable splitVar_2

Similarly, we include images of the examples and you can download it below:

vars-setup

vars-response

The script of JMeter with examples of variable functions requires Java 1.8+, JMeter 5+, and JMeter plugins.

7.- String Functions>

7.- String Functions #

It is very common in the environment of tests to modify and update string values. It is also possible to perform transformations, conversions, cuts or divisions of these same ones. Some of the most common uses are URL encoding and decoding with the functions urlencode and urldecode, both functions are really useful when dealing with web pages and publishing parameters. Although the string functions are destined for conversion, we have two functions that may not be as related to such tasks. For example, regexFunction, which is a regular expression extractor very similar to post-processing, and dateTimeConvert, which helps us transform different time formats.

Examples of String Functions:

FunctionExampleResult
char${__char(165)}Prints character 165: ¥
char${__char(182)}Prints character 182: ¶
changeCase${__changeCase(Mayusculas,UPPER,Tmp)}Changes case to uppercase and stores in variable Tmp
changeCase${__changeCase(Minusculas,LOWER,Tmp)}Changes case to lowercase and stores in variable Tmp
escapeOroRegexpChars${__escapeOroRegexpChars([0-9].+?,)}Prints the string: [0-9].+?
escapeXml${__escapeXml(“Valor”+&+’Value’)}Prints the string: "Valor"+&+?Value?
regexFunction${__regexFunction(min(.+),$1$,1,,,,Tmp)}Returns evaluation of regular expression in variable Tmp: sculas
escapeHtml${__escapeHtml(“Comillas Dobles”+‘sencillas’)}Prints the string: "Comillas Dobles"+‘sencillas’
unescapeHtml${__unescapeHtml("Comillas Dobles"+‘sencillas’)}Prints the string: “Comillas Dobles”+‘sencillas’
unescape${__unescape(\ttabulador)}Prints the string: tabulador
urlencode${__urldecode(%5Bflood%5D%3D%28element%29)}Prints the string: [flood]=(element)
urldecode${__urlencode([flood]=(element))}Prints the string: %5Bflood%5D%3D%28element%29
TestPlanName${__TestPlanName()}Prints the string: Strings-functions.jmx
dateTimeConvert${__dateTimeConvert(1589794881,,dd/MM/yyyy HH:mm,)}Prints the string: 18/05/2020 04:41

Similarly, we include images of the examples and you can download it below:

strings-setup

strings-response

The script of JMeter with examples of string functions requires Java 1.8+, JMeter 5+, and JMeter plugins.

Conclusion>

Conclusion #

The functions of JMeter have grown in number and value, although there are also extended functions that can be included using the Custom JMeter Functions addon and are extremely useful, especially Base64 when we need to calculate basic authentication headers. I did not cover these in this publication but it seems necessary to teach how to fish rather than just giving the fish, simply by installing the addons, selecting Custom JMeter Functions from the menu and restarting. I hope this information has been of help. Enjoy using JMeter’s functions!



Intermediate - This article is part of a series.
Part 12: This Article