Not understanding "sessions" or exec statements

Gatling version: 3.11.5 (must be up to date)
Gatling flavor: [ X] java kotlin scala javascript typescript
Gatling build tool: [X ] maven gradle sbt bundle npm

Hello Community,
I have the Gatling scenario below and have 2 questions.

  1. Why does the first “exec(session ->” following a “check” not produce a compile error but the second “exec(session ->” statement prevent the code from compiling? Both are directly have at “check” statement.
  2. Even without the second “exec(session ->” statement, although I have a “check/saveAs” statement to save the “QUID_UUID” value to the session in request_12, request_23 during execution says the “QUID_UUID” cannot be found, but the “exec(session ->” directly after that prints the value of the “QUID_UUID”?

Here is the code:

private ScenarioBuilder scn = scenario("RQRFirefoxTest")
  .exec(session -> {
        Session newSession = session.set("environment", environment);
        return newSession;
    })
  .exec(
  	  // RQRLogon,
  	  http("request_0")
  		.get(uri04 + "xxxxxx")
  		.check(status().is(200))
  		.check(bodyString().saveAs("responseBody")),
  		exec(session -> {
  		  System.out.println("----------> Response Body:");
  		  System.out.println(session.getString("responseBody"));
  		  return session;
  		}),
  	  pause(1),
  	  http("request_1")
  		.get("/quote-api/api/auth/who")
  		.resources(
  		  http("request_2")
  			.get("/quote-api/api/quote/getUserDetails/bcantin"),
  		  http("request_4")
  			.get("/quote-api/api/auth/multi-state-enabled")
  		),
  	  pause(4),
  	  http("request_11")
  		.post("xxxxx")
  		.headers(headers_8),
  	  http("request_12")
  		.get("XXXXXXXXXX")
  		.headers(headers_12)
  		.resources(
  		  http("request_13")
  			.post("XXXXXXXXX")
  			.headers(headers_13)
  			.check(bodyString().saveAs("setBusinessStateResponseBody"))
  			.check(jmesPath("uuid").saveAs("UID")),
  		// Was trying to insert below exec statement to print the UID but received a compile error			
  		exec(session -> {
  				System.out.println("----------> UID:");
  				System.out.println(session.getString("UID"));
  				return session;
  			}),

  		  http("request_14")
  			.post("/XXXXXXXX")
  			.headers(headers_14)
  			.body(RawFileBody("0014_request.json")),
  		  http("request_23")
  			.get("/XXXXXXXXXX/#{UID}")   <----- #{UID} value is not found during Gatling execution at this point buty is found in the exec below.
  		),
  		exec(session -> {
  			System.out.println("\n\n =======> <setBusinessStateResponseBody> Response Body:");
  			System.out.println("\n" + session.getString("setBusinessStateResponseBody") + "\n");
  			System.out.println("\n\n =======> Session UID: " + session.getString("UID") + "\n");
  			return session;
  		}),
  		pause(2)
  );

Thanks Timmer

just my taste :slight_smile: please cut the unnecessary code away and put the whole code into the code format block so that we all can easier to understand your code and what you mean.
I’m willing to help but code format kinda hurt…

Hello,
I cleaned it up the best I could and shortened it.
Repeat of my questions:
I have the Gatling scenario below and have 2 questions.

  1. Why does the first “exec(session ->” following a “check” not produce a compile error but the second “exec(session ->” statement prevent the code from compiling? Both are directly have at “check” statement.
  2. Even without the second “exec(session ->” statement, although I have a “check/saveAs” statement to save the “QUID_UUID” value to the session in request_12, request_23 during execution says the “QUID_UUID” cannot be found, but the “exec(session ->” directly after that prints the value of the “QUID_UUID”?

Here is the code:

  private ScenarioBuilder scn = scenario("RQRFirefoxTest")
    .exec(session -> {
          Session newSession = session.set("environment", environment);
          return newSession;
      })
    .exec(
		  // RQRLogon,
		  http("request_0")
			.get(uri04 + "xxxxxx")
			.check(status().is(200))
			.check(bodyString().saveAs("responseBody")),
			exec(session -> {
			  System.out.println("----------> Response Body:");
			  System.out.println(session.getString("responseBody"));
			  return session;
			}),
		  pause(1),
		  http("request_1")
			.get("/quote-api/api/auth/who")
			.resources(
			  http("request_2")
				.get("/quote-api/api/quote/getUserDetails/bcantin"),
			  http("request_4")
				.get("/quote-api/api/auth/multi-state-enabled")
			),
		  pause(4),
		  http("request_11")
			.post("xxxxx")
			.headers(headers_8),
		  http("request_12")
			.get("XXXXXXXXXX")
			.headers(headers_12)
			.resources(
			  http("request_13")
				.post("XXXXXXXXX")
				.headers(headers_13)
				.check(bodyString().saveAs("setBusinessStateResponseBody"))
				.check(jmesPath("uuid").saveAs("UID")),
			// Was trying to insert below exec statement to print the UID but received a compile error			
			exec(session -> {
					System.out.println("----------> UID:");
					System.out.println(session.getString("UID"));
					return session;
				}),

			  http("request_14")
				.post("/XXXXXXXX")
				.headers(headers_14)
				.body(RawFileBody("0014_request.json")),
			  http("request_23")
				.get("/XXXXXXXXXX/#{UID}")   <----- #{UID} value is not found during Gatling execution at this point buty is found in the exec below.
			),
			exec(session -> {
				System.out.println("\n\n =======> <setBusinessStateResponseBody> Response Body:");
				System.out.println("\n" + session.getString("setBusinessStateResponseBody") + "\n");
				System.out.println("\n\n =======> Session UID: " + session.getString("UID") + "\n");
				return session;
			}),
			pause(2)
	);

You’re trying to have your second exec in a resources block, which is not legit. Only http requests can go there.

Hello Slandelle,
Thank you for explaining that. Since in many cases it appears that almost ALL of my http calls have a resources block and many times I need to capture a value from the response of one of the http calls INSIDE of the resources block to use in other requests, can you clarify if I perform a “check.saveAs” in one of the resources http calls, is that session variable available to me both INSIDE and OUTSIDE of the resources block? The problem I am having is that I have resources block with 10 http calls and the value I am capturing in the say, 2nd http call, is needed in the 4th, 5th, and 6th http calls INSIDE of the resources block. When I try to access the #{UID} session variable captured with a “check.saveAs” Gatling is giving me an error saying that “No attribute named ‘UID’ is define” message. Also, if I cannot use an ‘exec’ inside the resources block, are you saying that I can never use a System.out.println to show anything inside of the resources block at all?

Thanks Timmer

Beware that resources blocks are executed concurrently, not sequentially, so if you perform a check to save data from one request, it might or might not be available for another request in the same resources block. If you need to guarantee sequentiality, you cannot use resources.

Slandelle,
OK, understood, but a little confused.
It is the Gatling recorder that created the Java file with this http—>resources(http, http, http) format, but your answer does explain why that resource’s http calls (which were named request_# in order) are not executed in that order when I run the simulation.
So, if I need those items to run sequentially, I can just pull them pull the requests out of the resources block altogether? If so, what is the purpose of the resources block then?
And also, for a single user is there only ONE session regardless of where the simulation is running, so that share the same session regardless of whether they are in and resources block or outside of the resources block?
Also, you never answered if there is a way to println in java, a value inside a http call INSIDE the resources block if you cannot use an exec statement?

Thanks, Timmer

The recorder can only try to figure out which requests are resources based on temporality. It’s not perfect and probably cannot be.

Ok, understood.
Lot to learn.
Still had a few open questions whenever you have a moment.

Thank you for your help, much appreciated.
Timmer

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.