Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache JMeter : Add random data in body for request

I'm working on stress testing our application in Apache JMeter.

I thought of calling register user method which will add users in the database. But if the email already exists, then the database action does not take place.

How can I add a random number in body data? Or is there some other way I can stress test my application connected with database?

Here are some screenshots : enter image description hereenter image description here

Controller code :

@RequestMapping(value = "/person/add", method = RequestMethod.POST)
public String addPerson(@ModelAttribute("person") Person person, BindingResult bindingResult) {
    System.out.println("Person add called"+person.getUsername());
    person.setUsername(this.stripHTML(person.getUsername()));
    int personId = this.personService.addPerson(person);
    if (!(personId == 0)) {
        Person person1 = this.personService.getPersonById(personId);
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        Authentication authentication = new UsernamePasswordAuthenticationToken(person1, null, authorities);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return "redirect:/canvaslisting";
    } else {
        return "redirect:/";
    }
}
like image 625
We are Borg Avatar asked Jun 28 '17 10:06

We are Borg


People also ask

How does JMeter generate random names?

As per Using JMeter Functions guide __RandomString() function takes 3 parameters: Length of the desired random string. Source characters. If you need to store generated string into a JMeter variable you can provide variable name as 3rd argument.

What does the function random do in JMeter?

RAND - Tells JMeter to choose a match at random. ALL - Tells JMeter to use all matches, and create a template string for each one and then append them all together.


3 Answers

Take a look at JMeter Functions like:

  • __Random() - which generates a random number in the given range
  • __RandomString() - which generates a random string from the given input
  • __threadNum() - which returns the current thread number
  • __UUID() - which returns an unique GUID structure
  • __time() - which returns current time stamp in different formats
  • any combination of above

JMeter functions can be used anywhere in the test so you can put them directly into your request body.

Some more recommendations:

  • Don't use JMeter GUI for running the load test, GUI mode is designed for tests development and debugging only, the tests themselves need to be run in command-line non-GUI mode
  • Remove all the listeners from the test plan while running your load test as JMeter listeners are very resource intensive and create unnecessary overhead.
like image 58
Dmitri T Avatar answered Sep 30 '22 23:09

Dmitri T


  1. Use Random Variable with variable name emailValue and send ${emailValue} in request

  2. Use JDBC request to your database to create random number or sequence and save in variable name emailValue

  3. Use UUID function to create uniqueId and send in email ${uniqueId}@gmail.com for example

like image 36
user7294900 Avatar answered Sep 30 '22 23:09

user7294900


My example with __UUID

enter image description here

For POST request, make sure you have correct Content-Type in HTTP Header Manger , application/json for example.

like image 29
vanduc1102 Avatar answered Sep 30 '22 22:09

vanduc1102