How can I measure only a http request inside default function?

Hello, my default function looks something like this:

{
  let Id = **firstPost**();// a function outside of default which is getting an Id as a response of the request
  const url = `https://example/xyz/${Id}`;
  const payload = JSON.stringify({
    x: "y",
  });
  const **secondPost** = http.post(url, payload, params);
  myTrend.add(secondPost.timings.duration);
  deleteFromDb(Id);
}

So, here the http_req_duration is around 590ms, and the measure of secondPost is 625ms, which doesnt make sense, how can I measure correctly the second post?
Also, how can I parse values to teardown function, so I won’t use deleteFromDb(Id) in the default function?

Hi @kkk1

For the first part, I believe you are looking for URL grouping. I’m not sure I understood the issue correctly though, so let me know if that does not work for your case. It’s possible tags and groups can also help you here.

For the second question, how to pass values to the teardown function I would suggest to have a look at this thread: Destroying test data generated during the test. Especifically Destroying test data generated during the test - #2 by nedyalko and Destroying test data generated during the test - #9 by imiric.

I hope this helps.

Cheers!

For accurate measurement of the second post request duration, you can use the http.batch() function to execute multiple requests in parallel. This will ensure that the measurement includes the time taken for both requests, rather than just the second one. Here’s an example:

import http from 'k6/http';
import { check, sleep } from 'k6';

export default function () {
  let Id = firstPost();
  const url = `https://example/xyz/${Id}`;
  const payload = JSON.stringify({
    x: "y",
  });
  
  // Use http.batch() to execute multiple requests in parallel
  const responses = http.batch([
    { method: 'POST', url: url, body: payload, params: { tags: { name: 'First Post' } } },
    { method: 'POST', url: url, body: payload, params: { tags: { name: 'Second Post' } } },
  ]);

  // Use the response objects to measure durations and perform assertions
  const firstPostDuration = responses[0].timings.duration;
  const secondPostDuration = responses[1].timings.duration;
  
  myTrend.add(secondPostDuration);

  deleteFromDb(Id);
}

// Teardown function to clean up resources after the test
export function teardown(data) {
  // Access the values passed from the default function
  const idToDelete = data.Id;
  deleteFromDb(idToDelete);
}

In this example, the http.batch() function is used to send two parallel POST requests. Each request is given a unique tag using the params parameter. Then, the durations of the two requests are accessed from the responses array.

To pass values to the teardown function, you can use the data object parameter. In the default function, you can specify the values to pass to the teardown function by returning an object. In the teardown function, you can access these values using the data parameter.

Note: Make sure to adjust the code according to your specific scenario and function names (firstPost, deleteFromDb, etc.).

Regards,
Rachel Gomez

1 Like