Skip to content
Go back

Your First Real-World k6 Test

Edit page

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:

  1. visiting a page.
  2. loading product data.
  3. 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

  1. Create a Working Directory

This directory will store all blog-series scripts:

mkdir k6-course
cd k6-course
  1. If you are using VS Code, I recommend installing k6 for Visual Studio Code plugin.

  2. Create a file real-world-test.js in k6-course directory:

The Flow We’re Going to Simulate

Instead of fake endpoints, this test uses a realistic sequence:

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";

Test Options

export const options = {
  vus: 5,
  duration: "30s",
};

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,
});

Fetch Product List

let products = http.get("https://fakeloadtest.com/api/products/");
check(products, {
  "Get Products": (r) => r.status === 200,
});
const list = products.json();

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,
});

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,
});

Adding Pacing Time

sleep(1);

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::

Practice Exercise

Extend this real-world flow by adding another API call and another check.

  1. After the POST request, add a DELETE request to simulate “remove from cart”.
  2. Save the response body to a variable.
  3. Add a new check verifying:

Run this test again and compare metrics to the previous run.


Edit page
Share this post on:

Next Post
Introduction & Getting Started with k6