Search code examples
javascriptload-testingk6

K6 Load Testing - How to make sequential id for entire test run


I have a api endpoint that each request need to be a different id , but how to make a id global and sequentual increment for each iteration shared for all VU, like a primary key on database table.

Ex:

request 1 : <id>400</id> VU :1
request 2 : <id>401</id> VU :1

request 1 : <id>402</id> VU :2

request 3 : <id>403</id> VU :1
request 4 : <id>404</id> VU :1

request 2 : <id>405</id> VU :2
request 3 : <id>406</id> VU :2

Is there any way to declare a variable that is shared by the entire test? Setup and Init are for each VU and cannot share data according to the documentation.


Solution

  • This is not easily possible because each VU runs in a separate JavaScript VM and memory is not shared between them. See the Test life cycle documentation for details. This is done to allow test runs to be distributed across k6 instances, so synchronizing data across them will require an external solution.

    One approach you could take is to keep track of and increment the ID in a web service that your k6 script can query to get the next ID from. Redis could serve this purpose well, see this related answer for ideas. But note that any such solution will impact your end-of-test test metrics and the performance of the test itself, so it's not ideal, but I don't see a better approach.