Different types of Performance testing in k6 | Tech

background
Reading Time: 4 minutes

Hi Readers, In this blog, I will explore how it is possible to run many types of testing in k6. Each type of testing has a different purpose.

K6

Basically, It is a free and open-source load testing tool for testing the performance of APIs. K6 has a goal-oriented testing mode, and users can define goals using Thresholds when building their tests. 

Different Types of Testing

There are many types of testing which come under performance testing and each type of testing serves a different purpose. Different test types define and optimize the performance. There are many performance testings are-

Load Testing in K6

Load testing is testing where we check an application’s performance by applying some load, which is either less than or equal to the desired load.

Example:-

Suppose, You are downloading a series of large files from the internet. In this case, it is called Load Testing.

Write a script in K6 and save it as Load.js

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

export const options = {
  stages: [
    { duration: '5m', target: 100 }, // simulate ramp-up of traffic from 1 to 100 users over 5 minutes.
    { duration: '10m', target: 100 }, // stay at 100 users for 10 minutes
    { duration: '5m', target: 0 }, // ramp-down to 0 users
  ],
  thresholds: {
    'http_req_duration': ['p(99)<1500'], // 99% of requests must complete below 1.5s
    'logged in successfully': ['p(99)<1500'], // 99% of requests must complete below 1.5s
  },
};

const BASE_URL = 'https://test-api.k6.io';
const USERNAME = 'TestUser';
const PASSWORD = 'SuperCroc2020';

export default () => {
  const loginRes = http.post(`${BASE_URL}/auth/token/login/`, {
    username: USERNAME,
    password: PASSWORD,
  });

  check(loginRes, {
    'logged in successfully': (resp) => resp.json('access') !== '',
  });

  const authHeaders = {
    headers: {
      Authorization: `Bearer ${loginRes.json('access')}`,
    },
  };

  const myObjects = http.get(`${BASE_URL}/my/crocodiles/`, authHeaders).json();
  check(myObjects, { 'retrieved crocodiles': (obj) => obj.length > 0 });

  sleep(1);
};

Smoke Testing

Smoke testing is configured with minimal load means it is a normal and regular test load. You want to run a smoke test as a sanity check every time you write a new script or modify an existing script.

Example:-

Verify that your system doesn’t throw any errors when under minimal load.

Write a script in K6 and save it as Smoke.js

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

export const options = {
  vus: 1, // 1 user looping for 1 minute
  duration: '1m',

  thresholds: {
    http_req_duration: ['p(99)<1500'], // 99% of requests must complete below 1.5s
  },
};

const BASE_URL = 'https://test-api.k6.io';
const USERNAME = 'TestUser';
const PASSWORD = 'SuperCroc2020';

export default () => {
  const loginRes = http.post(`${BASE_URL}/auth/token/login/`, {
    username: USERNAME,
    password: PASSWORD,
  });

  check(loginRes, {
    'logged in successfully': (resp) => resp.json('access') !== '',
  });

  const authHeaders = {
    headers: {
      Authorization: `Bearer ${loginRes.json('access')}`,
    },
  };

  const myObjects = http.get(`${BASE_URL}/my/crocodiles/`, authHeaders).json();
  check(myObjects, { 'retrieved crocodiles': (obj) => obj.length > 0 });

  sleep(1);
};

Stress Testing

The purpose of stress testing is to verify the stability and reliability of the system under extreme conditions. Stress testing involves running simulations to identify hidden vulnerabilities. Stress testing is testing how an application, software, or website performs when under extreme pressure—an unexpected load. 

Example:-

Stress testing is very much a requirement as on the day of some result, a huge number of users/applicants/students will login to the application to check their results.

Write a script in k6 and save it as Stress.js

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

export const options = {
  stages: [
    { duration: '2m', target: 100 }, // below normal load
    { duration: '5m', target: 100 },
    { duration: '2m', target: 200 }, // normal load
    { duration: '5m', target: 200 },
    { duration: '2m', target: 300 }, // around the breaking point
    { duration: '5m', target: 300 },
    { duration: '2m', target: 400 }, // beyond the breaking point
    { duration: '5m', target: 400 },
    { duration: '10m', target: 0 }, // scale down. Recovery stage.
  ],
};

export default function () {
  const BASE_URL = 'https://test-api.k6.io'; // make sure this is not production

  const responses = http.batch([
    ['GET', `${BASE_URL}/public/crocodiles/1/`, null, { tags: { name: 'PublicCrocs' } }],
    ['GET', `${BASE_URL}/public/crocodiles/2/`, null, { tags: { name: 'PublicCrocs' } }],
    ['GET', `${BASE_URL}/public/crocodiles/3/`, null, { tags: { name: 'PublicCrocs' } }],
    ['GET', `${BASE_URL}/public/crocodiles/4/`, null, { tags: { name: 'PublicCrocs' } }],
  ]);

  sleep(1);
}

Soak Testing

Basically, Soak testing is known as endurance and capacity testing. Soak Testing is a type of software testing in which a system is tested under a huge load over a continuous availability period to check the behavior of the system under production use.

Example:-

Soak testing is ideal for websites or applications facing huge transaction rates, let’s say 1000 transactions in an hour. 

Write a script in k6 and save it as Soak.js

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

export const options = {
  stages: [
    { duration: '2m', target: 400 }, // ramp up to 400 users
    { duration: '3h56m', target: 400 }, // stay at 400 for ~4 hours
    { duration: '2m', target: 0 }, // scale down. (optional)
  ],
};

const API_BASE_URL = 'https://test-api.k6.io';

export default function () {
  http.batch([
    ['GET', `${API_BASE_URL}/public/crocodiles/1/`],
    ['GET', `${API_BASE_URL}/public/crocodiles/2/`],
    ['GET', `${API_BASE_URL}/public/crocodiles/3/`],
    ['GET', `${API_BASE_URL}/public/crocodiles/4/`],
  ]);

  sleep(1);
}
I hope you enjoyed it and it helped you!! stay connected for more future blogs. 

Thank you!!

References:

https://k6.io/docs/results-visualization/cloud/

Written by 

Soniya is a QA consultant having experience of more than 3 years. She has experience with Performance testing, API testing and Automation testing(Selenium, Katalon, Cypress and k6). She is always eager to learn new and advanced concepts.

Leave a Reply