Now that we have complete setup of K6 ready following previous Introduction & Getting Started with k6 post, let’s create a practical k6 script that simulates an actual flow.
We will simulate:
- visiting a page.
- loading product data.
- performing an action.
This scenario mirrors the real world traffic and we can expand later by adding more test features.
Table of contents
Open Table of contents
Project Setup
- Create a Working Directory
This directory will store all blog-series scripts:
mkdir k6-course
cd k6-course
-
If you are using VS Code, I recommend installing k6 for Visual Studio Code plugin.
-
Create a file real-world-test.js in
k6-coursedirectory:
The Flow We’re Going to Simulate
Instead of fake endpoints, this test uses a realistic sequence:
- Load the homepage
- Fetch a product list
- View a single product
- Perform a POST action (e.g., add to cart)
This pattern resembles real SaaS interaction as uur script will hit multiple endpoints, validates each step and mirror actual user behavior patterns.
Code Walkthrough
Imports
import http from "k6/http";
import { sleep, check } from "k6";
httpprovides functions to send HTTP requests (get,post,put,del, etc.).- check evaluates test conditions on responses (status codes, response body) and records pass/fail metrics.
sleeppauses a VU for a number of seconds to simulate real world user behaviour.
Test Options
export const options = {
vus: 5,
duration: "30s",
};
- vus: virtual users → “how many simultaneous users to simulate”
- duration: total runtime → avoids running indefinitely
This runs 5 users for 30 seconds, a small load but enough to reveal basic performance patterns.
Default function — the VU behavior
export default function () { ... } is the function every virtual user executes repeatedly for the test duration.
Homepage Request
let home = http.get("https://fakeloadtest.com");
check(home, {
"homepage loaded": (r) => r.status === 200,
});
- Sends a
GETrequest r.statusreturns the status code- We can use
(r) => r.status === 200check to ensure the endpoint responds correctly with 200 OK status code
Fetch Product List
let products = http.get("https://fakeloadtest.com/api/products/");
check(products, {
"Get Products": (r) => r.status === 200,
});
const list = products.json();
- Fetches a list of items (in this case, public products)
- check to ensure the endpoint responds correctly with 200 OK status code
const list = products.json()Converts the response body into a JavaScript array
View Product Details
const productId = list[0].id;
let product = http.get(
`https://fakeloadtest.com/api/products/${productId}/`
);
check(product, {
"Get Product Detail": (r) => r.status === 200,
});
list[0].idPicks one product (the first one)- Fetches its detail page
- check to ensure the endpoint responds correctly with 200 OK status code
Perform a POST Action
let action = http.post(`https://fakeloadtest.com/api/products/add-to-cart`, {
product_id: productId,
});
check(action, {
"Product Added To Cart": (r) => r.status === 200,
});
- Simulates an action like “add to cart”, “save item”, etc.
- Uses httpbin.org as a safe echo endpoint
- Confirms server accepts POST requests
Adding Pacing Time
sleep(1);
- Pacing time helps mimic real world user pacing
Putting it all together
import http from "k6/http";
import { sleep, check } from "k6";
export const options = {
vus: 5,
duration: "30s",
};
export default function () {
// Step 1: Homepage
let home = http.get("https://fakeloadtest.com");
check(home, {
"homepage loaded": (r) => r.status === 200,
});
// Step 2: Product List
let products = http.get("https://fakeloadtest.com/api/products/");
check(products, {
"Get Products": (r) => r.status === 200,
});
const list = products.json();
if (!list.length) return;
// Step 3: Pick a product and view details
const productId = list[0].id;
let product = http.get(
`https://fakeloadtest.com/api/products/${productId}/`
);
check(product, {
"Get Product Detail": (r) => r.status === 200,
});
// Step 4: Add product to cart
let action = http.post(`https://fakeloadtest.com/api/products/add-to-cart`, {
product_id: productId,
});
check(action, {
"Product Added To Cart": (r) => r.status === 200,
});
sleep(1);
}
k6-course/real-world-test.js
Now Run:
k6 run real-world-test.js
You should see success/failure for each check, plus useful metrics like::
- iteration duration
- request latency
- HTTP timings
- throughput
Practice Exercise
Extend this real-world flow by adding another API call and another check.
- After the POST request, add a
DELETErequest to simulate “remove from cart”. - Save the response body to a variable.
- Add a new check verifying:
- status is not 400
- response contains “json” field
Run this test again and compare metrics to the previous run.