How to iterate csv value inside a forever loop?

Hi, I have a scenario where I only need one user, but to change to value of the account repeatedly, I had a csv file with the account already, when I try this sample code

.feed(myCsvVar)
.forever().on(
   getTokenRequest,
   pace(500.milliseconds)
   .exec(
        encodeAccountRequest, //this request will have #{accountValue}
        getInfoFromEncodeValue
   )
)

Upon checking the output, I see accountValue is not changing, is there a way to force increase it in the loop ?

did you try to put feed inside the loop?

.forever().on(
   feed(myCsvVar),
   getTokenRequest,
   pace(500.milliseconds)
   .exec(
        encodeAccountRequest, //this request will have #{accountValue}
        getInfoFromEncodeValue
   )
)

or please, be more explicit with actual values (a full example, maybe?)

Cheers!

Hi @sbrevet , first I will be having a csv looks like this

accountValue
value1
value2
value3

My request flow will be following, for an admin user (which is why I only need 1 user)

getTokenRequest >> encodeAccountRequest [this request will need accountValue from csv file, changing accordingly] >> getInfoFromEncodeValue

Sample request for encodeAccountRequest (really, it’s just this simple)

http('encodeAccountRequest')
.get('https://www.url')
.header('authorization', '#{token}')
.body(StringBody('#{accountValue}')
.check(status().is(200))

this encodeAccountRequest and getInfoFromEncodeValue will have repeated loop as admin will continue checking each account, so I put it into a pace, as the sample code you see above.

Now the problem with the original code, is when I feed the csv file in, that #{accountValue} will stay the same, aka value1, for every requests, so is it possible to force it to increment by every request?

My protocol:

Add the src/test/resources/myCsv.csv file:

accountValue
value1
value2
value3

Setup:

  private static final HttpProtocolBuilder httpProtocol = http.baseUrl("http://localhost:1080")
      .acceptHeader("application/json")
      .userAgentHeader(
          "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36");

// same content as before

{    
  setUp(scenario.injectOpen(atOnceUsers(1))).protocols(httpProtocol).maxDuration(50);
}

Change the scenario to:

  private static final ScenarioBuilder scenario = scenario("Scenario")
      .feed(csv("myCsv.csv"))
      .forever().on(
          pace(3),
          http("Session").get("/session?accountValue=#{accountValue}")
      );

Result (reproducer):

[GET] - http://localhost:1080/session?accountValue=value1
[GET] - http://localhost:1080/session?accountValue=value1
[GET] - http://localhost:1080/session?accountValue=value1
[GET] - http://localhost:1080/session?accountValue=value1
...

When changing scenario (as described in my previous message) into:

  private static final ScenarioBuilder scenario = scenario("Scenario")
      .forever().on(
          feed(csv("myCsv.csv")),
          pace(3),
          http("Session").get("/session?accountValue=#{accountValue}")
      );

Result:

[GET] - http://localhost:1080/session?accountValue=value1
[GET] - http://localhost:1080/session?accountValue=value2
[GET] - http://localhost:1080/session?accountValue=value3

And then the simulation broke, because the CSV is not large enough (either add more entries or make it circular)

1 Like

yup that’s exactly what I need, thank you @sbrevet :smiley: