最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Aug 7, 2020 at 2:17 J.SperandioJ.Sperandio 1171 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

For anyone looking for a way to generate sequential numbers. Since version v0.0.34, k6/execution has been introduced that has a property iterationInTest that is an integer described as:

The unique and zero-based sequential number of the current iteration in the scenario. It is unique in all k6 execution modes - in local, cloud and distributed/segmented test runs. However, while every instance will get non-overlapping index values in cloud/distributed tests, they might iterate over them at different speeds, so the values won't be sequential across them.

This can be used in your test code to generate sequential numbers such as:

import exec from 'k6/execution':

export default function () {
  const url = `https://foo.bar/${exec.scenario.iterationInTest}`;
  http.get(url)
}

I was able to use to increment a number for each iteration of the test.

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.

发布评论

评论列表(0)

  1. 暂无评论