Grafana K6 is an open-source tool designed for performance testing. It's great for testing APIs, microservices, and websites at scale, providing developers and testers insights into system performance. This cheat sheet will cover the key aspects every performance engineer should know to get started with Grafana K6.
Grafana K6 is a modern load testing tool for developers and testers that makes performance testing simple, scalable, and easy to integrate into your CI pipeline.
Install Grafana K6 via Homebrew or Docker:
brew install k6 # Or with Docker docker run -i grafana/k6 run - <script.js
Here's how to run a simple test using a public REST API.
import http from "k6/http"; import { check, sleep } from "k6"; // Define the API endpoint and expected response export default function () { const res = http.get("https://jsonplaceholder.typicode.com/posts/1"); // Define the expected response const expectedResponse = { userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto", }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); sleep(1); }
To run the test and view the results in a web dashboard, we can use the following command:
K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run ./src/rest/jsonplaceholder-api-rest.js
This will generate a report in the reports folder with the name html-report.html.
But we also can see the results in the web dashboard by accessing the following URL:
http://127.0.0.1:5665/
Once we access the URL, we can see the results on real time of the test in the web dashboard.
Example using a public GraphQL API.
If you don't know what is a GraphQL API, you can visit the following URL: What is GraphQL?.
For more information about the GraphQL API we are going to use, you can visit the documentation of the following URL: GraphQL Pokémon.
For more information about how to test GraphQL APIs, you can visit the following URL: GraphQL Testing.
This is a simple test to get a pokemon by name and check if the response is successful.
import http from "k6/http"; import { check } from "k6"; // Define the query and variables const query = ` query getPokemon($name: String!) { pokemon(name: $name) { id name types } }`; const variables = { name: "pikachu", }; // Define the test function export default function () { const url = "https://graphql-pokemon2.vercel.app/"; const payload = JSON.stringify({ query: query, variables: variables, }); // Define the headers const headers = { "Content-Type": "application/json", }; // Make the request const res = http.post(url, payload, { headers: headers }); // Define the expected response const expectedResponse = { data: { pokemon: { id: "UG9rZW1vbjowMjU=", name: "Pikachu", types: ["Electric"], }, }, }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); }
Define global configurations options such as performance thresholds, the number of virtual users (VU), and durations in one place for easy modification.
brew install k6 # Or with Docker docker run -i grafana/k6 run - <script.js
Separate code into reusable modules, for example, separating constants and requests from test logic.
For our REST API example, we can create a constants.js file to store the base URL of the API and a requests-jsonplaceholder.js file to store the functions to interact with the API.
import http from "k6/http"; import { check, sleep } from "k6"; // Define the API endpoint and expected response export default function () { const res = http.get("https://jsonplaceholder.typicode.com/posts/1"); // Define the expected response const expectedResponse = { userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto", }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); sleep(1); }
Now we can create the requests-jsonplaceholder.js file to store the functions to interact with the API.
K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run ./src/rest/jsonplaceholder-api-rest.js
Finally, we can create our test script jsonplaceholder-api-rest.js to use the functions we created in the requests-jsonplaceholder.js file.
http://127.0.0.1:5665/
Our script code is now much simpler to understand, and if something changes in the URL, parameters or if a new method needs to be added, the place where the changes need to be made is centralised, making our solution simpler to extend over time.
We could further improve our scripts by creating more atomic functions that we can reuse to create more complex scenarios in the future if necessary, it is getting simpler to understand what our test script does. For example if we wanted to test the existence of a post, we could create a function that gets a post and returns the response, then we could use this function in our test script jsonplaceholder-api-rest.js.
import http from "k6/http"; import { check } from "k6"; // Define the query and variables const query = ` query getPokemon($name: String!) { pokemon(name: $name) { id name types } }`; const variables = { name: "pikachu", }; // Define the test function export default function () { const url = "https://graphql-pokemon2.vercel.app/"; const payload = JSON.stringify({ query: query, variables: variables, }); // Define the headers const headers = { "Content-Type": "application/json", }; // Make the request const res = http.post(url, payload, { headers: headers }); // Define the expected response const expectedResponse = { data: { pokemon: { id: "UG9rZW1vbjowMjU=", name: "Pikachu", types: ["Electric"], }, }, }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); }
We can modify the constants.js file to add the base URL of the GraphQL API and the headers we need to use.
// ./src/config/options.js export const options = { stages: [ { duration: '1m', target: 100 }, // ramp up to 100 VUs { duration: '5m', target: 100 }, // stay at 100 VUs for 5 mins { duration: '1m', target: 0 }, // ramp down ], thresholds: { http_req_duration: ['p(95)<500'], // 95% of requests should complete in under 500ms }, };
Now we can create the requests-graphql-pokemon.js file to store the functions to interact with the GraphQL API.
// ./src/utils/constants.js export const BASE_URLS = { REST_API: 'https://jsonplaceholder.typicode.com', };
In this moment we can create our test script to use the functions we created in the requests-graphql-pokemon.js file. We will create a simple test script that will get the data of a pokemon and check if the response is successful.
// ./src/utils/requests-jsonplaceholder.js import { BASE_URLS } from './constants.js'; import http from 'k6/http'; export function getPosts() { return http.get(`${BASE_URLS.REST_API}/posts`); } export function getPost(id) { return http.get(`${BASE_URLS.REST_API}/posts/${id}`); } export function createPost(post) { return http.post(`${BASE_URLS.REST_API}/posts`, post); } export function updatePost(id, post) { return http.put(`${BASE_URLS.REST_API}/posts/${id}`, post); } export function deletePost(id) { return http.del(`${BASE_URLS.REST_API}/posts/${id}`); }
In the same way as for the example of api rest, we can improve our script by creating more atomic functions that we can reuse to create more complex scenarios in the future if necessary, it is getting simpler to understand what our test script does.
There is still a better way to optimise and have a better parameterisation of the response and request results, what do you imagine we could do?
Use dynamic data to simulate more realistic scenarios and load different data sets. K6 allows us to use shared arrays to load data from a file. Shared arrays are a way to store data that can be accessed by all VUs.
We can create a users-config.js file to load the users data from a JSON file users.json.
brew install k6 # Or with Docker docker run -i grafana/k6 run - <script.js
import http from "k6/http"; import { check, sleep } from "k6"; // Define the API endpoint and expected response export default function () { const res = http.get("https://jsonplaceholder.typicode.com/posts/1"); // Define the expected response const expectedResponse = { userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto", }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); sleep(1); }
And then we can use it in our test script jsonplaceholder-api-rest.js.
K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run ./src/rest/jsonplaceholder-api-rest.js
A well-organized project structure helps in maintaining and scaling your tests. Here's a suggested folder structure:
http://127.0.0.1:5665/
This structure helps in keeping your project organized, scalable, and easy to maintain, avoiding clutter in the project root.
Another option would be to group test scripts into folders by functionality, you can test and compare what makes the most sense for your context. For example, if your project about a wallet that makes transactions, you could have a folder for each type of transaction (deposit, withdrawal, transfer, etc.) and inside each folder you could have the test scripts for that specific transaction.
import http from "k6/http"; import { check } from "k6"; // Define the query and variables const query = ` query getPokemon($name: String!) { pokemon(name: $name) { id name types } }`; const variables = { name: "pikachu", }; // Define the test function export default function () { const url = "https://graphql-pokemon2.vercel.app/"; const payload = JSON.stringify({ query: query, variables: variables, }); // Define the headers const headers = { "Content-Type": "application/json", }; // Make the request const res = http.post(url, payload, { headers: headers }); // Define the expected response const expectedResponse = { data: { pokemon: { id: "UG9rZW1vbjowMjU=", name: "Pikachu", types: ["Electric"], }, }, }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); }
On this second example, we have a more complex data structure, but we can still reuse the same requests functions that we created for the first example.
Performance testing with K6 is critical for identifying bottlenecks and ensuring application scalability. By following best practices such as modularizing code, centralizing configurations, and using dynamic data, engineers can create maintainable and scalable performance testing scripts.
Big hug.
Charly Automatiza
The above is the detailed content of Grafana Kheat sheet: everything a performance engineer should know. For more information, please follow other related articles on the PHP Chinese website!