Skip to main content
Back to blog
tutorials 13 April 2020 2 min read

Create Data with JMeter

Using Apache JMeter with JSR223 samplers to generate test data including random emails, dates of birth, and strings for performance testing.

M

Mark

Performance Testing Expert

Apache JMeter, typically used for load testing, can also generate test data through JSR223 samplers supporting multiple languages.

Random Email Generation

Create thousands of random email addresses using a JSR223 sampler with Groovy:

File file = new File('RandomEmails.csv')

def uuid = UUID.randomUUID().toString()
def randomNum = new Random().nextInt(99999999)
def email = "${uuid}-${randomNum}@gmail.com"

file.append("${email}\n")

This produces emails like: f81e43fb-99f2-4f93-85fb-c1bf2e3ea461-36522621@gmail.com

Date of Birth Data

Build on the previous example by adding randomly generated dates using the timeShift function:

File file = new File('RandomData.csv')

def uuid = UUID.randomUUID().toString()
def randomNum = new Random().nextInt(99999999)
def email = "${uuid}-${randomNum}@gmail.com"

// Generate random date between 1920 and 2002
def startYear = 1920
def endYear = 2002
def randomYear = startYear + new Random().nextInt(endYear - startYear)
def randomMonth = new Random().nextInt(12) + 1
def randomDay = new Random().nextInt(28) + 1
def dob = String.format("%04d-%02d-%02d", randomYear, randomMonth, randomDay)

file.append("${dob},${email}\n")

Random String Addition

Extend the dataset further by incorporating randomly generated forenames and surnames:

File file = new File('RandomData.csv')

def uuid = UUID.randomUUID().toString()
def randomNum = new Random().nextInt(99999999)
def email = "${uuid}-${randomNum}@gmail.com"

// Random date of birth
def startYear = 1920
def endYear = 2002
def randomYear = startYear + new Random().nextInt(endYear - startYear)
def randomMonth = new Random().nextInt(12) + 1
def randomDay = new Random().nextInt(28) + 1
def dob = String.format("%04d-%02d-%02d", randomYear, randomMonth, randomDay)

// Random forename (10 characters) and surname (15 characters)
def alphabet = 'abcdefghijklmnopqrstuvwxyz'
def forename = (1..10).collect { alphabet[new Random().nextInt(26)] }.join()
def surname = (1..15).collect { alphabet[new Random().nextInt(26)] }.join()

file.append("${dob},${email},${forename},${surname}\n")

Final Output

The complete solution generates CSV rows with four fields:

  1. Date of birth
  2. Email address
  3. Forename
  4. Surname

This demonstrates how complex test datasets can be quickly created without manual effort.

Tags:

#jmeter #test-data #jsr223 #groovy #data-generation

Need help with performance testing?

Let's discuss how I can help improve your application's performance.

Get in Touch