Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example for running specific scenario via cli #1762

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions docs/sources/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

Utilizing an [environment variable](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables) and a bit of code, you can run a specific scenario via command line as opposed to running all configured scenarios. By default, [k6 runs all scenarios listed in a file](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Utilizing an [environment variable](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables) and a bit of code, you can run a specific scenario via command line as opposed to running all configured scenarios. By default, [k6 runs all scenarios listed in a file](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios).
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.

I think if we change the order of the sentences here, it might be a little bit easier to read.


The following provides an example of how to add the ability to run a specific scenario among multiple configured scenarios:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The following provides an example of how to add the ability to run a specific scenario among multiple configured scenarios:
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 supplied via cli env, then run that scenario. Otherwise, run like normal
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// if a scenario is supplied via cli env, then run that scenario. Otherwise, run like normal
// 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 >}}

Alternatively, slightly modifying this approach, it can instead be used as a skip feature. Reading from the custom environment variable allows calling a specific scenario via command line:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Alternatively, slightly modifying this approach, it can instead be used as a skip feature. Reading from the custom environment variable allows calling a specific scenario via command line:
Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running:

I think we can just give an instruction for the user as to how they can execute this from the command line. If we want to mention using this as a skip feature, I think we should do it after the command line examples, or in the first section.


{{< 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/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

Utilizing an [environment variable](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables) and a bit of code, you can run a specific scenario via command line as opposed to running all configured scenarios. By default, [k6 runs all scenarios listed in a file](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios).

The following provides an example of how to add the ability to run a specific scenario among multiple configured scenarios:
{{< 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 supplied via cli env, then run that scenario. Otherwise, run like normal
// 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 >}}

Alternatively, slightly modifying this approach, it can instead be used as a skip feature. Reading from the custom environment variable allows calling a specific scenario via command line:

{{< 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/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

Utilizing an [environment variable](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables) and a bit of code, you can run a specific scenario via command line as opposed to running all configured scenarios. By default, [k6 runs all scenarios listed in a file](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios).

The following provides an example of how to add the ability to run a specific scenario among multiple configured scenarios:
{{< 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 supplied via cli env, then run that scenario. Otherwise, run like normal
// 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 >}}

Alternatively, slightly modifying this approach, it can instead be used as a skip feature. Reading from the custom environment variable allows calling a specific scenario via command line:

{{< 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/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

Utilizing an [environment variable](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables) and a bit of code, you can run a specific scenario via command line as opposed to running all configured scenarios. By default, [k6 runs all scenarios listed in a file](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios).

The following provides an example of how to add the ability to run a specific scenario among multiple configured scenarios:
{{< 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 supplied via cli env, then run that scenario. Otherwise, run like normal
// 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 >}}

Alternatively, slightly modifying this approach, it can instead be used as a skip feature. Reading from the custom environment variable allows calling a specific scenario via command line:

{{< 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/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

Utilizing an [environment variable](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables) and a bit of code, you can run a specific scenario via command line as opposed to running all configured scenarios. By default, [k6 runs all scenarios listed in a file](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios).

The following provides an example of how to add the ability to run a specific scenario among multiple configured scenarios:
{{< 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 supplied via cli env, then run that scenario. Otherwise, run like normal
// 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 >}}

Alternatively, slightly modifying this approach, it can instead be used as a skip feature. Reading from the custom environment variable allows calling a specific scenario via command line:

{{< 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/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

Utilizing an [environment variable](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/#environment-variables) and a bit of code, you can run a specific scenario via command line as opposed to running all configured scenarios. By default, [k6 runs all scenarios listed in a file](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios/#scenarios).

The following provides an example of how to add the ability to run a specific scenario among multiple configured scenarios:
{{< 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 supplied via cli env, then run that scenario. Otherwise, run like normal
// 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 >}}

Alternatively, slightly modifying this approach, it can instead be used as a skip feature. Reading from the custom environment variable allows calling a specific scenario via command line:

{{< 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
Loading