Follow

Using JMeter Functions - Part I

So, what are functions in JMeter?

According to the user manual, JMeter functions are special values that can populate fields of any Sampler or other element in a test tree.  It is very useful tool to make your test plan flexible and maintainable.

Generally, function calls looks like this: ${__functionName(var1,var2,var3)}, where “__functionName” matches the name of function. Parentheses () surround the parameters sent to the function, e.g. ${__time(YMD)}.

The actual parameters vary from function to function. Functions that require no parameters can leave off the parentheses, e.g. ${__threadNum}. Some of the most useful functions in JMeter are: __log(), __BeanShell(), __RandomString().

It is important to note that these functions are case sensitive.

1.__log ()

Create short test-plan as displayed.


 

In the HTTP Request set “Server name or IP”=jmeter.apache.org and “Path”=/changes.html. Run this test plan to confirm that it is working. Now, log part of the URL. In this scenario, use __log() function. Rewrite the “Path” to the following variant - per the screenshot below.  


 

Activate the “Log Viewer” to view results. 

 

Execute the test plan again and view the logs.

 

Note that “/changes.html” was logged by JMeter and “/changes.html” was logged three times, twice before the ThreadGroup started and the third time – during the execution of the HTTP Request. In addition to the aforementioned functions, __log() has other parameters: log level and string message that can be thrown into the logs. The full syntax is  _log(var1,var2, var3), where:

  • var1 – String to be logged(and returned)
  • var2 – Log level(default INFO) or OUT or ERR
  • var3 – Throwable text(optional)

 

2.__BeanShell ()

The second interesting function that deserves attention is _BeanShell(). Full syntax is-BeanShell(var1,var2), where:

  • var1 - Expression to evaluate,
  • var2 – Name of variable in which to store the result (optional)

We will use the original example again, but it will be modified for use with _BeanShell(). Bean Shell – is scripting language with Java syntax. It supports objects and the majority of  in use Java features. But unlike Java, it doesn’t need compiling before running, it is executed line by line. And this is widely used in JMeter while solving non-trivial issues. 

For this purpose, a complex URL was used, http://jmeter.apache.org/api/index.html.
Insert jmeter.apache.org to “Server name or IP”. Then add it to the “Path” field.

${__BeanShell(b="/api";b.concat("/index.html"))}


 

The only issue with __BeanShell() is that the script concatenates two parts of the URL. After starting test, the following response will be visible in View Results Tree.


 

3._RandomString ()

Another useful function is __RandomString(). It can be used to test, for instance, the login form of web-sites with large amounts of invalid users. The HTTP Request below is modified. It it irrelevant that this particular request is invalid. The idea is simply to monitor a scenario to note how __RandomString() works.


 

Two parameters have been added to the POST request body; user and password.  Both parameters will be generated by __RandomString() with the following syntax:

  1. __RandomString(var1,var2,var3),where:
  • var1 - Random string length
  • var2 - Chars to use for random string generation
  • var3 - Name of variable in which to store the result (optional)

Results post test run? See below (Figure 8).


 

As expected, the HTTP Request was invalid and therefore it failed. But note that the POST data contains the parameters that were generated by __RandomString ().

 

4. Functions Helper

Our fourth and final items of the days is....drumroll please......Functions Helper! this is the newest feature of JMeter 2.6!


 

Simply select the functions you want to use from the drop-down list,  fill in the parameter values and press “Generate”. You will receive results similar to ${__RandomString(123,aldkjfhalskdfalkdfhalkdjfhaldjfhaldsjfhaldjfh,)}. This can be copy/pasted anywhere.

Want to know more about JMeter Functions? Read more in Part 2.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.