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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces
  • Share Delta Lake Tables Between Databricks and Snowflake
  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence

Trending

  • AI Agents for Software Engineering and Autonomous Development Workflows
  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence
  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • Three Measured Outcomes of Manually Scaling Fleet in Prod

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Nov. 27, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces
  • Share Delta Lake Tables Between Databricks and Snowflake
  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook