Skip to content

Commit 643a39d

Browse files
committed
Release v0.54.x documentation
1 parent 24420f2 commit 643a39d

File tree

820 files changed

+62251
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

820 files changed

+62251
-0
lines changed

docs/sources/v0.54.x/_index.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
aliases:
3+
- /docs/k6/
4+
description: 'The k6 documentation covers everything you need to know about k6 OSS, load testing, and performance testing.'
5+
menuTitle: Grafana k6
6+
title: Grafana k6
7+
weight: -10
8+
hero:
9+
title: Grafana k6
10+
level: 1
11+
image: /media/docs/k6/GrafanaLogo_k6_orange_icon.svg
12+
width: 100
13+
height: 100
14+
description: Grafana k6 is an open-source, developer-friendly, and extensible load testing tool. k6 allows you to prevent performance issues and proactively improve reliability.
15+
cards:
16+
title_class: pt-0 lh-1
17+
items:
18+
- title: Run your first k6 test
19+
href: ./get-started/
20+
description: Learn how to install the k6 CLI, run your first k6 test, and view metric results in the terminal.
21+
height: 24
22+
- title: Using k6
23+
href: ./using-k6/
24+
description: Learn about k6 options and concepts such as thresholds, metrics, lifecycle hooks, and more.
25+
height: 24
26+
- title: Testing guides
27+
href: ./testing-guides/
28+
description: Discover how to plan and define your performance testing strategy with these guides.
29+
height: 24
30+
- title: k6 JavaScript API
31+
href: ./javascript-api/
32+
description: Explore the k6 APIs through their documentation and examples.
33+
height: 24
34+
- title: Explore k6 extensions
35+
href: ./extensions/
36+
description: Have a particular testing need? Find k6 extensions that extend the native k6 functionality.
37+
height: 24
38+
- title: k6 script examples
39+
href: ./extensions/
40+
description: Learn how to script your tests with this list of common k6 examples.
41+
height: 24
42+
---
43+
44+
{{< docs/hero-simple key="hero" >}}
45+
46+
---
47+
48+
## Overview
49+
50+
Using k6, you can test the reliability and performance of your application and infrastructure.
51+
52+
k6 helps engineering teams prevent errors and SLO breaches, enabling them to build resilient and high-performing applications that scale.
53+
54+
Engineering teams, including Developers, QA Engineers, SDETs, and SREs, commonly use k6 for:
55+
56+
- **Load and performance testing**
57+
58+
k6 is optimized for minimal resource consumption and designed for running high-load performance tests such as
59+
[spike](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/test-types/spike-testing), [stress](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/test-types/stress-testing), or [soak tests](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/test-types/soak-testing).
60+
61+
- **Browser performance testing**
62+
63+
Through the [k6 browser API](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser), you can run browser-based performance tests and collect browser metrics to identify performance issues related to browsers. Additionally, you can mix browser tests with other performance tests to get a comprehensive view of your website's performance.
64+
65+
- **Performance and synthetic monitoring**
66+
67+
You can schedule tests to run with minimal load very frequently, continuously validating the performance and availability of your production environment. For this, you can also use [Grafana Cloud Synthetic Monitoring](https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/checks/k6/), which supports running k6 scripts.
68+
69+
- **Automation of performance tests**
70+
71+
k6 integrates seamlessly with CI/CD and automation tools, enabling engineering teams to [automate performance testing](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/automated-performance-testing/) as part of their development and release cycle.
72+
73+
- **Chaos and resilience testing**
74+
75+
You can use k6 to simulate traffic as part of your chaos experiments, trigger them from your k6 tests or inject different types of faults in Kubernetes with [xk6-disruptor](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor).
76+
77+
- **Infrastructure testing**
78+
79+
With [k6 extensions](https://grafana.com/docs/k6/<K6_VERSION>/extensions/), you can add support to k6 for new protocols or use a particular client to directly test individual systems within your infrastructure.
80+
81+
Watch the video below to learn more about k6 and why it could be the missing puzzle in your Grafana stack.
82+
83+
{{< youtube id="1mtYVDA2_iQ" >}}
84+
85+
## Explore
86+
87+
{{< card-grid key="cards" type="simple" >}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Examples
3+
weight: 800
4+
---
5+
6+
# Examples
7+
8+
<!-- TODO: Add content -->
9+
10+
{{< section >}}
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
---
2+
title: 'API CRUD Operations'
3+
description: 'This example covers the usage of k6 to test a REST API CRUD operations.'
4+
weight: 10
5+
---
6+
7+
# API CRUD Operations
8+
9+
The examples showcase the testing of CRUD operations on a REST API.
10+
11+
CRUD refers to the basic operations in a database: Create, Read, Update, and Delete. We can map these operations to HTTP methods in REST APIs:
12+
13+
- _Create_: HTTP `POST` operation to create a new resource.
14+
- _Read_: HTTP `GET` to retrieve a resource.
15+
- _Update_: HTTP `PUT`or `PATCH` to change an existing resource.
16+
- _Delete_: HTTP `DELETE` to remove a resource.
17+
18+
This document has two examples, one that uses the core k6 APIs (`k6/http` and `checks`) and another to show the more recent APIs [`httpx`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/httpx) and [`k6chaijs`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6chaijs)).
19+
20+
## Test steps
21+
22+
In the [setup() stage](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/test-lifecycle#setup-and-teardown-stages) we create a user for the [k6 HTTP REST API](https://test-api.k6.io/). We then retrieve and return a bearer token to authenticate the next CRUD requests.
23+
24+
The steps implemented in the [VU stage](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/test-lifecycle#the-vu-stage) are as follows:
25+
26+
1. _Create_ a new resource, a "croc".
27+
2. _Read_ the list of "crocs".
28+
3. _Update_ the name of the "croc" and _read_ the "croc" to confirm the update operation.
29+
4. _Delete_ the "croc" resource.
30+
31+
## Core k6 APIs example
32+
33+
{{< code >}}
34+
35+
```javascript
36+
import http from 'k6/http';
37+
import { check, group, fail } from 'k6';
38+
39+
export const options = {
40+
vus: 1,
41+
iterations: 1,
42+
};
43+
44+
// Create a random string of given length
45+
function randomString(length, charset = '') {
46+
if (!charset) charset = 'abcdefghijklmnopqrstuvwxyz';
47+
let res = '';
48+
while (length--) res += charset[(Math.random() * charset.length) | 0];
49+
return res;
50+
}
51+
52+
const USERNAME = `${randomString(10)}@example.com`; // Set your own email or `${randomString(10)}@example.com`;
53+
const PASSWORD = 'superCroc2019';
54+
55+
const BASE_URL = 'https://test-api.k6.io';
56+
57+
// Register a new user and retrieve authentication token for subsequent API requests
58+
export function setup() {
59+
const res = http.post(`${BASE_URL}/user/register/`, {
60+
first_name: 'Crocodile',
61+
last_name: 'Owner',
62+
username: USERNAME,
63+
password: PASSWORD,
64+
});
65+
66+
check(res, { 'created user': (r) => r.status === 201 });
67+
68+
const loginRes = http.post(`${BASE_URL}/auth/token/login/`, {
69+
username: USERNAME,
70+
password: PASSWORD,
71+
});
72+
73+
const authToken = loginRes.json('access');
74+
check(authToken, { 'logged in successfully': () => authToken !== '' });
75+
76+
return authToken;
77+
}
78+
79+
export default (authToken) => {
80+
// set the authorization header on the session for the subsequent requests
81+
const requestConfigWithTag = (tag) => ({
82+
headers: {
83+
Authorization: `Bearer ${authToken}`,
84+
},
85+
tags: Object.assign(
86+
{},
87+
{
88+
name: 'PrivateCrocs',
89+
},
90+
tag
91+
),
92+
});
93+
94+
let URL = `${BASE_URL}/my/crocodiles/`;
95+
96+
group('01. Create a new crocodile', () => {
97+
const payload = {
98+
name: `Name ${randomString(10)}`,
99+
sex: 'F',
100+
date_of_birth: '2023-05-11',
101+
};
102+
103+
const res = http.post(URL, payload, requestConfigWithTag({ name: 'Create' }));
104+
105+
if (check(res, { 'Croc created correctly': (r) => r.status === 201 })) {
106+
URL = `${URL}${res.json('id')}/`;
107+
} else {
108+
console.log(`Unable to create a Croc ${res.status} ${res.body}`);
109+
return;
110+
}
111+
});
112+
113+
group('02. Fetch private crocs', () => {
114+
const res = http.get(`${BASE_URL}/my/crocodiles/`, requestConfigWithTag({ name: 'Fetch' }));
115+
check(res, { 'retrieved crocs status': (r) => r.status === 200 });
116+
check(res.json(), { 'retrieved crocs list': (r) => r.length > 0 });
117+
});
118+
119+
group('03. Update the croc', () => {
120+
const payload = { name: 'New name' };
121+
const res = http.patch(URL, payload, requestConfigWithTag({ name: 'Update' }));
122+
const isSuccessfulUpdate = check(res, {
123+
'Update worked': () => res.status === 200,
124+
'Updated name is correct': () => res.json('name') === 'New name',
125+
});
126+
127+
if (!isSuccessfulUpdate) {
128+
console.log(`Unable to update the croc ${res.status} ${res.body}`);
129+
return;
130+
}
131+
});
132+
133+
group('04. Delete the croc', () => {
134+
const delRes = http.del(URL, null, requestConfigWithTag({ name: 'Delete' }));
135+
136+
const isSuccessfulDelete = check(null, {
137+
'Croc was deleted correctly': () => delRes.status === 204,
138+
});
139+
140+
if (!isSuccessfulDelete) {
141+
console.log(`Croc was not deleted properly`);
142+
return;
143+
}
144+
});
145+
};
146+
```
147+
148+
{{< /code >}}
149+
150+
## httpx and k6chaijs example
151+
152+
{{< code >}}
153+
154+
```javascript
155+
import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js';
156+
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';
157+
import {
158+
randomIntBetween,
159+
randomItem,
160+
randomString,
161+
} from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
162+
163+
export const options = {
164+
// for the example, let's run only 1 VU with 1 iteration
165+
vus: 1,
166+
iterations: 1,
167+
};
168+
169+
const USERNAME = `user${randomIntBetween(1, 100000)}@example.com`; // Set your own email;
170+
const PASSWORD = 'superCroc2019';
171+
172+
const session = new Httpx({ baseURL: 'https://test-api.k6.io' });
173+
174+
// Register a new user and retrieve authentication token for subsequent API requests
175+
export function setup() {
176+
let authToken = null;
177+
178+
describe(`setup - create a test user ${USERNAME}`, () => {
179+
const resp = session.post(`/user/register/`, {
180+
first_name: 'Crocodile',
181+
last_name: 'Owner',
182+
username: USERNAME,
183+
password: PASSWORD,
184+
});
185+
186+
expect(resp.status, 'User create status').to.equal(201);
187+
expect(resp, 'User create valid json response').to.have.validJsonBody();
188+
});
189+
190+
describe(`setup - Authenticate the new user ${USERNAME}`, () => {
191+
const resp = session.post(`/auth/token/login/`, {
192+
username: USERNAME,
193+
password: PASSWORD,
194+
});
195+
196+
expect(resp.status, 'Authenticate status').to.equal(200);
197+
expect(resp, 'Authenticate valid json response').to.have.validJsonBody();
198+
authToken = resp.json('access');
199+
expect(authToken, 'Authentication token').to.be.a('string');
200+
});
201+
202+
return authToken;
203+
}
204+
205+
export default function (authToken) {
206+
// set the authorization header on the session for the subsequent requests
207+
session.addHeader('Authorization', `Bearer ${authToken}`);
208+
209+
describe('01. Create a new crocodile', (t) => {
210+
const payload = {
211+
name: `Croc name ${randomString(10)}`,
212+
sex: randomItem(['M', 'F']),
213+
date_of_birth: '2023-05-11',
214+
};
215+
216+
session.addTag('name', 'Create');
217+
const resp = session.post(`/my/crocodiles/`, payload);
218+
219+
expect(resp.status, 'Croc creation status').to.equal(201);
220+
expect(resp, 'Croc creation valid json response').to.have.validJsonBody();
221+
222+
session.newCrocId = resp.json('id');
223+
});
224+
225+
describe('02. Fetch private crocs', (t) => {
226+
session.clearTag('name');
227+
const resp = session.get('/my/crocodiles/');
228+
229+
expect(resp.status, 'Fetch croc status').to.equal(200);
230+
expect(resp, 'Fetch croc valid json response').to.have.validJsonBody();
231+
expect(resp.json().length, 'Number of crocs').to.be.above(0);
232+
});
233+
234+
describe('03. Update the croc', (t) => {
235+
const payload = {
236+
name: `New croc name ${randomString(10)}`,
237+
};
238+
239+
const resp = session.patch(`/my/crocodiles/${session.newCrocId}/`, payload);
240+
241+
expect(resp.status, 'Croc patch status').to.equal(200);
242+
expect(resp, 'Fetch croc valid json response').to.have.validJsonBody();
243+
expect(resp.json('name'), 'Croc name').to.equal(payload.name);
244+
245+
// read "croc" again to verify the update worked
246+
const resp1 = session.get(`/my/crocodiles/${session.newCrocId}/`);
247+
248+
expect(resp1.status, 'Croc fetch status').to.equal(200);
249+
expect(resp1, 'Fetch croc valid json response').to.have.validJsonBody();
250+
expect(resp1.json('name'), 'Croc name').to.equal(payload.name);
251+
});
252+
253+
describe('04. Delete the croc', (t) => {
254+
const resp = session.delete(`/my/crocodiles/${session.newCrocId}/`);
255+
256+
expect(resp.status, 'Croc delete status').to.equal(204);
257+
});
258+
}
259+
```
260+
261+
{{< /code >}}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
aliases:
3+
- ./bundling-and-transpilation # docs/k6/<K6_VERSION>/examples/bundling-and-transpilation
4+
title: 'Bundling and transpiling'
5+
description: |
6+
Refer to the JavaScript and TypeScript compatibility mode page for more details on how to use TypeScript with k6
7+
weight: 18
8+
---
9+
10+
# Bundling and transpiling
11+
12+
For more details about bundling and transpiling, refer to [JavaScript and TypeScript compatibility mode](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/javascript-typescript-compatibility-mode/).

0 commit comments

Comments
 (0)