Skip to content

Functions Usage

Since OctoPerf operates JMeter in the background, it is also possible to use JMeter functions. Functions are shortcuts you can use to avoid adding a script block into your virtual user. The same result can be achieved programmatically but they make life easier by handling a lof of common situations.

Timestamp

When a requests sends a timestamp in milliseconds, you can just replace it with: ${__time()} This way, the current timestamp will be send every time instead of the recorded/original value. It is also possible to get a timestamp in seconds using: ${__time(/1000)} Or many other date formats, be sure to check its documentation page for more details.

CSV Read

When the CSV variables are not enough for your requirements you can manually parse a CSV file using functions:

${__CSVRead(path/name.csv,0)}

Check out our example on the CSV variable page for more details.

Counter

This is meant to implement a counter that can be shared between all users running. It will only increment when moving to the next iteration/loop of each user. Which means you can use it several times in a virtual user and its value remains consistent (at least until you get to the next iteration). It can be used this way: ${__counter}

More details can be found on its documentation page here.

Random

A simple random function with a min and max value: ${__Random(0,10)}. You can store the random value in a variable for further use: ${__Random(0,10, variable)}.

Other random functions include the random date: ${__RandomDate()} and the random string (with length): ${__RandomString(5)}

Properties

Properties are values shared among all users running inside JMeter. The value of property propertyName can be used like this: ${__P(propertyName)}.

This can be used in combination with the runtime properties to control your scripts in real time while the test is running.

Nested variables

The ${...} syntax used for variables does not allow nested variables (otherwise only one of them will be replaced). When this happens we must use the __V function.

${__V(categories_${counter})}

We already have a detailed example of a situation where this can be useful on the while page.

Quick script

It is sometime required to use a scripting language to deal with unusual behaviors. Although it is possible to write JSR samplers/pre/postprocessors it is sometimes easier to just go for a quick one line script just where it is required. You can call it by using the language name like this:

${__javaScript(...)} or ${__groovy(...)}

It can be as easy as a simple condition for your while/if:

${__groovy(${count}<=2)}

Or you could compute a value:

${__groovy(vars.get("myVar").substring(0\,2))}

Info

If you make use of groovy, avoid using the ${...} syntax for variables. Otherwise since the result may differ on each execution your script cannot be cached and may result in poor performance.

Encoding

Sometimes strings must be encoded to be send over the network. Typically in a form-urlencoded post for instance. A good way to take care of that is to use the __urlencode function.

For example ${__urlencode(string th@t requires "encoding")} will return string+th%40t+requires+%22encoding%22

Note how the spaces were encoded as +, if you prefer to have them encoded as %20 you can use another function:

${__javaScript(encodeURIComponent("${query}"),)}

Our correlation rules will use this function when encoding is set to Url encode.

Info

__urldecode will do the exact opposite if you need to put the string back in a more readable format.