How to get data from a POST and store it to use in a DELETE (feeders maybe?)

I am new to Gatling and not sure if I am using the correct terminology for what I am asking. Thanks in advance. :slight_smile:

What I am trying to do is create a new user, get the userId that is automatically created during user creation and then delete that user with the userId. The trouble I am running into is I have no idea how to get the userId and use it for the part of my test which is to delete the user.

I have read and watched lots of tutorials about feeders and haven’t found the answer yet.

What I assume needs to happen is I check for the newly created userId in the POST and store it (maybe in a feeder?) so I can use it in the DELETE call. I am not sure how to do this.

I hope this makes sense.

Hello and welcome!

I guess the userId you mention is the one created on your own server.
In that case, you have to retrieve it from your server, and as you wrote, the newly created userId in the POST is usually the best place to find it.

If the same virtual user (from Gatling simulation) can create and delete itself, the best place to store it is in its own session attributes through saveAs method on your check.

Does that help?

Cheers!

1 Like

Thank you for the response. saveAs method makes sense and I will look into that and let you know.

Thank you so much for your response. That was exactly what I needed. Cheers!

@sbrevet I have a followup question.
I would like to load test my delete user endpoint with 100 users. I need to create the 100 users first and save their userId that my server automatically creates. I then need to delete the 100 users. Before I was creating and deleting a user at a time over and over 100 times. This time I need to test just the delete endpoint.

How can I save the 100 userIds while creating the users and then use those 100 userIds in delete endpoint?

I hope this makes sense.

Thank you for your time and help.

I’m not sure to understand.

If I tell it with my own words, you want to separate (in times) the creation phase of the 100 users from the deletion phase.

It really depend on you!

What I have in mind:

virtual users with gap

You can use the same scenario as before, but putting a fixed pause large enough to cover the injection phase.

Say your initial Simulation is:

  ChainBuilder createUser = exec(
    http("create user")
      .post("/users")
      .check(jsonPath("$.id").saveAs("myUserId"))
  );

  ChainBuilder deleteUser = exec(
    http("delete user")
      .delete("/users/#{myUserId}")
  );

  int injectionDurationInSeconds = 10;

  ScenarioBuilder scn = scenario("Sample")
    .exec(createUser)
    .exec(deleteUser)
    ;

  {
    setUp(
        scn.injectOpen(constantUsersPerSec(10).during(injectionDurationInSeconds))
    ).protocols(httpProtocol);
  }

You can change the scenario:

  ScenarioBuilder scn = scenario("Sample")
    .exec(createUser)
    .pause(injectionDurationInSeconds)
    .exec(deleteUser)
    ;

And you’ll have a bunch of users at the end of the 10 seconds that will only begin to delete themselves.

Global variable

Using a queue as global variable to store your user ids.

  ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
  
  ScenarioBuilder scnWithCreation = scenario("Creation")
    .exec(createUser)
    .exec(session -> {
      queue.offer(session.getString("myUserId"));
      return session;
    })
  ;
  ScenarioBuilder scnWithDeletion = scenario("Deletion")
    .exec(session -> session.set("myUserId", queue.poll()))
    .exec(deleteUser)
  ;

  {
    setUp(
      scnWithCreation.injectOpen(constantUsersPerSec(10).during(injectionDurationInSeconds))
        .andThen(
          scnWithDeletion.injectOpen(constantUsersPerSec(10).during(injectionDurationInSeconds))
        )
    ).protocols(httpProtocol);
  }

WDYT?

(Next time, please, open another thread even if you want to reference this one. We cannot have two solutions for a single thread)

Cheers!

1 Like