Apache JMeter : Add random data in body for request

We are Borg picture We are Borg · Jun 28, 2017 · Viewed 16.8k times · Source

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:/";
    }
}

Answer

Dmitri T picture Dmitri T · Jun 29, 2017

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: