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

Apr 01 2018

How to Use the Custom JMeter Functions Plugin - Part 2

Welcome to part two of our blog post about the Custom JMeter functions plugin. The Custom JMeter functions plugin provides functions that are not available in Apache JMeter™ out of the box but could be very useful for your load testing scripts. In part one we took a look at the first seven functions in the list below. In this blog post we will take a look at the remaining eight functions listed below. If you missed part one be sure to check it out here!

 

  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 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 iteration in thread group
  14. if: provides if syntax
  15. caseFormat: provides changing string case format

 

For official documentation on the plugin you can head on over to their official page: Custom JMeter functions plugin. If you need help installing plugins you can find that information here: How to Install the JMeter Plugins Manager.

 

During the investigation of these functions I found that two of them don’t work. If you know otherwise feel free to share in the comments section. I used version 2.0 of Custom JMeter function plugin. Let’s get started!

 

We will use the BlazeDemo website to create demo JMeter scripts, this site is available to the public for trying out performance scripts.

 

8. strLen function

 

This function calculates string length or variable value length. The last argument passed to the function is the variable to store the result.

 

Example:


${__strLen(test_string, lenVar)}

  • test_string is a string to calculate length
  • lenVar is a variable name to store the length

 

How to use:

 

using jmeter functions

 

This If Controller will execute the Purchase request if the flight number length exceeds 2 digits. strLen will return the length of the ‘flight’ variable and save it to the ‘flightLen’ variable.

 

using jmeter customer jmeter functions plugin

 

The Purchase request is an example of how to use the ‘flightLen’ variable. The value of this variable will be sent with the ‘flight_length’ parameter.

 

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

 

jmeter functions tutorial

 

The Purchase request was run. This means that the length of the ‘flight’ variable satisfied the condition in our If Controller and is more than 2 characters. We can see it in the request parameters of this request ‘flight=234&flight_length=3’.

 

9. substring function

 

This function extracts a part of a string with another string. Actually this function is a wrapper for the Java String.substring method. The last argument is a variable that is used to save the result of the function.

 

Example:


${__substring(test blaze string, 5, 10, substringVar)}

  • ‘test blaze string’ is a string to take the substring from
  • ‘5’ is the index of the character to start from. Index starts from 0
  • ‘10’ is the index of the character to end on. Index starts from 0
  • ‘substringVar’ is a variable to store the substring

 

How to use:

 

Let’s demonstrate the substring function with a Landing page request.

 

demonstrating custom jmeter functions plugin

 

The function on the screenshot above will extract the substring ‘blaze’, send it as a value of the ‘substring’ parameter and save it to the ‘substringVar’ variable.

 

jmeter function guide

 

Here is an example of how to use the variable ‘substringVar’. The search request will send its value as a ‘substring’ parameter.

 

Run the script and see how it works.

 

jmeter functions step by step

 

The Landing page request has the parameter ‘substring’ with value ‘blaze’.

 

jmeter plugins how to use

 

The Search request also has parameter ‘substring’ with value ‘blaze’. So we can draw the conclusion that the substring function works fine.

 

10. strReplace function

 

This function replaces a part of a string with another string. This is a wrapper for the Java String.replace method. The last argument passed to the function is the variable to store the result. The function will return ‘string’ as is if a substring for replacement is not be found.

 

Example:


${__strReplace(superblazedemo,blaze,meter,replaceVar)}

  • superblazedemo’ is a string in which replacement will take place
  • blaze’ is a substring to be replaced
  • meter’ is the replacement string
  • replaceVar’ is a variable to save result string

 

How to use:

 

Let’s demo the strReplace function by executing it to compose the value for the ‘replace’ parameter of a Landing page request.

 

demoing jmeter functions plugins

 

This function will replace the substring ‘blaze’ with the string ‘meter’, return the updated string and save it to the ‘replaceVar’ variable.

 

jmeter custom functions plugin demonstration

 

The above request shows how to use the ‘replaceVar’ variable. The result string will be sent in the ‘replace’ parameter of the Search request.

 

Run the script and see how it works.

 

learning jmeter functions

 

The Landing page request has a ‘replace’ parameter with value ’supermeterdemo’.

 

jmeter custom functions plugin example

 

The Search request also has a ‘replace’ parameter with value ’supermeterdemo’. So we can see that the useful strReplace function works fine.

 

11. strReplaceRegex function

 

This function will replace all the substrings that satisfy the regular expression with the replacement string. strReplaceRegex function is a wrapper for the Java String.replaceAll method. If the substring is not found, the function will return the original string as is.

 

NOTE: the strReplaceRegex function doesn’t seem to work, at least for me. I used version 2.0 of the Custom JMeter Functions plugin.

 

Example:
 

${__strReplaceRegex(super blaze demo,' ',-,replaceVar)}

  • super blaze demo’ is a string to apply replacements
  • ‘ ’ is a regular expression to search
  • -’ is a replacement string
  • replaceVar’ is a variable to save result string

 

How to use:

 

Let’s try to use this function to generate a value for the ‘replace_regex’ parameter of a Landing page request.

 

generate a value with jmeter custom functions plugin

 

strReplaceRegex should replace all spaces with dash symbols, return the updated string and store it to the ‘replaceVar’ variable.

 

Now let’s see how it works.

 

information about jmeter plugins

 

The Landing page request has a ‘replace_regex’ parameter with value ‘${__strReplaceRegex(super blaze demo,' ',-,replaceVar)}’. This shows that the function didn’t work. Let me know in the comments if it works for you.

 

12. uppercase and lowercase functions

 

The uppercase function transforms the case of a string or variable value to uppercase.

 

Example:
 

${__uppercase(Blazemeter, upperVariable)}

  • ‘Blazemeter’ is a string to apply uppercase
  • ‘upperVariable’ is a variable to save result string

 

The lowercase function transforms the case of a string or variable value to lowercase.

 

Example:


${__lowercase(Blazemeter, lowerVariable)}

  • Blazemeter’ is a string to apply lowercase
  • lowerVariable’ is a variable to save result string

 

How to use:

 

Let’s use the Landing page request to demonstrate how the uppercase and lowercase functions work.

 

demonstrating upper case and lower case functions in jmeter

 

The ‘uppercase’ parameter will have string in uppercase and the same string will be saved as ‘upperVariable’ variable. The ‘lowercase’ parameter will have a string in lowercase and the same string will be saved as ‘lowerVaraible’ variable.

 

jmeter custom functions plugin information

 

Here is an example showing how to use the ‘upperVariable’  and ‘lowerVariable’ variables.

 

Let’s run the script.

 

running a jmeter functions script

 

The Landing page request has an ‘uppercase’ parameter with the value ‘BLAZEMETER’ and a ‘lowercase’ parameter with the value ‘blazemeter’.

 

jmeter functions test results

 

The Search request also has both parameters with correct values. So it works fine!

 

13. iterationNum function

 

This function returns the number of the current iteration in a thread group. This function has no parameters.

 

Example:


${__iterationNum}

 

How to use:

 

Let’s increase the number of iterations (loop count) to 10 in the Thread Group and use the If Controller to filter half of the Search requests. This could be useful if you want to emulate behavior when not all visitors have moved on to the next step.

 

using jmeter custom functions plugin tutorial open source

 

This condition will return true for each even iteration number.

 

So run the script and check the results.

 

load tests results for jmeter functions

 

We see ten requests to the Landing page and only five to the Search. Great! The iterationNum function works fine.

 

14. If function

 

This function provides if (a == b ? c : d) syntax.

 

Example:


${__if(${a},${b},equal,different)}

  • ${a} is a variable with the actual value
  • ${b} is a variable with the expected value
  • equal is a value to return if the condition is true
  • different is a value to return if the condition is false

 

How to use:

 

Let’s parametrize the price value of the Purchase request to demonstrate the if function.

 

how to use custom jmeter functions

 

The ‘price’ parameter will have the value ‘333’ if the flight number is equal to 234 and ‘999’ if it isn’t.

 

Run the script.

 

which jmeter function should i choose?

 

The Purchase request has the price value ‘333’. This means that the condition returned true.

 

questions and answers about jmeter plugins

 

Update the expected value from ‘234’ to ‘43’ and run the script again.

 

custom jmeter functions plugin q and a

 

The Purchase request now has s price value of ’999’. This means the condition returned false.

 

As you can see the if function works well.

 

15. caseFormat function

 

This function should allow you to change the case format of a string.

 

Example:


${__caseFormat("blazeMeter", "UPPER_CAMEL_CASE", caseVariable)}

  • "blazeMeter" is a string to apply the transformation to
  • "UPPER_CAMEL_CASE" is the expected case format. To find a list of available case formats click here
  • caseVariable is a variable to store updated string.

 

NOTE: The caseFormat function doesn’t seem to work. I used version 2.0 of the Custom JMeter Functions plugin.

 

How to use:

 

Let’s try to demo this function with the Landing page request. Let’s generate the value of ‘case_format’ parameter using the caseFormat function.

 

custom jmeter functions plugin qna

 

The function in this request should change the case of ‘blazeMeter’, return it and store it to the ‘caseVariable’ variable.

 

So let’s see what happens.

 

load testing with jmeter functions

 

Parameter ‘case_format’ has the value ‘${__caseFormat("blazeMeter", "UPPER_CAMEL_CASE", caseVariable)}’. This means that caseFormat didn’t work.

 

I found one interesting fact during my investigation as to why this function doesn’t work. It appears that the jar file for version 2.0 of the plugin doesn’t contain the CaseFormat.class. See screenshot below.

 

performance testing with jmeter functions

 

That’s it for this post. We have researched all of the functions from the Custom JMeter Functions plugin and found that 2 of them don’t work. All other functions could be used in your awesome scripts. For part one of this blog post click here.

 

I hope this was useful for you. Thanks for reading. If you want to learn more advanced JMeter you can attend our free JMeter academy.

 

To try out BlazeMeter and enhance your JMeter scripts with scalability, reporting and collaboration, 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.