3

I have a file with a Json request bodies in a file. I'd like to benchmark a web sending a constant number of requests per second, using the constantUsersPerSec(..).

However, every time a request is sent, I would like it to be taken from a list of requests (randomly, or in a loop, I don't care).

I cannot figure out how to do it with Gatling. Using the following code the request is randomly selected once and the same request is sent over and over again. Which is not what I want

My code:

class GcStressManyRequests extends Simulation{ 
  ...
  ...
    val httpProtocol: HttpProtocolBuilder = http
    .baseURL(baseUrl)
    .contentTypeHeader(contentType)  

    var requests = Source.fromResource("bodies/requests.txt").getLines().toIndexedSeq
    val random = new Random(System.currentTimeMillis())

    val scn: ScenarioBuilder = scenario("AE prod requests")
    .exec (
         http("bid_request")
         .post(endpoint)
         .body(StringBody(requests(random.nextInt(requests.length))))
         .asJSON
         .check(status.is(200))) 

         setUp(
         scn.inject(
         constantUsersPerSec(400) during (1 minutes),
         ).protocols(httpProtocol))

 }
Michael P
  • 2,017
  • 3
  • 25
  • 33

3 Answers3

4

I see you have your requests in a text file bodies.txt. For your task I would use a Feeder (check Step 03). With a feeder you can retrieve a random item from a list of items and use it in your calls. The steps you have to undertake to make it work:

  1. Convert your text file to a csv file, a csv file just needs a header and then the values. One row for each value. Let say you name the header 'request'.
  2. Create a feeder:

    val random_request = csv("bodies.csv").random
    
  3. Edit scenario:

     val scn: ScenarioBuilder = scenario("AE prod requests")
       .feed(random_request) //Puts a random request in your session.
       .exec (
         http("bid_request")
         .post(endpoint)
         .body(StringBody(${request})) //Retrieve the request from current session.
         .asJSON
         .check(status.is(200))) 
    
     setUp(
       scn.inject(
       constantUsersPerSec(400) during (1 minutes),
     ).protocols(httpProtocol))
    
Stanko
  • 4,275
  • 3
  • 23
  • 51
2

For completeness, I'd like to share Domingos Creado's answer on Gatling Google Groups that worked for me.

val scn: ScenarioBuilder = scenario("AE prod requests")
.exec(session => {
          val session2 = session.set("therequest", requests(random.nextInt(requests.length)))
          session2
        })
.exec (
     http("bid_request")
     .post(endpoint)
     .body(StringBody("${therequest}"))
     .asJSON
     .check(status.is(200))) 
Michael P
  • 2,017
  • 3
  • 25
  • 33
1

Step 1 : Create Input in my case it is stack .The below is just a method you can use your own implementation

var jobsQue: Stack[InputData] = GenerateInput.creatJobsCollection()

Step 2 : Use it

val execScn = scenario("Requests").group("Groups") {
asLongAs(session => jobsQue.length > 0) {  exec { session =>
    var length = jobsQue.length  //Whatever
    var reportElement = jobsQue.pop(); //POP it } } }

Hope this is helpful enough to keep you going.

user666
  • 1,104
  • 12
  • 20
  • Thanks user666. I am confused about why I need the group and asLongAs. Also the most important part which is how to send the request is not in the answer and I cannot figure out how to do it. – Michael P Feb 02 '18 at 19:45
  • i am using asLongAs for short circuit so it will keep on running till stack is greater than zero length which is getting popped below .. – user666 Feb 03 '18 at 20:49