Martkos IT Ltd

View Original

Create Data with JMeter

Apache JMeter is intended to place end points under load. With the addition of the JSR223 sampler you also write code in Groovy, Java, Javascript, Beanshell, jexl and other languages. This means JMeter can be used for other purposes such as creating test data.

Create 1000s of random emails

Using a combination of Apache JMeter variables and a JSR223 sampler we can quickly generate random email addresses and write them out to a file.

  • Create a JSR223 sampler

  • Uncheck the “Cache compiled script if available”

  • Add the following code which creates a file called “RandomEmails.csv” and the appends the email address on a new line.

Code snippet

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

file.append("${__V(${__UUID()}-${__Random(1,90000000,)}@gmail.com)}\n")

The email will be made up of a UUID such as f81e43fb-99f2-4f93-85fb-c1bf2e3ea461 and a random number.

Example output

f81e43fb-99f2-4f93-85fb-c1bf2e3ea461-36522621@gmail.com

Add Data of Birth

To add more columns of data you can simply amend the file.append line.

We could generate random date of births for example. Here we randomly choose a day, month and year and then subtract 20 years (approximately 7300 days not including leap years)

${__timeShift(d/M/yyyy,${__Random(01,28,)}/${__Random(01,12,)}/${__Random(1920,2002,)},P-7300D,,)}

The new file.append line looks like

file.append("${__timeShift(d/M/yyyy,${__Random(01,28,)}/${__Random(01,12,)}/${__Random(1920,2002,)},P-7300D,,)},${__V(${__UUID()}-${__Random(1,90000000,)}@gmail.com)}\n")

The data of birth will use the format d/M/YYYY so will result in a date based off that value.

Example output

18/7/1916,8db3fd1d-9d0b-4804-9c06-46a2f56c4925-50363068@gmail.com

Add Random String

We could continue to add more and more columns and make the data far more complex.

In the final step lets add two random strings to represent forename and surname.

For Forename we will use a combination of 10 letters from the alphabet

${__RandomString(10,abcdefghijklmnopqrstuvwxyz)}

For Forename we will use a combination of 15 letters from the alphabet

${__RandomString(15,abcdefghijklmnopqrstuvwxyz)}

The new file.append would look like

file.append("${__timeShift(d/M/yyyy,${__Random(01,28,)}/${__Random(01,12,)}/${__Random(1920,2002,)},P-7300D,,)},${__V(${__UUID()}-${__Random(1,90000000,)}@gmail.com)},${__RandomString(10,abcdefghijklmnopqrstuvwxyz)},${__RandomString(15,abcdefghijklmnopqrstuvwxyz)}\n")

We should now have a DOB, Email, Forename and Surname written out to the file.

Example output

13/5/1940,5423893d-fabe-4d11-8627-7341c30bb95a-67903199@gmail.com,bwopwyqkpd,tbuyfryigeiuiup

Using a JSR223 sampler you can quickly see how easy it is to create data. This does not have to be written out to a file and could be generated as part of the test.

FURTHER READING

JMeter Cheat Sheet

JSR223 Sampler