Skip to content

Commit

Permalink
add basic slides for a beginner workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
mdcruz committed Sep 8, 2023
1 parent baa96d9 commit 70ddcc1
Show file tree
Hide file tree
Showing 15 changed files with 531 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ This repo contains resources for:

Consider running a workshop for k6. Below is an outline of what that workshop could look like, as well as modules you could use for each topic. Feel free to take these and include the parts most relevant to you!

We have also created built-in slides for you, which you are free to edit. Please fork this repo and make the edits accordingly how you want the workshop to be structure.
We have also created built-in slides for you, which you are free to edit. For a more custom slides, please fork this repo and make the edits accordingly how you want the workshop to be structured.

The slides are created using [reveal.js](https://revealjs.com/).
The slides are created using [reveal.js](https://revealjs.com/) and are all found in the [slides](./slides/) folder.

### Running the slides

Expand Down
Binary file added images/bert-relaxing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/grafana-cloud-k6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/k6-end-of-summary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/k6-order-of-preference-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/k6-output-options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 2 additions & 5 deletions slides/05-the-k6-cli/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,8 @@ k6 run test.js -e DOMAIN=test.k6.io
k6 always [prioritizes settings](https://k6.io/docs/using-k6/k6-options/how-to/#order-of-precedence) in this order:
1. Command-line flags
1. Environment variables
1. Exported k6 script options
1. Config file
1. Defaults
![Priority of configurations and settings in k6](../../images/k6-order-of-preference-settings.png)
<!-- .element class="stretch" -->
---
Expand Down
10 changes: 8 additions & 2 deletions slides/06-understanding-k6-results/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ http_req_duration..............: avg=130.19ms min=130.19ms med=130.19ms max=130.
---
`http_req_duration` is the value for *all* requests, whether or not they passed.
### http_req_duration
> 💡 `http_req_duration` is the value for *all* requests.
The line below reports the response time for *only* the successful requests.
Expand Down Expand Up @@ -104,4 +106,8 @@ The number of iterations describes how many times k6 looped through your script
iterations.....................: 1 1.525116/s
```
---
---
## Adding checks to your k6 script
- Move to: [07-adding-checks-to-your-script](?p=07-adding-checks-to-your-script)
75 changes: 75 additions & 0 deletions slides/07-adding-checks-to-your-script/slides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# k6 checks

---

## Our script

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

export default function() {
let url = 'https://httpbin.test.k6.io/post';
let response = http.post(url, 'Hello world!');

console.log(response.json().data);
}
```

---

## Add checks to your script

```js [1|3-6]
import { check } from 'k6';

check(response, {
'Application says hello': (r) => r.body.includes('Hello world!')
});
}
```

---

## Let's run our test again!

Do you remember the command to run the test? 👀

```shell
✓ Application says hello

checks.........................: 100.00% ✓ 1 ✗ 0
```

---

## Failed checks

```js
check(response, {
'Application says hello': (r) => r.body.includes('Bonjour!')
});
}
```

---

## Let's run our test again!

```shell
✗ Application says hello
↳ 0% — ✓ 0 / ✗ 1

checks.........................: 0.00% ✓ 0 ✗ 1
```

---

## Failed checks are not errors!

> 💡 To make failing checks stop your test, you can [combine them with thresholds](https://k6.io/docs/using-k6/thresholds/#failing-a-load-test-using-checks).
---

## Making your scripts realistic with think time

- Move to: [08-adding-think-time](?p=08-adding-think-time)
84 changes: 84 additions & 0 deletions slides/08-adding-think-time/slides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Think time

---

## What is think time?

The amount of time that a script pauses during test execution to simulate delays that real users have in the course of using an application.

---

## When should you use think time?

- If your test follows a user flow
- If you want to simulate actions that take some time to carry out
- Your load generator, or the machine you're running k6 from, displays high (> 80%) CPU utilization during test execution.

---

## When should you **NOT** use think time?

- You want to do a [stress test](https://k6.io/docs/test-types/stress-testing/)
- The API endpoint you're testing experiences a high amount of requests per second in production that occur without delays
- Your load generator can run your test script without crossing the 80% CPU utilization mark.

---

## k6 Sleep

```js [2|11]
import http from 'k6/http';
import { check, sleep } from 'k6';

export default function() {
let url = 'https://httpbin.test.k6.io/post';
let response = http.post(url, 'Hello world!');
check(response, {
'Application says hello': (r) => r.body.includes('Hello world!')
});

sleep(1);
}
```

---

**Sleep does not affect the response time (`http_req_duration`); the response time is always reported with sleep removed. Sleep is, however, included in the `iteration_duration`.**

---

## Dynamic think time

A dynamic think time is more realistic, and simulates real users more accurately.

---

### Random sleep

One way to implement dynamic think time is to use the JavaScript `Math.random()` function:

```js
sleep(Math.random() * 5);
```

---

### Random sleep between

```js [1|3]
import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.0.0/index.js";

sleep(randomIntBetween(1,5));
```

---

## How much think time should you add?

The real answer is: it depends.

---

## Let's move to scaling up your test

- Move to: [09-load-test-options](?p=09-load-test-options)
130 changes: 130 additions & 0 deletions slides/09-load-test-options/slides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# k6 Load Test Options

---

## Script options

```js
export let options = {
vus: 10,
iterations: 40,
};
```

---

> 💡 If you only define VUs and no other test options, you may get the following error:
```shell
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io

WARN[0000] the `vus=10` option will be ignored, it only works in conjunction with `iterations`, `duration`, or `stages`
execution: local
script: test.js
output: -
```
---
## Iterations
```js
vus: 10,
iterations: 40,
```
> Setting the number of iterations in test options defines it for **all** users.
---
## Duration
```js
vus: 10,
duration: '2m'
```
> Setting the duration instructs k6 to repeat the script for each of the VUs until the duration is reached.
---
## Iterations and durations
```js
vus: 10,
duration: '5m',
iterations: 40,
```
> If you set the duration in conjunction with setting the number of iterations, the value that ends earlier is used.
---
## Stages
Defining iterations and duration creates a _simple load profile_
![A simple load profile](../../images/load_profile-no_ramp-up_or_ramp-down.png)
<!-- .element class="stretch" -->
---
## Constand load profile
What if you want to add a ramp-up or ramp-down, so that the profile looks more like this?
![Constant load profile, with ramps](../../images/load_profile-constant.png.png)
<!-- .element class="stretch" -->
---
In that case, you may want to use [stages](https://k6.io/docs/using-k6/options/#stages).
```js
export let options = {
stages: [
{ duration: '30m', target: 100 },
{ duration: '1h', target: 100 },
{ duration: '5m', target: 0 },
],
};
```
---
## The full script so far
If you're using stages, here's what your script should look like so far:
```js
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
stages: [
{ duration: '30m', target: 100 },
{ duration: '1h', target: 100 },
{ duration: '5m', target: 0 },
],
};
export default function() {
let url = 'https://httpbin.test.k6.io/post';
let response = http.post(url, 'Hello world!');
check(response, {
'Application says hello': (r) => r.body.includes('Hello world!')
});
sleep(Math.random() * 5);
}
```
---
## Let's set some thresholds
- Move to [10-setting-test-criteria-with-thresholds](?p=10-setting-test-criteria-with-thresholds)
Loading

0 comments on commit 70ddcc1

Please sign in to comment.