Raghav
Raghav

Reputation: 151

How to generate a random, unique mobile number in JMeter?

I have a requirement to generate a Random mobile number and input it in my subsequent request. During load test when running using multiple users ( ~1k users), how should I ensure the uniqueness of it across all my users?

I am using a groovy script for this task that generates a mobile number randomly.

(int)Math.floor(Math.random() * (9000000000 - 9000020000)) + 9000020000

I know that I can pre-generate the random mobile numbers and send as parameters. But I don't want to use this approach of static/parameterized data.

Upvotes: 0

Views: 3671

Answers (3)

w4dd325
w4dd325

Reputation: 617

If there is a formatting requirement on the mobile number you could prefix a timestamp and limit the output to the remaining number of digits like so:

Note: Mddmmsss will add 8 random digits to the prefix 077 (total of 11 digits like a standard UK phone number)

077${__time(Mddmmsss,)}

output exmaple:

07792203003

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168147

You cannot guarantee the "uniqueness" if you're talking about random.

You could do something like:

  1. Get the current timestamp
  2. Get the current virtual user number
  3. Get the current iteration
  4. Put everything together

Example code:

def timestamp = System.nanoTime()
def currentUser = ctx.getThreadNum()
def iteration = vars.getIteration()

def randomValue = timestamp as String + currentUser + iteration

vars.put('randomValue', randomValue) 

More information on the aforementioned JMeter API shorthands: Top 8 JMeter Java Classes You Should Be Using with Groovy

Upvotes: 2

Janesh Kodikara
Janesh Kodikara

Reputation: 1841

You could use Java faker to generate the random number.

import com.github.javafaker.service.FakeValuesService
import com.github.javafaker.service.RandomService

FakeValuesService fakeValuesService

fakeValuesService = new FakeValuesService(new Locale("en-AU"), new RandomService());

String prefixMobile = "04"
String mobileNumber  = fakeValuesService.regexify(String.format("%s\\d{8}", prefixMobile))
vars.put("mobileNumber",mobileNumber)
        
  

You will have to download the jar file, place the jar inside JMETER_HOME/bin folder and restart the JMeter to ensure the jar is available for your test.

Upvotes: -1

Related Questions