Skip to content

Commit fb698a3

Browse files
authored
Added example for running specific scenario via cli (#1762)
* Added ex for custom cli env exec. * fix: reworded several sections for clarity
1 parent 81570ab commit fb698a3

File tree

9 files changed

+549
-0
lines changed

9 files changed

+549
-0
lines changed

docs/sources/k6/next/using-k6/scenarios/advanced-examples.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,64 @@ export function apitest() {
201201
```
202202

203203
{{< /code >}}
204+
205+
## Run specific scenario via environment variable
206+
207+
k6 runs all [scenarios](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios) listed in a test script by default. But, with some small code changes and using [environment variables](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables), you can tell k6 to only run a specific scenario via the command-line.
208+
209+
The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
210+
{{< code >}}
211+
212+
```javascript
213+
import http from 'k6/http';
214+
215+
const scenarios = {
216+
my_web_test: {
217+
executor: 'per-vu-iterations',
218+
vus: 1,
219+
iterations: 1,
220+
maxDuration: '30s',
221+
},
222+
my_api_test: {
223+
executor: 'per-vu-iterations',
224+
vus: 1,
225+
iterations: 1,
226+
maxDuration: '30s',
227+
},
228+
};
229+
230+
const { SCENARIO } = __ENV;
231+
export const options = {
232+
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
233+
// using the pre-configured scenarios above.
234+
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
235+
discardResponseBodies: true,
236+
thresholds: {
237+
http_req_duration: ['p(95)<250', 'p(99)<350'],
238+
},
239+
};
240+
241+
export default function () {
242+
const response = http.get('https://test-api.k6.io/public/crocodiles/');
243+
}
244+
```
245+
246+
{{< /code >}}
247+
248+
Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:
249+
250+
{{< code >}}
251+
252+
```bash
253+
$ SCENARIO=my_web_test k6 run script.js
254+
```
255+
256+
```windows
257+
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
258+
```
259+
260+
```powershell
261+
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
262+
```
263+
264+
{{< /code >}}

docs/sources/k6/v0.47.x/using-k6/scenarios/advanced-examples.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,64 @@ export function apitest() {
201201
```
202202

203203
{{< /code >}}
204+
205+
## Run specific scenario via environment variable
206+
207+
k6 runs all [scenarios](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios) listed in a test script by default. But, with some small code changes and using [environment variables](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables), you can tell k6 to only run a specific scenario via the command-line.
208+
209+
The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
210+
{{< code >}}
211+
212+
```javascript
213+
import http from 'k6/http';
214+
215+
const scenarios = {
216+
my_web_test: {
217+
executor: 'per-vu-iterations',
218+
vus: 1,
219+
iterations: 1,
220+
maxDuration: '30s',
221+
},
222+
my_api_test: {
223+
executor: 'per-vu-iterations',
224+
vus: 1,
225+
iterations: 1,
226+
maxDuration: '30s',
227+
},
228+
};
229+
230+
const { SCENARIO } = __ENV;
231+
export const options = {
232+
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
233+
// using the pre-configured scenarios above.
234+
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
235+
discardResponseBodies: true,
236+
thresholds: {
237+
http_req_duration: ['p(95)<250', 'p(99)<350'],
238+
},
239+
};
240+
241+
export default function () {
242+
const response = http.get('https://test-api.k6.io/public/crocodiles/');
243+
}
244+
```
245+
246+
{{< /code >}}
247+
248+
Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:
249+
250+
{{< code >}}
251+
252+
```bash
253+
$ SCENARIO=my_web_test k6 run script.js
254+
```
255+
256+
```windows
257+
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
258+
```
259+
260+
```powershell
261+
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
262+
```
263+
264+
{{< /code >}}

docs/sources/k6/v0.48.x/using-k6/scenarios/advanced-examples.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,64 @@ export function apitest() {
201201
```
202202

203203
{{< /code >}}
204+
205+
## Run specific scenario via environment variable
206+
207+
k6 runs all [scenarios](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios) listed in a test script by default. But, with some small code changes and using [environment variables](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables), you can tell k6 to only run a specific scenario via the command-line.
208+
209+
The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
210+
{{< code >}}
211+
212+
```javascript
213+
import http from 'k6/http';
214+
215+
const scenarios = {
216+
my_web_test: {
217+
executor: 'per-vu-iterations',
218+
vus: 1,
219+
iterations: 1,
220+
maxDuration: '30s',
221+
},
222+
my_api_test: {
223+
executor: 'per-vu-iterations',
224+
vus: 1,
225+
iterations: 1,
226+
maxDuration: '30s',
227+
},
228+
};
229+
230+
const { SCENARIO } = __ENV;
231+
export const options = {
232+
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
233+
// using the pre-configured scenarios above.
234+
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
235+
discardResponseBodies: true,
236+
thresholds: {
237+
http_req_duration: ['p(95)<250', 'p(99)<350'],
238+
},
239+
};
240+
241+
export default function () {
242+
const response = http.get('https://test-api.k6.io/public/crocodiles/');
243+
}
244+
```
245+
246+
{{< /code >}}
247+
248+
Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:
249+
250+
{{< code >}}
251+
252+
```bash
253+
$ SCENARIO=my_web_test k6 run script.js
254+
```
255+
256+
```windows
257+
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
258+
```
259+
260+
```powershell
261+
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
262+
```
263+
264+
{{< /code >}}

docs/sources/k6/v0.49.x/using-k6/scenarios/advanced-examples.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,64 @@ export function apitest() {
201201
```
202202

203203
{{< /code >}}
204+
205+
## Run specific scenario via environment variable
206+
207+
k6 runs all [scenarios](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios) listed in a test script by default. But, with some small code changes and using [environment variables](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables), you can tell k6 to only run a specific scenario via the command-line.
208+
209+
The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
210+
{{< code >}}
211+
212+
```javascript
213+
import http from 'k6/http';
214+
215+
const scenarios = {
216+
my_web_test: {
217+
executor: 'per-vu-iterations',
218+
vus: 1,
219+
iterations: 1,
220+
maxDuration: '30s',
221+
},
222+
my_api_test: {
223+
executor: 'per-vu-iterations',
224+
vus: 1,
225+
iterations: 1,
226+
maxDuration: '30s',
227+
},
228+
};
229+
230+
const { SCENARIO } = __ENV;
231+
export const options = {
232+
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
233+
// using the pre-configured scenarios above.
234+
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
235+
discardResponseBodies: true,
236+
thresholds: {
237+
http_req_duration: ['p(95)<250', 'p(99)<350'],
238+
},
239+
};
240+
241+
export default function () {
242+
const response = http.get('https://test-api.k6.io/public/crocodiles/');
243+
}
244+
```
245+
246+
{{< /code >}}
247+
248+
Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:
249+
250+
{{< code >}}
251+
252+
```bash
253+
$ SCENARIO=my_web_test k6 run script.js
254+
```
255+
256+
```windows
257+
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
258+
```
259+
260+
```powershell
261+
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
262+
```
263+
264+
{{< /code >}}

docs/sources/k6/v0.50.x/using-k6/scenarios/advanced-examples.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,64 @@ export function apitest() {
201201
```
202202

203203
{{< /code >}}
204+
205+
## Run specific scenario via environment variable
206+
207+
k6 runs all [scenarios](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios) listed in a test script by default. But, with some small code changes and using [environment variables](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables), you can tell k6 to only run a specific scenario via the command-line.
208+
209+
The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
210+
{{< code >}}
211+
212+
```javascript
213+
import http from 'k6/http';
214+
215+
const scenarios = {
216+
my_web_test: {
217+
executor: 'per-vu-iterations',
218+
vus: 1,
219+
iterations: 1,
220+
maxDuration: '30s',
221+
},
222+
my_api_test: {
223+
executor: 'per-vu-iterations',
224+
vus: 1,
225+
iterations: 1,
226+
maxDuration: '30s',
227+
},
228+
};
229+
230+
const { SCENARIO } = __ENV;
231+
export const options = {
232+
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
233+
// using the pre-configured scenarios above.
234+
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
235+
discardResponseBodies: true,
236+
thresholds: {
237+
http_req_duration: ['p(95)<250', 'p(99)<350'],
238+
},
239+
};
240+
241+
export default function () {
242+
const response = http.get('https://test-api.k6.io/public/crocodiles/');
243+
}
244+
```
245+
246+
{{< /code >}}
247+
248+
Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:
249+
250+
{{< code >}}
251+
252+
```bash
253+
$ SCENARIO=my_web_test k6 run script.js
254+
```
255+
256+
```windows
257+
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
258+
```
259+
260+
```powershell
261+
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
262+
```
263+
264+
{{< /code >}}

docs/sources/k6/v0.51.x/using-k6/scenarios/advanced-examples.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,64 @@ export function apitest() {
201201
```
202202

203203
{{< /code >}}
204+
205+
## Run specific scenario via environment variable
206+
207+
k6 runs all [scenarios](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios) listed in a test script by default. But, with some small code changes and using [environment variables](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables), you can tell k6 to only run a specific scenario via the command-line.
208+
209+
The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
210+
{{< code >}}
211+
212+
```javascript
213+
import http from 'k6/http';
214+
215+
const scenarios = {
216+
my_web_test: {
217+
executor: 'per-vu-iterations',
218+
vus: 1,
219+
iterations: 1,
220+
maxDuration: '30s',
221+
},
222+
my_api_test: {
223+
executor: 'per-vu-iterations',
224+
vus: 1,
225+
iterations: 1,
226+
maxDuration: '30s',
227+
},
228+
};
229+
230+
const { SCENARIO } = __ENV;
231+
export const options = {
232+
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
233+
// using the pre-configured scenarios above.
234+
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
235+
discardResponseBodies: true,
236+
thresholds: {
237+
http_req_duration: ['p(95)<250', 'p(99)<350'],
238+
},
239+
};
240+
241+
export default function () {
242+
const response = http.get('https://test-api.k6.io/public/crocodiles/');
243+
}
244+
```
245+
246+
{{< /code >}}
247+
248+
Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:
249+
250+
{{< code >}}
251+
252+
```bash
253+
$ SCENARIO=my_web_test k6 run script.js
254+
```
255+
256+
```windows
257+
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
258+
```
259+
260+
```powershell
261+
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
262+
```
263+
264+
{{< /code >}}

0 commit comments

Comments
 (0)