Dmitri Tikhanski is a Contributing Writer to the BlazeMeter blog.

Learn JMeter in 5 Hours

Start Learning
Slack

Test Your Website Performance NOW!

arrow Please enter a valid URL

How to Use JMeter Functions - Part III

This is the third post in our series on “Using JMeter Functions”. Missed the last two? Take a look at Using JMeter Functions - Part I and Using JMeter Functions - Part II.

 

Today we’re going to explore and explain five more JMeter Functions - each followed by real-life use cases.

 

1. __time

 

Sometimes business use cases tested with Apache JMeter require the provision of the date and/or time (i.e. when simulating date or time picker events, providing a unique parameter, or prefixing result files). If you’re familiar with any these options, then you’re in the right place!

 

The __time function uses the SimpleDateFormat Java SDK class implementation. See the JavaDoc page for patterns and examples.

 

Bear in mind that a “short” or “long” date can be controlled via pattern repetition. For example:

 

Pattern

Outcome

E (or EE or EEE)

Sun

EEEE

Sunday

M

1

MM

01

MMM

Jan

MMMM

January

 

 

2. __StringFromFile

 

The parametrization of JMeter tests is often required. JMeter offers CSV Data Set Config which covers the majority of use cases -  but is currently limited to one file only. The __StringFromFile function allows you to read external data from multiple input files.

 

This function always reads the next line from file(s) specified. When the end of the file (EOF) is reached, the file is rotated - whether the function is hit by the next thread or the same thread on the next iteration. The result can either be used ‘ad-hoc’ or stored as a variable.

 

When using a > 1 file and a combination of multiple threads and loops, the total number of threads * iterations must not be more than the total number of records in all files. If it goes over, you will get an “End of Sequence” error.

 

When you’re providing multiple files and you can’t change their names (i.e. datafile0001, datafile0002, datafile0003…. datafile9999), you might want to check out the DecimalFormat class documentation to see the correct way to tell JMeter which files to read.

 

BlazeMeter naturally supports the  __StringFromFile function -  as well as all built-in features and extensions. You just need to make sure that all the files are provided via Upload Files tab - and that their names match Blazemeter conventions. That’s all! You’ll then be able to access any file in the __ScriptFromFile function just by giving its name.

 

3. __FileToString

 

Another useful function for parametrization is __FileToString. As its name suggests, this function reads the entire file every time it is called - and stores it in a JMeter variable when data is inserted in the Variable Name parameter. 

 

One example use case is the JMS Point-to-Point Sampler, in which the “Content” section doesn’t allow the payload to be  >10 lines. If you need to provide more than 10 lines of data, the  __FileToString function (or a relevant variable) can be placed right there. See a step-by-step example below:

 

Assume the simple text file “payload.txt” with the following contents:

 

Row1

Row2

Row3

Row15

 

Next, the JMS Point-to-Point sampler configuration:

 

 

As you can see in the ‘View Results Tree Listener’, the entire 15 line file is sent along with the request - even though the GUI only allows a maximum of 10 lines. 

 

 

Now specify the “File Encoding” property. We recommend that you keep all files in UTF-8 encoding as it is the default of the modern world - both in general and within BlazeMeter’s specific systems.  If all your files are in UTF-8, you don’t need to worry about this setting. However, if your use case reads and provides the file in a different encoding, you should consider setting this field to avoid taking the risk that JMeter will send unpredictable data instead of the actual file’s contents.

 

Just like the __StringFromFile function,  BlazeMeter’s systems fully support the __FileToString function...as long as the file is uploaded in the Upload Files section.

 

4. __UUID

 

The JMeter __UUID function ensures that the Type 4 UUID (also known as GUID) structure will always be returned.

 

Type 4 UUID stands for the 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated. It consists of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits. The following example UUID shows the groupings of hexadecimal digits in an UUID:

 

6B29FC40-CA47-1067-B31D-00DD010662DA

 

 

Type 1 UUID is time-based, Type 2 UUID is name-based and Type 4 UUID is randomly generated. So anywhere a random GUID structure is required, feel free to use the __UUID function. It will guarantee the structure will have a unique nature, as in the example below:

 

([A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12})

 

The following screenshots show five random UUIDs which have been generated and added into the error log stream - where they are highlighted red to ensure you won’t miss them.

 

 

Notice the reference to a UUID function being passed as an argument to a Beanshell script. A similar approach can be applied to all scripting extension samplers. These arguments can be referred in script as two distinct Beanshell entities: as single string “Parameters” or string array “bsh.args” - which automatically split arguments by spaces. Look at the jmeter.log file and you’ll see FIVE ERROR messages containing generated UUIDs.

 

 

 5. __intSum

 

The __intSum function is trivial but it can be very useful for instances when the sum of two or more integers (increment or decrement) is required.

 

Examples:

 

  • ${__intSum(1,5,)} - will return 6
  • ${__intSum(1,5,8)} - will return 14
  • ${__intSum(1,5,8,SUM)} - will return 14 and store it to SUM variable
  • ${__intSum(10,-5)} - will return 5
  • ${__intSum(${A},${B})} - will return an evaluation of A and B variables integer representation sum, which can be handy for Counter value processing.

 

Be aware that the maximum value which can be effectively used is 2147483647 and the minimum is -2147483648. If you need more, you might want to consider switching to the __longSum function - which is exactly the same but operates java.lang.Long rather than java.lang.Integer. If you need to operate even bigger numbers, you can always switch to Beanshell and use the BigInteger class from there. 

 

I hope that you found this useful! Want to find out more? Sign up for our free JMeter Online training course. Full details here

     
arrow Please enter a valid URL

You might also find these useful:

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