Skip to content

Commit 034b0e9

Browse files
MattDodsonEnglishmstoykovppcanoimmavalls
authored
Tutorial: Get started with k6 (#1192)
Test as code tutorial / getting started with k6 --------- Co-authored-by: Mihail Stoykov <[email protected]> Co-authored-by: Pepe Cano <[email protected]> Co-authored-by: Imma Valls <[email protected]>
1 parent 3095287 commit 034b0e9

20 files changed

+1316
-38
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ docs/sources/get-started/run-your-first-tests.md
66
CONTRIBUTING_FILE_FORMAT.md
77
src/data/markdown/docs/40 xk6-disruptor/04 Examples/01 Inject Grpc faults into Service.md
88
src/data/markdown/docs/40 xk6-disruptor/04 Examples/02 Inject HTTP faults into Pod.md
9+
src/data/markdown/docs/05 Examples/02 Tutorials/01 Get started with k6/100 Test-for-functional-behavior.md
10+
src/data/markdown/docs/05 Examples/02 Tutorials/01 Get started with k6/200 Test for performance.md
11+
src/data/markdown/docs/05 Examples/02 Tutorials/01 Get started with k6/400 Reuse and re-run tests.md

src/data/doc-examples/links.json

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,55 +141,61 @@
141141
],
142142
"tutorials": [
143143
{
144-
"title": "How to use Datadog alerts and Thresholds to fail your load test",
145-
"description": "This article will show how to use Datadog alerts and Thresholds to fail your load test.",
144+
"title": "Get started with k6",
145+
"description": "This tutorial provides some procedures for common real-life uses of k6.",
146+
"to": "/examples/tutorials/get-started-with-k6/",
147+
"url": ""
148+
},
149+
{
150+
"title": "8 basic tasks to learn k6",
151+
"description": "Challenges are great ways to learn. All the tasks in this blog provide a little context about particular k6 features.",
146152
"to": "",
147-
"url": "https://k6.io/blog/datadog-alerts-fail-load-test"
153+
"url": "https://k6.io/blog/learning-js-through-load-testing/"
148154
},
149155
{
150-
"title": "Load Testing Your API with Postman",
151-
"description": "How to use postman collections in k6?",
156+
"title": "k6 Learn",
157+
"description": "A set of modules to learn about performance testing principles, k6 foundations, and intermediate concepts.",
152158
"to": "",
153-
"url": "https://k6.io/blog/load-testing-with-postman-collections"
159+
"url": "https://github.com/grafana/k6-learn"
154160
},
155161
{
156-
"title": "Load testing with Swagger/OpenAPI",
162+
"title": "Swagger/OpenAPI integration",
157163
"description": "How to generate a load test script out of an OpenAPI document.",
158164
"to": "",
159165
"url": "https://k6.io/blog/load-testing-your-api-with-swagger-openapi-and-k6"
160166
},
161167
{
162-
"title": "Performance monitoring with cron and k6",
168+
"title": "Schedule k6 tests with cron",
163169
"description": "This tutorial will show you how to schedule k6 tests with cron to monitor the performance of your system.",
164170
"to": "",
165171
"url": "https://k6.io/blog/performance-monitoring-with-cron-and-k6"
166172
},
167173
{
168-
"title": "Load testing GraphQL with k6",
169-
"description": "The popularity and adoption of GraphQL have been impressive in the last years, this post provides an example using k6 to load testing a GraphQL service.",
174+
"title": "Load test a GraphQL service",
175+
"description": "This post provides an example using k6 to load testing a GraphQL service.",
170176
"to": "",
171177
"url": "https://k6.io/blog/load-testing-graphql-with-k6"
172178
},
173179
{
174-
"title": "Load Testing RESTful APIs with k6",
175-
"description": "Let's take a look at how we can use the k6 open source load testing tool to performance test REST API endpoints.",
176-
"to": "",
177-
"url": "https://k6.io/blog/load-testing-restful-apis-with-k6"
178-
},
179-
{
180-
"title": "Using TypeScript in k6 scripts",
180+
"title": "Use TypeScript in k6 scripts",
181181
"description": "This repository provides a scaffolding project to start using Typescript in your k6 scripts.",
182182
"to": "",
183183
"url": "https://github.com/k6io/template-typescript"
184184
},
185185
{
186-
"title": "Debugging Using a Web Proxy",
187-
"description": "How to configure k6 to send HTTP traffic through a web proxy. Things to look out for when debugging your performance test.",
186+
"title": "Debug using a Web Proxy",
187+
"description": "How to configure k6 to send HTTP traffic through a web proxy for debugging k6 tests.",
188188
"to": "",
189189
"url": "https://k6.io/blog/k6-load-testing-debugging-using-a-web-proxy/"
190190
},
191191
{
192-
"title": "Creating a k6 extension",
192+
"title": "Distributed k6 tests on K8s",
193+
"description": "Deploy the k6 operator in your Kubernetes cluster to run distributed k6 tests.",
194+
"to": "",
195+
"url": "https://k6.io/blog/running-distributed-tests-on-k8s/"
196+
},
197+
{
198+
"title": "Create a k6 extension",
193199
"description": "This short tutorial will walk you through the steps to build a k6 extension for Redis and use it in your k6 test.",
194200
"to": "",
195201
"url": "https://k6.io/blog/extending-k6-with-xk6"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: 'Tutorials'
3+
excerpt: 'k6 Tutorials'
4+
---
5+
6+
- [Get started with k6](/examples/tutorials/get-started-with-k6/)
7+
- [8 basic tasks to learn k6](https://k6.io/blog/learning-js-through-load-testing/)
8+
- [k6 Learn](https://github.com/grafana/k6-learn)
9+
- [Swagger/OpenAPI integration](https://k6.io/blog/load-testing-your-api-with-swagger-openapi-and-k6)
10+
- [Schedule k6 tests with cron](https://k6.io/blog/performance-monitoring-with-cron-and-k6)
11+
- [Load test a GraphQL service](https://k6.io/blog/load-testing-graphql-with-k6)
12+
- [Use TypeScript in k6 scripts](https://github.com/k6io/template-typescript)
13+
- [Debug using a Web Proxy](https://k6.io/blog/k6-load-testing-debugging-using-a-web-proxy/)
14+
- [Distributed k6 tests on K8s](https://k6.io/blog/running-distributed-tests-on-k8s/)
15+
- [Create a k6 extension](https://k6.io/blog/extending-k6-with-xk6)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: 'Get started with k6'
3+
excerpt: A series of docs to learn how to use the major features of k6
4+
---
5+
6+
This tutorial provides some procedures for common real-life uses of k6.
7+
They assume no prior knowledge of k6 or of JavaScript.
8+
9+
These tasks are all reproducible, so open up your favorite editor and follow along.
10+
Imagine that you are a developer or tester who is responsible for performance in your development team.
11+
12+
## Context: test a new endpoint
13+
14+
Your development team has just developed a new login endpoint.
15+
Before releasing, you must test the endpoint for the following questions:
16+
- Does it work?
17+
- Does it perform within the service-level objectives under average load?
18+
- At what load does performance degrade beyond objectives?
19+
20+
After you test the endpoint, your team wants you to compare different components of the user-facing application.
21+
22+
Finally, after you test the API and web application, break your scripts down into reusable parts.
23+
24+
## Before you start
25+
26+
We encourage you to open your terminal and actively experiment with these examples.
27+
The tutorial requires the following:
28+
- [ ] [k6 installed](/get-started/installation)
29+
- [ ] A clean directory to experiment in.
30+
- [ ] Something to do during the minute or two when k6 runs the longest example tests
31+
- [ ] Optional: [`jq`](https://stedolan.github.io/jq/) to filter some results
32+
33+
## Tutorials
34+
35+
| In this tutorial | Learn how to |
36+
|---------------------------------------------------------------------------------|--------------------------------------------------------------------|
37+
| [Test for functional behavior](/examples/tutorials/get-started-with-k6/test-for-functional-behavior) | Use k6 to script requests and evaluate that performance is correct |
38+
| [Test for performance](/examples/tutorials/get-started-with-k6/test-for-performance) | Use k6 to increase load and find faults |
39+
| [Analyze results](/examples/tutorials/get-started-with-k6/analyze-results) | Filter results and make custom metrics |
40+
| [Reuse and re-run](/examples/tutorials/get-started-with-k6/reuse-and-re-run-tests) | Modularize and re-run tests |
41+
42+
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
title: Test for functional behavior
3+
excerpt: Use k6 to write requests and assert that they respond correctly
4+
---
5+
6+
In this tutorial, learn how to write a test that does the following:
7+
- Sends a POST request to the new endpoint
8+
- Creates a check for the response status
9+
10+
## Script the Request
11+
12+
The first thing to do is to add logic for the endpoint.
13+
To do that, you need to make an [HTTP request](/using-k6/http-requests):
14+
1. Import the HTTP module.
15+
2. Create a payload to authenticate the user.
16+
3. Use the [`http.post`](/javascript-api/k6-http/post/) method to send your request with the payload to an endpoint.
17+
18+
To test, copy this file and save it as `api-test.js`.
19+
20+
<CodeGroup labels={["api-test.js"]} lineNumbers={[]} showCopyButton={[true]}>
21+
22+
```javascript
23+
// import necessary module
24+
import http from "k6/http";
25+
26+
export default function () {
27+
28+
// define URL and payload
29+
const url = "https://test-api.k6.io/auth/basic/login/";
30+
const payload = JSON.stringify({
31+
username: "test_case",
32+
password: "1234",
33+
});
34+
35+
const params = {
36+
headers: {
37+
"Content-Type": "application/json",
38+
},
39+
};
40+
41+
// send a post request and save response as a variable
42+
const res = http.post(url, payload, params);
43+
}
44+
```
45+
46+
</CodeGroup>
47+
48+
Run the script using the `k6 run` command:
49+
50+
```bash
51+
k6 run api-test.js
52+
```
53+
54+
After the test finishes, k6 reports the [default result summary](/results-output/end-of-test/#the-default-summary).
55+
56+
```bash
57+
/\ |‾‾| /‾‾/ /‾‾/
58+
/\ / \ | |/ / / /
59+
/ \/ \ | ( / ‾‾\
60+
/ \ | |\ \ | (‾) |
61+
/ __________ \ |__| \__\ \_____/ .io
62+
63+
execution: local
64+
script: api-test.js
65+
output: -
66+
...
67+
```
68+
69+
To make sure you're getting the right response, you could log the response body to the console as follows:
70+
71+
```javascript
72+
const res = http.post(url, payload, params);
73+
console.log(res.body);
74+
```
75+
76+
## Add response checks
77+
78+
Once you're sure the request is well-formed, add a [check](/using-k6/checks) that validates whether the system responds with the expected status code.
79+
80+
1. Update your script so it has the following check function.
81+
82+
<CodeGroup labels={["checks response is 200"]} lineNumbers={["true"]} showCopyButton={[true]}>
83+
84+
```javascript
85+
// Import necessary modules
86+
import { check } from "k6";
87+
import http from "k6/http";
88+
89+
export default function () {
90+
// define URL and request body
91+
const url = "https://test-api.k6.io/auth/basic/login/";
92+
const payload = JSON.stringify({
93+
username: "test_case",
94+
password: "1234",
95+
});
96+
const params = {
97+
headers: {
98+
"Content-Type": "application/json",
99+
},
100+
};
101+
102+
// send a post request and save response as a variable
103+
const res = http.post(url, payload, params);
104+
105+
// check that response is 200
106+
check(res, {
107+
"response code was 200": (res) => res.status == 200,
108+
});
109+
}
110+
```
111+
112+
</CodeGroup>
113+
114+
1. Inspect the result output for your check.
115+
It should look something like this.
116+
117+
```
118+
✓ response code was 200
119+
```
120+
121+
122+
<Blockquote mod="note" title="">
123+
124+
Under larger loads, this check will fail in some iterations.
125+
**Failed checks do not stop tests.**
126+
127+
Rather, k6 tracks the success rate and presents it in your [end of test](/results-output/end-of-test/) summary.
128+
129+
</Blockquote>
130+
131+
## Next steps
132+
133+
In this tutorial, you've used k6 to make a POST request and check that it responds with a `200` status.
134+
135+
However, these tests make only one request, which doesn't say much about how the system will respond under load.
136+
For that, you need to [test under load](/examples/tutorials/get-started-with-k6/test-for-performance/).

0 commit comments

Comments
 (0)