Batch request with constant arrival rate

Hello colleagues! We switched to work with k6 and there was a question about the batch method and its use with executor: ‘constant-arrival-rate’

  1. When we maintain a certain number of iterations, one iteration is considered complete when responses from all requests in the batch have arrived?
  2. Metrics iteration_duration is calculated as an average of all requests or the total time of requests in a batch
  3. If iteration duration outputs the average of all requests, how to measure the percentile between batches of requests

Thanks for any help!

An iteration is complete when everything in the iteration has finished running. http.batch() executes its requests in parallel, but it itself is a synchronous function, so it wont return a result until all of the requests are done. The number of requests that http.batch() will execute in parallel is determined by the batch and batchPerHost global options.

Because of this, iteration_duration won’t necessarily contain either the average or the total time, it will contain the time the whole iteration took to execute. Even if your iteration contains just a single http.batch() call, the iteration_duration will depend on the number of requests you specified in it and the value of these options.

For example, if your iteration has an http.get() call for some website, followed by an http.batch() call for the static resources of the same website, the iteration_duration would be roughly representative for how long it takes a browser to load that webpage.

For the individual time of requests, whether you are interested in the average or some percentile, just look at the regular HTTP metrics that k6 measures like http_req_duration. And if you want the total time all requests would take if you execute them linearly, one after the other, you are probably better off not using http.batch() and instead using regular http.request() calls. But if you want to use http.batch() for some reason, you can add a custom metric and use the http.Response.timings for every request in the batch result to populate it.

Thanks for your reply!
The documentation describes the batch method as an alternative for the browser to work when the page is loaded.
The team’s task is to collect metrics on the page load time after clicking on certain urls.
We tried xk6-browser, but while maintaining a certain RPS, this task is not possible, even in headless mode.
Our solution is to collect all outgoing requests in the browser when loading the page using Selenium, import it into a file as an array, and use this array in the batch method. Now I would like to collect metrics for the execution of all requests in a batch at a certain RPS.
From your answer I understand that iteration_duration gives us metrics for loading the entire page and executing all queries in the group.
There was an idea to wrap batch in group (), but it seemed to us redundant.

If you have any comments or can suggest a more practical way to solve the problem, I will be very grateful to you! Thanks again!

Instead of Selenium, maybe look into recording the real browser traffic in .har files and then converting them into k6 scripts: HAR converter

Unfortunately the har-to-k6 converter can’t generate http.batch() calls yet (Add support for http.batch() · Issue #17 · grafana/har-to-k6 · GitHub), but you can edit that part of the script manually. Or you can use the old k6 convert sub-command - even though it’s deprecated, it still works, and it produces scripts that have http.batch(), even if they are sub-par in other ways. In both cases you’d need some manual edits to the script, but it might be simpler than going through selenium.

Thank you! Great! We’ll try it!
In terms of metrics, iteration_duratio when using executor: ‘constant-arrival-rate’ will show results for groups of requests in a batch and not for individual requests. Did you understand correctly?

Yes, it will contain the time it takes to make all of the requests concurrently (based on options.batch and options.batchPerHost).

1 Like