Performance Testing with k6: A Beginner's Guide

A beginner-friendly guide to performance testing your websites and apps using k6, explained in simple terms with real-world analogies.


Hey there! If you're a computer science student or a junior engineer, you might be wondering why performance testing is a big deal. Well, let me tell you - it's super important, and many beginners often skip it. But don't worry, I'm here to help you understand why it matters and how to do it easily.

Why Performance Testing Matters

Imagine you've built a cool website or app. It works great when you and your friends use it. But what happens when hundreds or thousands of people try to use it at the same time? Will it still work smoothly, or will it slow down or crash?

This is where performance testing comes in. It helps you:

  1. Make sure your website or app can handle lots of users
  2. Find out where things might break or slow down
  3. Fix problems before real users experience them
  4. Save time and money by catching issues early

Many CS students and junior engineers focus on making features work but forget to check if they work well under pressure. It's like building a car that looks great but breaks down on a long trip. Not good, right?

Enter k6: Your New Best Friend for Performance Testing

Now, you might think performance testing sounds hard or boring. But what if I told you there's a tool that makes it easy and kind of fun? That's where k6 comes in.

k6 is a modern tool that helps you test how well your website or app performs. It's becoming super popular because it's easy to use, especially if you know a bit of JavaScript. In this post, I'll show you how to use k6 to make sure your projects can handle the spotlight when it's their time to shine.

What Makes k6 Cool?

Here's why k6 is awesome:

  • It uses JavaScript, so if you know some basic coding, you're already halfway there!
  • You can test small websites or big, complex apps
  • You can run tests on your own computer or in the cloud
  • It plays well with other tools developers love

Let's Get Started with k6

To install k6, just head over to this link: https://grafana.com/docs/k6/latest/set-up/install-k6/ . This page has all the instructions you need for any operating system - whether you're using Windows, Mac, or Linux. Just follow the steps for your system, and you'll have k6 up and running in no time!

Now, let's write your first test. It's easier than you might think! Create a new file called my_first_test.js and put this code in it:

import http from "k6/http";
import { check, sleep } from "k6";
 
export default function () {
  let response = http.get("https://test.k6.io");
  check(response, { "status is 200": (r) => r.status === 200 });
  sleep(1);
}

Don't worry if this looks confusing - I'll explain what it does:

  1. We're telling k6 to visit a website (https://test.k6.io)
  2. We're checking if the website responds correctly (that's what "status is 200" means)
  3. We're waiting for 1 second before doing it again (that's what sleep(1) does)

To run this test, save the file and type this in your terminal:

k6 run my_first_test.js

Boom! You've just run your first performance test. How cool is that?

Making Your Tests More Interesting

Now that you've got the basics down, let's make things a bit more exciting. We can tell k6 to pretend to be lots of users visiting your site at once. Here's how:

import http from "k6/http";
import { check, sleep } from "k6";
 
export let options = {
  stages: [
    { duration: "30s", target: 20 },
    { duration: "1m30s", target: 10 },
    { duration: "20s", target: 0 },
  ],
};
 
export default function () {
  let response = http.get("https://test.k6.io");
  check(response, { "status is 200": (r) => r.status === 200 });
  sleep(1);
}

This test does something pretty cool:

  1. It starts with 20 pretend users for 30 seconds
  2. Then it goes down to 10 users for 1 minute and 30 seconds
  3. Finally, it slowly stops over 20 seconds

It's like having a bunch of friends visit your site, then some leave, and finally everyone goes home. This helps you see how your site handles different numbers of visitors.

Different Types of Performance Tests

Now that you're getting the hang of it, let's look at some different types of tests you can do with k6.

1. Load Test: The Everyday Workout

A load test is like checking if your site can handle a normal busy day. Here's how you might do it:

import http from "k6/http";
import { check, sleep } from "k6";
 
export let options = {
  stages: [
    { duration: "5m", target: 100 }, // Slowly add 100 users over 5 minutes
    { duration: "10m", target: 100 }, // Stay at 100 users for 10 minutes
    { duration: "5m", target: 0 }, // Slowly go back to 0 users over 5 minutes
  ],
};
 
export default function () {
  let response = http.get("https://test.k6.io");
  check(response, { "status is 200": (r) => r.status === 200 });
  sleep(1);
}

This test simulates a gradual increase in users, holds steady for a while, then gradually decreases.

2. Stress Test: Pushing the Limits

A stress test is when you really push your site to see when it starts to struggle:

import http from "k6/http";
import { check, sleep } from "k6";
 
export let options = {
  stages: [
    { duration: "2m", target: 100 }, // Start with 100 users
    { duration: "5m", target: 100 }, // Stay at 100 for a bit
    { duration: "2m", target: 200 }, // Increase to 200
    { duration: "5m", target: 200 }, // Stay at 200
    { duration: "2m", target: 300 }, // Increase to 300
    { duration: "5m", target: 300 }, // Stay at 300
    { duration: "2m", target: 400 }, // Increase to 400
    { duration: "5m", target: 400 }, // Stay at 400
    { duration: "10m", target: 0 }, // Everyone goes home
  ],
};
 
export default function () {
  let response = http.get("https://test.k6.io");
  check(response, { "status is 200": (r) => r.status === 200 });
  sleep(1);
}

This test keeps adding more and more users until your site starts to sweat!

3. Spike Test: The Surprise Party

A spike test is like throwing a surprise party for your website. Suddenly, lots of people show up at once:

import http from "k6/http";
import { check, sleep } from "k6";
 
export let options = {
  stages: [
    { duration: "10s", target: 100 }, // Get to 100 users fast
    { duration: "1m", target: 100 }, // Stay at 100 for a minute
    { duration: "10s", target: 1000 }, // Suddenly jump to 1000 users!
    { duration: "3m", target: 1000 }, // Stay at 1000 for 3 minutes
    { duration: "10s", target: 100 }, // Quickly drop back to 100
    { duration: "3m", target: 100 }, // Stay at 100 for 3 minutes
    { duration: "10s", target: 0 }, // Everyone leaves
  ],
};
 
export default function () {
  let response = http.get("https://test.k6.io");
  check(response, { "status is 200": (r) => r.status === 200 });
  sleep(1);
}

This test sees how your site handles a sudden rush of visitors.

4. Soak Test: The Marathon

A soak test is like a marathon for your website. It checks how your site performs over a long time:

import http from "k6/http";
import { check, sleep } from "k6";
 
export let options = {
  stages: [
    { duration: "2m", target: 400 }, // Get to 400 users over 2 minutes
    { duration: "3h", target: 400 }, // Stay at 400 users for 3 hours!
    { duration: "2m", target: 0 }, // Everyone goes home
  ],
};
 
export default function () {
  let response = http.get("https://test.k6.io");
  check(response, { "status is 200": (r) => r.status === 200 });
  sleep(1);
}

This test runs for a long time to see if your site starts to slow down or use up too much memory after running for hours.

Wrapping Up

And there you have it! You've just learned the basics of performance testing with k6. It's not so scary after all, right?

Remember, performance testing is super important. It's like giving your website or app a health check-up. By using k6, you can easily make sure your projects are ready for the big time. So next time you're building something cool, don't forget to give it a workout with k6. Your users (and your future self) will thank you! Happy testing, and may your websites always be fast and never crash! 😊