diff --git a/docs/sources/k6/next/examples/data-parameterization.md b/docs/sources/k6/next/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/next/examples/data-parameterization.md +++ b/docs/sources/k6/next/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.47.x/examples/data-parameterization.md b/docs/sources/k6/v0.47.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.47.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.47.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.48.x/examples/data-parameterization.md b/docs/sources/k6/v0.48.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.48.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.48.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.49.x/examples/data-parameterization.md b/docs/sources/k6/v0.49.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.49.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.49.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.50.x/examples/data-parameterization.md b/docs/sources/k6/v0.50.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.50.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.50.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.51.x/examples/data-parameterization.md b/docs/sources/k6/v0.51.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.51.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.51.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.52.x/examples/data-parameterization.md b/docs/sources/k6/v0.52.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.52.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.52.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.53.x/examples/data-parameterization.md b/docs/sources/k6/v0.53.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.53.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.53.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.54.x/examples/data-parameterization.md b/docs/sources/k6/v0.54.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.54.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.54.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.55.x/examples/data-parameterization.md b/docs/sources/k6/v0.55.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.55.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.55.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.56.x/examples/data-parameterization.md b/docs/sources/k6/v0.56.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.56.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.56.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v0.57.x/examples/data-parameterization.md b/docs/sources/k6/v0.57.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v0.57.x/examples/data-parameterization.md +++ b/docs/sources/k6/v0.57.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v1.0.x/examples/data-parameterization.md b/docs/sources/k6/v1.0.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v1.0.x/examples/data-parameterization.md +++ b/docs/sources/k6/v1.0.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v1.1.x/examples/data-parameterization.md b/docs/sources/k6/v1.1.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v1.1.x/examples/data-parameterization.md +++ b/docs/sources/k6/v1.1.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v1.2.x/examples/data-parameterization.md b/docs/sources/k6/v1.2.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v1.2.x/examples/data-parameterization.md +++ b/docs/sources/k6/v1.2.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js diff --git a/docs/sources/k6/v1.3.x/examples/data-parameterization.md b/docs/sources/k6/v1.3.x/examples/data-parameterization.md index f084b2ee68..88308ae4ab 100644 --- a/docs/sources/k6/v1.3.x/examples/data-parameterization.md +++ b/docs/sources/k6/v1.3.x/examples/data-parameterization.md @@ -1,24 +1,21 @@ --- -title: 'Data Parameterization' +title: 'Data parameterization' description: | - Scripting examples on how to parameterize data in a test script. Parameterization is typically - necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. - - Parameterization helps to prevent server-side caching from impacting your load test. - This will, in turn, make your test more realistic. + Scripting examples on how to parameterize data in a test script. weight: 05 --- -# Data Parameterization +# Data parameterization + + _Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. This page gives some examples of how to parameterize data in a test script. -Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +Parameterization is typically necessary when Virtual Users (VUs) make a POST, PUT, or PATCH request in a test. You can also use parameterization when you need to add test data from a separate file. -Parameterization helps to prevent server-side caching from impacting your load test. -This will, in turn, make your test more realistic. +Parameterization helps to prevent server-side caching from impacting your load test, which makes your tests more realistic. ## Performance implications of `SharedArray` @@ -38,8 +35,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ ## From a JSON file -{{< code >}} - ```json { "users": [ @@ -49,10 +44,6 @@ A note on performance characteristics of `SharedArray` can be found within its [ } ``` -{{< /code >}} - -{{< code >}} - ```javascript import { SharedArray } from 'k6/data'; // not using SharedArray here will mean that the code in the function call (that is what loads and @@ -68,16 +59,12 @@ export default function () { } ``` -{{< /code >}} - ## From a CSV file k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). You can download the library and import it locally like this: -{{< code >}} - ```javascript import papaparse from './papaparse.js'; import { SharedArray } from 'k6/data'; @@ -94,12 +81,8 @@ export default function () { } ``` -{{< /code >}} - Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. -{{< code >}} - ```javascript import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from 'k6/data'; @@ -117,13 +100,9 @@ export default function () { } ``` -{{< /code >}} - Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that data to login to the test.k6.io test site: -{{< code >}} - ```javascript /* Where contents of data.csv is: username,password @@ -171,18 +150,15 @@ export default function () { } ``` -{{< /code >}} - ## Retrieving unique data -It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your dataset. -> ### ⚠️ Multiple scenarios -> -> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. -> That means if you have multiple scenarios in your test you might need to split your data per scenario. +{{< admonition type="note" >}} -{{< code >}} +`scenario.iterationInTest` property is unique _per scenario_, not the overall test. That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< /admonition >}} ```javascript import { SharedArray } from 'k6/data'; @@ -210,15 +186,11 @@ export default function () { } ``` -{{< /code >}} - -Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. +Alternatively, if your use case requires using a unique dataset per VU, you could leverage a property called `vu.idInTest`. In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes a fixed amount of iterations. -{{< code >}} - ```javascript import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; @@ -246,7 +218,33 @@ export default function () { } ``` -{{< /code >}} +If you have a large dataset and want to make sure no two VUs use the same value in the dataset at the same time, while keeping a fixed number of VUs, use the modulo operator (`%`) with the global iteration index. This gives you a round-robin assignment over the dataset and avoids collisions during a run. + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +const vus = 100; +export const options = { + scenarios: { + login: { + executor: 'constant-vus', + vus: vus, + duration: '1h30m', + }, + }, +}; + +export default function () { + console.log(`Users name: ${users[scenario.iterationsInTest % vus].username}`); + sleep(1); +} +``` ## Generating data using faker.js