Is it possible to create a session value that executes a function in order to interpolate it?

Here’s what I want to do:

`
http( desc )
.post( url )
.body( StringBody( “”"{dateTime:"${TIMESTAMP}",…}""" ) )

`

I want TIMESTAMP to be evaluated automatically by the expression language engine to build a timestamp in the correct format based on the current date/time.

Is it possible to store a function that returns a string into TIMESTAMP, and have the expression language call that function in order to get the value to interpolate into the string?

Not with Gatling EL.

You can write an Expression that would use Scala String interpolation.
See bottom example: http://gatling.io/docs/2.0.0-RC6/http/http_request.html?highlight=stringbody#request-body

Believe it or not, I found a way to do it!

`
object DateTimeString {
override def toString = …
}


.exec( session => session.set( DATETIME, DateTimeString ) )

.exec( http( “${DATETIME}” ).get( url ) )

`

How very fun

Well, you asked “Is it possible to store a function that returns a string into TIMESTAMP”, which is not was you did :wink:
You store a value.

Yes, that is true. I did not accomplish the thing I asked about.

See, the power of a question… had I asked the right question… :slight_smile:

Is this the best way to go to inject data into the session to be used by the Gattling EL?

I was trying to do the same as you today and started uit with an Array for a custom feeder but that got evaluated immediately and I was stuck with the same DateTime value as when it got evaluated.

You solution does work for me but does this mean that I have to this for all my “non static” data that I want to use with Gatling EL?

What are some examples of your “non-static data”?

Well the current DateTime in example.

I have a csv feeder for pre defined data like Person.name and Person.gender but I also need to do some calculation per request. I figured that maybe creating a feeder that contains something like a <key, function> would do the trick but I haven’t found it.

So the output depends on the values in other session variables?

For example: A = 1, B = 2, C = ${A}+${B} = 3; change A or B and the value of C automatically changes, yes?

You can accomplish that by having a function in your chain, e.g.

`
.exec( session => session.set( A, 1 ).set( B, 2 ) )

.exec( session => session.set( C, session(A).as[Int] + session(B).as[Int] ) )

`

But then, the value of C is only updated when that exec() statement is executed. It would be NICE to be able to compose an expression that is evaluated every time it is converted to a string, but to the best of my knowledge, that doesn’t work.

The trick with the date/time worked only because it did not need access to the session.

That said, Scala is a pretty powerful language. Who knows what kind of crazy wonderful trickery you might be capable of doing to make it do what you want. I’d be interested, just from the geekyness factor. If I come up with something, I’ll let you know. I trust you will do likewise. :slight_smile:

Yeah I’d be interested in the solution as well. I’m going to dust off my Scala knowledge, the syntax kinda slipped away a bit. But when I do get a satisfactory solution to this I’ll definitely post it here.

http://gatling.io/docs/2.0.0/general/scenario.html#exec

exec(session => session(“datetime”, System.currentTimeMillis) // not sure what you mean by DateTime