Sergey Horban is a QA Engineer with 3+ years of experience in automation and manual testing, bug-tracking and test management.

Learn JMeter in 5 Hours

Start Learning
Slack

Run massively scalable performance tests on web, mobile, and APIs

Nov 21 2017

How to Perform Arithmetic Operations on Numeric Variables When Load Testing

When we are performing load or functional tests, we often need to conduct arithmetic operations on our variables. Imagine for example that we are testing an API that returns three parameters with a numeric value. One of the parameters is the sum of the other two. To verify that the API performs the addition operation correctly, we need to perform the addition operation ourselves and compare it to the value returned by the API.

 

To perform arithmetic operations on variables of a numeric data type (these are int, long, float, double, short, byte), we use the arithmetic characters shown in the table below.

 

Arithmetic Operations Description
- Subtraction
+ Addition
* Multiplication
/ Division
%
The remainder of a division
Ex: 22%7=1
In testing, this operation is rarely used, but it is necessary to know and understand its meaning.
Arithmetic Operations with an Assignment Description
+= Addition operation with assignment (explained below)
-+ Subtraction operation with assignment
*= Multiplication operation with assignment
/= Division operation with assignment
%= Remainder of division operation with assignment
Incrementing and Decrementing Operations Description
++ Increment (increase by 1)
-- Decrement (decrease by 1)

 

Arithmetic Operations with Assignments

 

In arithmetic operations with an assignment, a joint operation is performed: first adding/subtracting/etc., and then assigning the result to the variable. These constructions are quite common when using the logic control operators "while" and "for".

 

Let’s look at an example.

 

The expression a + = 4, is equivalent to the following expression a = a + 4.  If in the code we replace a + = 4 by a = a + 4, then we get the identical result. The table below shows the result of the + = operator at each step, if we were using a “for” loop.

 

The steps in the “for”  loop Calculation The final value of the variable "a"
before the start of the “for” loop a=0 0
0 step a=0+4 4
1 step a=4+4 8
2 step a=8+4 12
3 step a=12+4 16
4 step a=16+4 20
5 step a=20+4 24
6 step a=24+4 28
7 step a=28+4 32
8 step a=32+4 36
9 step a=36+4 40

 

Implementing Arithmetic Operations in JMeter

 

In order to implement the execution of arithmetic operations in Apache JMeter™, it is necessary to do the following.

 

1. Add a Thread Group

 

Test Plan -> Add -> Threads (Users) -> Thread Group

 

implementing arithmetic operations in jmeter

 

2. Add a JSR223 Sampler

 

Thread Group -> Add -> Sampler -> JSR223 Sampler

 

arithmetic operations in numeric variables

 

3. Choose the language: Java (BeanShell 2.0b5/BeanShell Engine 1.0)

 

jmeter, numeric variables, arithmetic operations

 

4. Open the JMeter console. The JMeter console will be used to show the results of performing arithmetic operations.

 

load testing with arithmetic in test scripts

 

5. In the JSR223 Sampler, add your code. To start out, use the following code example.

 

Example code

 

setStrictJava (true);

int a = 8; // Creating a Variable
int b = 2; // Creating a Variable

int c = a + b; // The addition of two variables
int e = a - b; // The operation of subtracting two variables
int d = a * b; // The operation of multiplying two variables
int f = a / b; // The operation of division two variables
int g = a % b; // Operation of obtaining the remainder from the division


log.info(c + " - Addition Operation");
log.info(e + " - Subtraction operation");
log.info(d + " - Multiplication operation");
log.info(f + " - Operation of addition");
log.info(g + " - Operation of obtaining the remainder from division");

 

log.info - This is a method that outputs information to the JMeter console. In the code example above, the console displays the value of the variable . When writing large tests, it’s valuable to output error messages to the console, to see if an error occurs at some step. This significantly reduces the time to find out why the error occurred.

 

JMeter mathematical function

 

In JMeter, dividing a number by 0 causes an error, as you can see in the screenshot below:

 

Example code

 

setStrictJava (true);

int a = 8;
int b = 0; 

int c = a / b; 

log.info(c + "");

 

JMeter - User - How to do arithmetic operations in Beanshell

 

Now let’s look at a more advanced example.

 

Implementing Arithmetic Operations with Assignment and Incrementing in JMeter

 

When writing autotests, arithmetic operations with assignments are used quite often. These operations reduce the amount of written code, and also calculate in Java faster.

 

Example code

 

setStrictJava (true);

int a = 0;

for (int i = 0; i < 10; i++) {
				
	a += 4;
		
	log.info(a + "");
}

 

How to execute basic operations(Addition, subtraction, multiplication, division) in jmeter

 

The sample code demonstrates the operation of addition with assignment simultaneously. The increment operation is used. Let's consider this code in more detail.

 

  • int a = 0 - Creating a Variable;
  • for (int i = 0; i < 10; i++) - a cycle with a condition that repeats the assignment 10 times, adding 1 to i each time.
  • int i = 0 - This is the initial value for calculating the number of steps in the cycle
  • i < 10 - This condition, upon which the cycle will stop
  • i++ - An incrementing operation that increments the value of the variable by 1.
  • Log.info - the method of outputting data to the JMeter console

 

That’s it! You are now ready to use arithmetic operations in your code. To learn more advanced JMeter, try out our free JMeter academy.

 

Click here to subscribe to our newsletter.

 

Scale your JMeter tests with BlazeMeter. To see how it works, request a demo, or put your URL or JMX file 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.