George Maksimenko is a Lead QA engineer at Adexin. He has 10 years of experience in software testing. His primary activities in software testing are automation testing and performance testing.

Learn JMeter in 5 Hours

Start Learning
Slack

Run massively scalable performance tests on web, mobile, and APIs

Mar 19 2018

How to Use the Custom JMeter Functions Plugin

Apache JMeter™ is a powerful tool for load testing. While most performance scripts are quite simple and can be easily created using JMeter’s standard features, it is also possible to create more complex scripts using the standard JMeter functions. But sometimes our scripts require features that are outside of JMeter’s standard functions. In this case we can install the Custom JMeter Functions plugin. This plugin extends JMeter’s functionality with fifteen useful functions:

 

  1. chooseRandom: chooses a random value from the list of arguments
  2. doubleSum: calculates the sum of floating point values
  3. env: receives an environment variable value
  4. isDefined: determines if the variable was already defined
  5. MD5: computes MD5 hash of string or variable value
  6. base64Encode: encodes a string or variable value with Base64 algorithm
  7. base64Decode: decodes a string or variable value from Base64 into string
  8. strLen: calculates the length of a constant string or variable value
  9. substring: returns a substring of a particular string
  10. strReplace: replaces a part of the string with another string
  11. strReplaceRegex: replaces all the substrings that satisfy the regular expression with the replacement string
  12. uppercase and lowercase: changes the string case or variable value case
  13. iterationNum: returns the number of current iterations in the thread group
  14. if: provides if syntax
  15. caseFormat: provides changing string case format

 

If you are new to installing JMeter plugins, please, follow this link to learn how to do it.

 

In this blog post we will take a look at the first seven functions listed above. We will use them in real scripts and see how they work. Next time, we will go over the remaining functions. Let’s get started!

 

1. chooseRandom function

 

This function chooses a random value from the list of arguments passed to the function, excluding the last one. The last argument will be used to store the result of the function.

 

Example:

 

${__chooseRandom(Paris,Philadelphia,Boston,Portland,departure)}

  • ‘Paris,Philadelphia,Boston,Portland’ are values to choose from;
  • ‘departure’ is a variable name to store the random value.

 

How to use:

 

Let’s use the BlazeDemo website to test our JMeter scripts. The landing page of this website has a form for searching for flights.

 

using jmeter functions

 

After searching for flights we will see a list of available flights. Let’s call this the Flights page.

 

adding functions to your load script

 

This is what our Search request looks in JMeter. See the screenshot below.

 

performance testing with functions

 

As you can see we have parameterized the departure and destination cities using the chooseRandom function. This function will return a random value and save it to a variable.

 

Let’s add an assertion to verify that the Flights page is presented after the Search request. In the screenshot above we see that the Flights page contains text ‘Flights from Philadelphia to New York:’. We will use this text for validating the page.

 

using the custom jmeter functions plugin

 

The Pattern in this assertion contains variables that were defined in the chooseRandom function above.

 

So, let’s run the script and see how it works.

 

add functions to your jmeter script

 

The request contains the fromPort and toPort parameters with random values from the parameter lists of the chooseRandom functions.

 

use functions in your jmeter script

 

Also we see that the Flights page is loaded correctly and the assertions worked fine.

 

2. doubleSum function

 

This function calculates the sum of float values. This could mean summing up two or more values. As with the previous function, this function stores the result in a variable. The last argument passed to the function is the variable to store the result.

 

Example:


${__doubleSum(3.3, 4.3, 5.4, totalCost)}

  • ‘3.3, 4.3, 5.4’ are numbers to sum;
  • ‘totalCost’ is a variable to store result of sum.

 

How to use:

 

The purchase page of the BlazeDemo website contains the Price, Taxes and Total Cost values, where Total Cost is the sum of Price and Taxes. Let’s use this case to demo the doubleSum function.

 

a guide for adding functions to your jmeter script

 

First of all we need to extract the Price and Taxes values from the Purchase page.

 

how to use functions when running your load test script

 

This Regular Expression Extractor element extracts the Price value.

 

extending your test capabilities with jmeter functions

 

This Regular Expression Extractor element extracts the Taxes value.

 

using functions in your performance test script

 

The pattern in this assertion verifies the Total Cost label and value on the Purchase page.

 

This function will sum the ${price} and ${taxes} values, save it to the variable totalCost and return the totalCost variable as a result of its work.

 

functions in jmeter script

 

This is an example of how to use the saved variable in future steps.

 

3. Env function

 

This function returns an environment variable value or returns a default value if the environment variable wasn’t defined.

 

Example:
 

${__env(HOME, homeVariable, defaultValue)}

  • HOME is the name of the environment variable;
  • homeVariable is the name of the JMeter variable to store the value of the environment variable;
  • defaultValue is the default value that is returned if the environment variable wasn’t defined.

 

How to use:

 

For demo purposes let’s send the value of the HOME environment variable as a Landing Page request parameter.

 

jmeter functions

 

This request will send the value of the HOME variable as the ‘env_var’ parameter.

 

Run the request and see how it works.

 

learning to add functions with the customer jmeter function plugin

 

The landing page request has an ‘evn_var’ parameter with ‘/Users/gmaksimenko’ as its value.

 

Let’s see how the default value works. Update the name of the variable from HOME to EHOME.

 

updating values with jmeter functions

 

The EHOME environment variable isn’t defined, so we expect to see the default value.

 

Run the script and see results.

 

jmeter functions updating variables

 

The Landing page request has ‘evn_var’ parameter with ‘defaultValue’ as its value.

 

4. isDefined function

 

This function determines if a variable was already defined. It returns 1 if the variable was defined and 0 if it wasn’t.

 

Example:

 

${__isDefined(flight)}

  • ‘flight’ is the variable name to check.

 

How to use:

 

Let’s use the Regular Expression Extractor to define the variable ‘flight’.

 

guide to jmeter functions

 

This extractor gets the flight number from the Flights page.

 

Let’s use the if controller to demo the isDefined function. The script will run the Purchase request only if the ‘flight’ variable is defined. See screenshot below.

 

how do i add jmeter functions to my script?

 

The if controller has a condition that will return true if the ‘flight’ variable is defined.

 

Run the script and see how it works.

 

working with jmeter scripts and adding jmeter functions

 

The purchase request was run. This means that the ‘flight’ variable was defined.

 

5. MD5 function

 

This function computes an MD5 hash of a string or variable value. The MD5 algorithm (Message Digest 5 Algorithm) is a cryptographic function producing a 128-bit hash value. The last argument of the function is the name of a variable to store the result.

 

Example:

 


${__MD5(test_string,encodeVar)}

  • ‘test_string’ is a string to calculate hash;
  • ‘encodeVar’ is a variable to store the MD5 hash.

 

How to use:

 

Let’s send an MD5 hash of a test string for demo purposes as a parameter of the Landing Page request.

 

how to add functions to your jmeter script

 

‘secret’ is the name of the parameter. ‘secret_string’ is the source string to get the hash from. The MD5 function will return a hash as a result of its work and save it to the ‘secretVar’ variable.

 

jmeter functions tutorial

 

This is an example of how to use the ‘secretVar’ variable. The search request will send the value of the hash in the parameter ‘secret’.

 

Run the script and see how it works.

 

custom functions plugin on jmeter - how to use

 

The Landing page request has an MD5 hash as the value of the ‘secret’ parameter.

 

looking at how to add functions to your jmeter script

 

The Search request also has an MD5 hash as the value of the ‘secret’ parameter.

 

6 and 7. base64Encode and base64Decode functions

 

The base64Encode function encodes a string or variable value with a Base64 algorithm. Base64 is a standard for encoding binary data with only 64 ASCII characters. The last argument is a variable to store the encoded result.

 

Example:

 

${__base64Encode(test_string,encodeVar)}

  • ‘test_string’ is a string to encode;
  • ‘encodeVar’ is a variable to store encoded value.

 

The base64Decode function decodes a string or variable value from Base64 into a string. The last argument is a variable to store the decoded string.

 

Example:


${__base64Decode(${encodeVar},decodeVar)}

  • ‘${encodeVar}’ is a variable value to encode;
  • ‘decodeVar’ is a variable to store decoded value.

 

How to use:

 

Let’s encode the string: ‘test_string’ and send it as a parameter to the Landing page request.

 

how to use jmeter functions

 

base64Encode will encode ‘test_string’, save it to the ‘encodeVar’ variable and return the encoded value as the result.

 

adding to performance testing capabilities with functions

 

The above example shows how to use the ‘encodeVar’ variable. The encoded value will be sent in the ‘encoded_string’ parameter of the Search request.

 

Let’s try the base64Decode function and decode the value of the ‘encodeVar’ variable back to a string.

 

all jmeter functions

 

This request will send a get request to the Login page of the BlazeDemo website. base64Decode will decode the value of ‘${encodeVar}’, return it and save it to ‘decodeVar’.

 

So now let’s run the script and see how it works.

 

learning jmeter functions

 

The Landing page request has an ‘encoded_string’ parameter with the encoded value of ‘test_string’.

 

paramaters, variables, jmeter functions

 

The Search request also has an ‘encoded_string’ parameter with the encoded value.

 

encoded values in jmeter functions

 

The Login page request has a ‘decoded_string’ parameter with ‘test_string’ as its value. As you can see from this example the original string was successfully decoded.

 

That’s it for now. We discussed 7 useful functions in this blog post. If you found this useful be sure to check out the second part of this blog post where we will take a look at the next 8 functions available in the Custom JMeter Functions plugin. Stay updated by joining our newsletter or signing up for the JMeter Slack group.

 

After creating all of your test scripts with these functions, you can scale your JMeter test in BlazeMeter. You will get:

  • Simple Scalability – It’s easy to create large-scale JMeter tests. You can run far larger loads far more easily with BlazeMeter than you could with an in-house lab.
  • Rapid-Start Deployment – BlazeMeter’s recorder helps you get started with JMeter right away, and BlazeMeter also provides complete tutorials and tips.
  • Web-Based Interactive Reports – You can easily share results across distributed teams and overcome the limitations of JMeter’s standalone UI.
  • Built-In Intelligence – The BlazeMeter Cloud provides on-demand geographic distribution of load generation, including built-in CDN-aware testing.

 

So request a demo, or put your URL in the box below and your test will start in minutes.

Interested in writing for our Blog?Send us a pitch!

Your email is required to complete the test. If you proceed, your test will be aborted.