DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Perform Arithmetic Operations on Numeric Variables When Load Testing

How to Perform Arithmetic Operations on Numeric Variables When Load Testing

Testers often need to conduct arithmetic operations on variables in load or functional testing. See how to accomplish this with JMeter.

Sergey Horban user avatar by
Sergey Horban
·
Nov. 27, 17 · Tutorial
Like (3)
Save
Tweet
Share
7.42K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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.

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

2. Add a JSR223 Sampler

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

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

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

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

Example Code

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.

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

Example code:

Now let's look at a more advanced example.

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:

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.

Data Types Load testing

Published at DZone with permission of Sergey Horban, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Debugging Streams and Collections
  • The Quest for REST
  • Pros and Cons of Using Styled Components in React
  • Efficiently Computing Permissions at Scale: Our Engineering Approach

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: