Skip to content

Commit

Permalink
Added example for running specific scenario via cli (#1762)
Browse files Browse the repository at this point in the history
* Added ex for custom cli env exec.

* fix: reworded several sections for clarity
  • Loading branch information
taylorflatt authored Oct 29, 2024
1 parent 81570ab commit fb698a3
Show file tree
Hide file tree
Showing 9 changed files with 549 additions and 0 deletions.
61 changes: 61 additions & 0 deletions docs/sources/k6/next/using-k6/scenarios/advanced-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,64 @@ export function apitest() {
```

{{< /code >}}

## Run specific scenario via environment variable

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.

The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
{{< code >}}

```javascript
import http from 'k6/http';

const scenarios = {
my_web_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
my_api_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
};

const { SCENARIO } = __ENV;
export const options = {
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
// using the pre-configured scenarios above.
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
discardResponseBodies: true,
thresholds: {
http_req_duration: ['p(95)<250', 'p(99)<350'],
},
};

export default function () {
const response = http.get('https://test-api.k6.io/public/crocodiles/');
}
```

{{< /code >}}

Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:

{{< code >}}

```bash
$ SCENARIO=my_web_test k6 run script.js
```

```windows
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
```

```powershell
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
```

{{< /code >}}
61 changes: 61 additions & 0 deletions docs/sources/k6/v0.47.x/using-k6/scenarios/advanced-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,64 @@ export function apitest() {
```

{{< /code >}}

## Run specific scenario via environment variable

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.

The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
{{< code >}}

```javascript
import http from 'k6/http';

const scenarios = {
my_web_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
my_api_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
};

const { SCENARIO } = __ENV;
export const options = {
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
// using the pre-configured scenarios above.
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
discardResponseBodies: true,
thresholds: {
http_req_duration: ['p(95)<250', 'p(99)<350'],
},
};

export default function () {
const response = http.get('https://test-api.k6.io/public/crocodiles/');
}
```

{{< /code >}}

Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:

{{< code >}}

```bash
$ SCENARIO=my_web_test k6 run script.js
```

```windows
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
```

```powershell
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
```

{{< /code >}}
61 changes: 61 additions & 0 deletions docs/sources/k6/v0.48.x/using-k6/scenarios/advanced-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,64 @@ export function apitest() {
```

{{< /code >}}

## Run specific scenario via environment variable

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.

The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
{{< code >}}

```javascript
import http from 'k6/http';

const scenarios = {
my_web_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
my_api_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
};

const { SCENARIO } = __ENV;
export const options = {
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
// using the pre-configured scenarios above.
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
discardResponseBodies: true,
thresholds: {
http_req_duration: ['p(95)<250', 'p(99)<350'],
},
};

export default function () {
const response = http.get('https://test-api.k6.io/public/crocodiles/');
}
```

{{< /code >}}

Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:

{{< code >}}

```bash
$ SCENARIO=my_web_test k6 run script.js
```

```windows
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
```

```powershell
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
```

{{< /code >}}
61 changes: 61 additions & 0 deletions docs/sources/k6/v0.49.x/using-k6/scenarios/advanced-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,64 @@ export function apitest() {
```

{{< /code >}}

## Run specific scenario via environment variable

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.

The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
{{< code >}}

```javascript
import http from 'k6/http';

const scenarios = {
my_web_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
my_api_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
};

const { SCENARIO } = __ENV;
export const options = {
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
// using the pre-configured scenarios above.
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
discardResponseBodies: true,
thresholds: {
http_req_duration: ['p(95)<250', 'p(99)<350'],
},
};

export default function () {
const response = http.get('https://test-api.k6.io/public/crocodiles/');
}
```

{{< /code >}}

Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:

{{< code >}}

```bash
$ SCENARIO=my_web_test k6 run script.js
```

```windows
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
```

```powershell
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
```

{{< /code >}}
61 changes: 61 additions & 0 deletions docs/sources/k6/v0.50.x/using-k6/scenarios/advanced-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,64 @@ export function apitest() {
```

{{< /code >}}

## Run specific scenario via environment variable

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.

The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
{{< code >}}

```javascript
import http from 'k6/http';

const scenarios = {
my_web_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
my_api_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
};

const { SCENARIO } = __ENV;
export const options = {
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
// using the pre-configured scenarios above.
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
discardResponseBodies: true,
thresholds: {
http_req_duration: ['p(95)<250', 'p(99)<350'],
},
};

export default function () {
const response = http.get('https://test-api.k6.io/public/crocodiles/');
}
```

{{< /code >}}

Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:

{{< code >}}

```bash
$ SCENARIO=my_web_test k6 run script.js
```

```windows
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
```

```powershell
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
```

{{< /code >}}
61 changes: 61 additions & 0 deletions docs/sources/k6/v0.51.x/using-k6/scenarios/advanced-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,64 @@ export function apitest() {
```

{{< /code >}}

## Run specific scenario via environment variable

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.

The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute:
{{< code >}}

```javascript
import http from 'k6/http';

const scenarios = {
my_web_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
my_api_test: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
maxDuration: '30s',
},
};

const { SCENARIO } = __ENV;
export const options = {
// if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run
// using the pre-configured scenarios above.
scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios,
discardResponseBodies: true,
thresholds: {
http_req_duration: ['p(95)<250', 'p(99)<350'],
},
};

export default function () {
const response = http.get('https://test-api.k6.io/public/crocodiles/');
}
```

{{< /code >}}

Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:

{{< code >}}

```bash
$ SCENARIO=my_web_test k6 run script.js
```

```windows
C:\k6> set "SCENARIO=my_web_test" && k6 run script.js
```

```powershell
PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js
```

{{< /code >}}
Loading

0 comments on commit fb698a3

Please sign in to comment.