DEV Community

Cover image for How to load test your api ?
moayad khader
moayad khader

Posted on

How to load test your api ?

Hey there, tech enthusiast! So, you've built this awesome API that's going to revolutionize the way data flows in your application. But, before you unleash it into the wild, there's one crucial step you can't afford to skip - load testing.
Now, I know what you might be thinking. "Load testing? Sounds like something straight out of a sci-fi movie." Well, worry not! In this blog, we're going to break it down in simple, everyday terms. No jargon, no complex algorithms - just good ol' fashioned explanation.

Picture this: you've got a car engine, all shiny and new, ready to hit the road. But how do you know it'll perform when you've got a whole bunch of passengers on board, each wanting to get somewhere fast? That's where load testing comes in!

In essence, load testing is like giving your API a trial run with a simulated crowd. It's like seeing if your car can handle a full load of passengers without sputtering or stalling.

So, join me on this journey where we'll learn the ins and outs of load testing, step by step. By the end, you'll be armed with the knowledge to make sure your API can handle the digital equivalent of rush hour traffic.

Let's dive in!

So, In this article I want to use the awesome k6 library by Grafana Labs.

k6 is fully open source, to download it please follow this link:

Download K6

Choose the downloading method that suites you and your operating system.

Now after you have downloaded k6 successfully let's talk about load testing types, we have 3 main test which are:

  • Stress Test

  • Spike Test

  • Soak Test

Stress Test

Well, it's pretty much what it sounds like. We're going to crank up the pressure on your API to see how much it can handle before it starts sweating.

Stress tests assess how a system performs at its limits when load exceeds the expected average.

Code Breakdown

import http from 'k6/http';
import { sleep, check } from 'k6';

export let options = {
  InsecureSkipTLSVerify: true,
  noConnectionReuse: false,
  stages:[
    { duration: '2m', target:100 },
    { duration: '5m', target:100 },
    { duration: '2m', target:200 },
    { duration: '5m', target:200 },
    { duration: '2m', target:300 },
    { duration: '5m', target:300 },
    { duration: '2m', target:400 },
    { duration: '5m', target:400 },
    { duration: '10m', target:0 },
  ]
};

export default function () {
  const response = http.get('https://test.k6.io');
  check(response,{
    '200': (r) => r.status === 200
  })
  sleep(1)
}

Enter fullscreen mode Exit fullscreen mode

To run this test:

k6 run stress.test.js
Enter fullscreen mode Exit fullscreen mode

Spike Test

The spike test. Imagine it like a sudden surge of energy in a music concert. Things are cruising along, then suddenly, BOOM! It's party time!

Spike tests validate the behavior and survival of your system in cases of sudden, short, and massive increases in activity.

Code Breakdown

import http from 'k6/http';
import { sleep, check } from 'k6';

export let options = {
  InsecureSkipTLSVerify: true,
  noConnectionReuse: false,
  stages:[
    { duration: '10s', target:100 },
    { duration: '1m', target:100 },
    { duration: '10s', target:1400 },
    { duration: '3m', target:1400 },
    { duration: '10s', target:100 },
    { duration: '3m', target:100 },
    { duration: '10s', target:0 },
  ]
};

export default function () {
  const response = http.get('https://test.k6.io');
  check(response,{
    '200': (r) => r.status === 200
  })
  sleep(1)
}
Enter fullscreen mode Exit fullscreen mode

To run this test:

k6 run spike.test.js
Enter fullscreen mode Exit fullscreen mode

Soak Test

The most boring test - a bit like taking your API for a leisurely stroll in the park.

Soak tests assess the reliability and performance of your system over extended periods.

Code Breakdown

import http from 'k6/http';
import { sleep, check } from 'k6';

export let options = {
  InsecureSkipTLSVerify: true,
  noConnectionReuse: false,
  stages:[
    { duration: '2m', target:400 },
    { duration: '5h30m', target:400 },
    { duration: '2m', target:0 }
  ]
};

export default function () {
  const response = http.get('https://test.k6.io');
  check(response,{
    '200': (r) => r.status === 200
  })
  sleep(1)
}
Enter fullscreen mode Exit fullscreen mode

To run this test:

k6 run soak.test.js
Enter fullscreen mode Exit fullscreen mode

Top comments (0)