how to run load testing via grafana k6

Introduction

When we developed a project then we need against the project to run load testing to measurement application loading for later application monitoring. We can use like wrk, jmeter, etc.
but I found an open source k6 from grafana lab.

Installation

Install k6 cli.
k6 installation

Test Types

Introduction

It is possible to perform many types of tests using k6, each type serving a different purpose.

load testing

Each test type is designed to give you different insights about your system.

  • Smoke Test: role is to verify that your system can handle minimal load, without any problems.
  • Load Test: It’s primarily concerned with assessing the performance of your system in terms of concurrent users or requests per second.: It’s primarily concerned with assessing the performance of your system in terms of concurrent users or requests per second.
  • Stress Test and Spike testing: are concerned with assessing the limits of your system and stability under extreme conditions.
  • Soak Test: tells you about reliability and performance of your system over an extended period of time.

The important thing to understand is that each test can be performed with the same test script. You can write one script and perform all the above tests with it. The only thing that changes is the test configuration, the logic stays the same.

Different test types will teach you different things about your system and give you the insight needed to understand or optimize performance.

First write smoke testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
};
1
k6 run scripts/smoke.js

smoke testing result