Venkat Matta

it's all about the Performance Testing

Jmeter Functions

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.

func1.jpg

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.

func2

Activate the “Log Viewer” to view results.

func3

Execute the test plan again and view the logs.

func4

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.

func5

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.

func6

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.

func7

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 image

func8

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 or later

func9

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.

5.__property()

One of the most interesting files of JMeter is jmeter.properties. It contains all major properties of JMeter which are important in testing. You can find this file in %JMETER_HOME_DIR/bin directory. There are numerous properties, common and more specific. Many of their values can be helpful during testing, right at runtime. To access property values we can use $__property() function.

Per the JMeter user guide  __property(): The property function returns the value of a JMeter property. If the property value cannot be found, and no default has been supplied, it returns the property name. When supplying a default value, there is no need to provide a function name – the parameter can be set to null, and it will be ignored.

For example:

  • ${__property(user.dir)} – return value of user.dir
  • ${__property(user.dir,UDIR)} – return value of user.dir and save in UDIR
  • ${__property(abcd,ABCD,atod)} – return value of property abcd (or “atod” if not defined) and save in ABCD
  • ${__property(abcd,,atod)} – return value of property abcd (or “atod” if not defined) but don’t save it
  • Parameters
  • Attribute
  • Description
  • Required
  • Property Name The property name to be retrieved Yes
  • Variable Name A reference name for reusing the value computed by this function No
  • Default Value The default value for the property No

Let’s envision a scenario requiring a decision over whether or not to execute a specific sampler, depending on current log level (see example using $_property().
Get the “log_level.jmeter” value; Compare it with pre-defined value in If Controller; Execute (or do not execute) some sampler from the If Controller.

How it can be implemented?

Create a simple test plan that contain one Thread Group. The Thread Group has one If Controller with one HTTP Request Sampler in it.

fun10

In this scenario, it does not matter what URL we put into the HTTP Request, so switch to If Controller to see what happens now.
func11

he sampler here should be executed only in the case of the log_level.jmeter equals “INFO”. In this case, we added the following javascript condition in the “Condition” field: “${__property(log_level.jmeter)}”==”INFO”.

You can see from the figures above that the log_level.jmeter=INFO. And, as expected, HTTP Request was executed. If switched back to the If Controller, we change “INFO” to “DEBUG” without changing jmeter.properties and then re-execute the same testing scenario.

func14

As expected, nothing is executed.
Of course, this test plan is a bit extreme as there are very few scenarios where one might run HTTP Requests depending on log_level.jmeter value. But if you understand how it works, you will be able to adapt it to your own testing needs.

6.__counter()

Per the JMeter user guide: __counter()
The counter generates a new number each time it is called, starting with 1 and increasing incrementally by one. The counter can be configured to keep each simulated user’s values separate, or to use the same counter for all users. If each user’s values have separate incrementations, that is similar to counting the number of iterations through the test plan. A global counter counts how many times that request was run.
The counter uses an integer variable to hold the count, which therefore has a maximum of 2,147,483,647.
The counter function instances are now completely independent. [JMeter 2.1.1 and earlier versions used a fixed thread variable to keep track of the per user count, so multiple counter functions operated on the same value.] The global counter – “FALSE” – is maintained separately by each counter instance.
Multiple __counter function calls in the same iteration won’t increase the value further.
If you want to have an incremental count for each sample, use the function in a Pre-Processor such as User Parameters .

  • Parameters
  • Attribute
  • Description
  • Required

7.__Random

The random function returns a random number that lies between the given minimum and maximum values.

  • Parameters
  • Attribute Description Required
  • Minimum value A number Yes
  • Maximum value A bigger number Yes
  • Variable Name A reference name for reusing the value computed by this function. No

8.__threadNum

The thread number function simply returns the number of the thread currently being executed. These numbers are independent of ThreadGroup, meaning thread #1 in one threadgroup is indistinguishable from thread #1 in another threadgroup, from the point of view of this function.
There are no arguments for this function.

9.__javaScript()

The javaScript function executes a piece of JavaScript (not Java!) code and returns its value.

JMeter uses the Mozilla Rhino implementation of JavaScript,  for detailes of language seeMozilla Rhino Overview. In this particular scenario, we use this function for two goals:

  • calculating different expressions;
  • getting access to JMeter java classes for data, that cannot be accessed via standard GUI element.
  • Start by calculating and using the same testing scenario as at the beginning of article. Simply modify the HTTP Request for use with __javascript().

func15

As you remember, ${__counter (FALSE)} generates a unique value from thread to thread. But inside of one thread this value remains the same. We can fix this issue with using  __javascript(). Multiply the password value by 2 and add 5 more to the user value.  Results are in….

func16

As expected, the password and user received different values that were calculated from 1.

 

Advertisements

Information

This entry was posted on March 23, 2016 by in Jmeter.
%d bloggers like this: