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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Thermometer Continuation in Scala
  • Thumbnail Generator Microservice for PDF in Spring Boot

Trending

  • The End of “Good Enough Agile”
  • MCP Servers: The Technical Debt That Is Coming
  • Event Driven Architecture (EDA) - Optimizer or Complicator
  • Scaling Microservices With Docker and Kubernetes on Production
  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.

By 
Sergey Horban user avatar
Sergey Horban
·
Nov. 27, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
9.3K 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.

Related

  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Thermometer Continuation in Scala
  • Thumbnail Generator Microservice for PDF in Spring Boot

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!