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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • How to Build Slack App for Audit Requests
  • Mastering Redirects With Cloudflare Bulk Redirects
  • Idempotency in Distributed Systems: When and Why It Matters

Trending

  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Scalable System Design: Core Concepts for Building Reliable Software
  1. DZone
  2. Data Engineering
  3. Data
  4. 3 Ways to Generate Random Variables in JMeter

3 Ways to Generate Random Variables in JMeter

Random numbers are useful in testing, like when you need a random URL. Learn how to generate random numbers in JMeter.

By 
Leticia Almeida user avatar
Leticia Almeida
·
Aug. 19, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
36.1K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes we need to generate random values in our Apache JMeter™ scripts and then work with them in our requests. For example, in cases where the URL requires an auto-generated and unique value as a parameter.

There are many ways to do this in JMeter. In this blog, we will review some of the most commons ones:

  • Working with epoch — Based on epoch time.
  • Working with CSV files — For cases where you want to use your own generated values.
  • Working with the Random Variable Sampler — Where you can select the seed and the length of your value, among other available configurations.

Let's get started.

Epoch time, also known as Unix epoch time, is a string of numbers that represents a specific and unique time (the number of seconds that have elapsed since January 1, 1970).

Working with this value helps you to simulate the request in an easy way, when the URL you want to hit looks like this and you need to randomize numbers for it:

In these cases, when setting up your request, you just have to invoke ${__time()}. This way you can generate a random and unique value.

Let see how to look at this invoke in JMeter:

And what happens when we run it?

As you can see, this JMeter function generated requests with a random and unique value for each of my calls.

That was easy, right? OL, let's continue reviewing other options.

If you have already generated the values you want to use, you can list them in a CSV file and consume them in your requests. But, how can I achieve this? Let's see.

Imagine we have a list of random variables that are already generated, saved as "variables.csv".

Now, we want to consume this list in our script. So, we have to add a CSV dataset config as we show below.

Thread Group -> Add -> Config Element -> CSV Data Set Config

Set the CSV Data Set Config as follows:

  • Filename: The name of the file, in this case. Please notice that if you locate the file in the same folder as the script, you just have to enter the name. In this case, the full path is not necessary, and you can work with the relative path of the script.
  • Variable names: The name we are going to use to invoke the variable. In this example, we set it as "random".
  • Leave the rest of the fields as their default settings. It is important to keep the Sharing mode as "All threads" if you don't want to repeat values.

If you want more information about this element sampler, please check it out here.

Having done this, we just have to invoke the value typing  ${random} 

So, if we run the test, we will see how the script consumes the values defined in the CSV file:

So, this is another option to work with random variables, in cases in which you already have the list of random variables you want to use.

A third option could be to work with Random Variable, an element available in JMeter's config elements:

Thread group -> Add -> Config Element -> Random Variable

The sampler looks like this:

Setting it as follows:

  • Variable name: The name we are going to use to invoke the variable. "random" in this example.
  • Output Format: The format for the variable. You can set the desired length of the number. I've set 00000 in order to work with a five-digit number. You can also use USER_000.
  • Minimum and Maximum: The range we want to set for the variable.
  • Seed: The seed for the random number generator. A seed is the first input that the number generation function receives to start the random generation.
  • Per Thread: Is important to take this option into consideration. If you set it as True the value will be shared by the threads. This means that there will be threads with the same value. If you require the variable to be different each time, this could cause problems for us. If you want to always generate a different value, you have to set it as False.

Please, check JMeter's component reference to read more information about the Random Variable.

So, if we run the test now, we will see:

We have shown three different options we can follow to achieve a script that works with random variables each time. Choose the option that fits you best according to your needs. If you have more ideas, please share them in the comments section below!

After creating your JMeter script with its random variables, run it in BlazeMeter. You will be able to massively scale, share tests and results and analyze metrics in real-time or compare them over time.

Data set CSV Requests Element Testing Data (computing)

Published at DZone with permission of Leticia Almeida, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • How to Build Slack App for Audit Requests
  • Mastering Redirects With Cloudflare Bulk Redirects
  • Idempotency in Distributed Systems: When and Why It Matters

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!