Trouble in passing the csv values to a function

Hi,

I am getting error while passing the values that are read from the csv file. Need some help!!!

def getRequest(min:Int, max:Int): String= {

val rnd = new scala.util.Random
val valRange = min to max

var usersForSimulation = rnd.nextInt(valRange.length)

ReqBody = “”“location=%s”"".format(usersForSimulation)

ReqBody
}

val execsendinfo = exec(http(“sendinfo”)
.post("${tenantname}/device/ota/sendinfo")
.headers(PerfConfig.HeartBeatHeader)
.body(StringBody(Session => getRequest(${min}, ${max}))) ====> Getting an error at this location “not found : value $”
.check(status.is(200))
.check(jsonPath("$.opstatus").is(“0”))
)

val ForcedSendInfo = scenario(“ForcedSendInfo”).feed(tenantFeeder).exec(execsendinfo)

Hi Dinesh,

What you’re doing it won’t work : you’re mixing EL and Scala code in incompatible ways.

Here’s a working solution :

`
val execsendinfo = exec(http(“sendinfo”)

`
.post("${tenantname}/device/ota/sendinfo")
. headers(PerfConfig.HeartBeatHeader)
.body(StringBody(session => getRequest(session(“min”).as[Int], session(“max”).as[Int]))
.check(status.is(200))
.check(jsonPath("$.opstatus").is(“0”))
)

Also :

  • usersForSimulation doesn’t need to be a var
  • I don’t know where you defined ReqBody but if it’s a var defined out getRequest method, you’re in for a LOT of trouble. You can’t guarantee that they’re wont be concurrency issues and that your users won’t access it simultaneously and mess it up. If possible, simply delete ReqBody, it looks like it’s not needed here, because if *""“location=%s”"".format(*usersForSimulation) is the last statement of your method, it will be returned automatically, there is no need to store it in a val/var to return it right away.

Hi Pierre,

Thanks for quick response. I have tried the code you have given, but got an “java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer” exception. I thought I should be little elaborate on what I am trying to do.

My CSV file contains the following information.

tenanturl,min,max
URL-001.com,1000,1999

I would like to read min and max values from here and take a random value between of it and construct the request with it and send to the tenant URL.

Coming to the other points you have mentioned, I made all the variables as val, and my request body is lot bigger (I gave a sample one as an example, in my earlier post) and I will need to modify at least 5 to 6 values randomly from different sets.

Regards
Dinesh

if your “min” and “max” session attributes comes from a CSV file, they are read as Strings and you’ll need to convert them to Ints beforehand :

`

val execsendinfo = exec(http("sendinfo")

.post("${tenantname}/device/ota/sendinfo")
. headers(PerfConfig.HeartBeatHeader)

.body(StringBody(session => getRequest(session(“min”).as[String].toInt, session(“max”).as[String].toInt))

.check(status.is(200))
.check(jsonPath("$.opstatus").is(“0”))
)

`

Alright, you code snippet was incomplete, and ReqBody as its use after all :slight_smile:
Still, if possible, you should rework the construction of ReqBody so that you can get a fully built ReqBody from getRequest(…), which would avoid concurrency issues that arise from using a shared, mutable variable in a multi-threaded context.

Cheers,

Pierre

Thanks Pierre. This worked :). And yes, my intention is to return the complete request body from the function.

Thanks for your support.

Regards
Dinesh

You’re welcome :slight_smile:

Cheers,

Pierre