Do you want your application to perform well during peak traffic times? Here's what you need to know.

What is Performance and Load Testing?

The two types of testing generally discussed are functional and non-functional. Functional testing is more concerned with the functions and business requirements of a product, whereas non-functional testing is more about testing connections and environments, rollbacks, etc.

Load testing is a type of software testing where we study the behavior of our web pages under the expected load. We specifically determine the behavior of the system during normal and peak loads. We basically test all real-time load conditions so that an application can perform well and can serve as many users as we want without failure.

Basic Procedure of Load Testing

The diagram below will help you understand the basic paradigm for load testing. There is no hard rocket science behind it. It is basically testing and analyzing your results on your own. 

Sometimes during Black Friday or other seasonal days, websites may be down because they are not able to handle the heavy traffic load. But if your team knows what the load was last year and can predict the number of users expected this year, you can plan accordingly and write your load test cases, increasing gradually, to test the behavior of your web pages.

JL 1

k6 Load Testing Tool

There are various open source tools available, but k6 is quite easy to configure and use in you UI codebase. k6 is a development-oriented, open source load testing tool for making load and performance testing scripts and testing web pages.

While you can use k6 without a lot of studying and training, basic knowledge of Node Packaging Manager (NPM) and JavaScript are recommended. If you need help, you can always refer to the k6 official website and write your script accordingly. 

How to Install k6

Below is the dependency required to install before proceeding. Please run these commands:

Mac:

JL 2

Windows:

You can download k6 installer from this link.

How to Use it – Writing Basic Script

After installing k6 dependency, we can go ahead and write our scripts. Create any file script.js in your code base, and let's start writing basic load script. Below is a sample we can use for testing:

JL 3

Line 1 – Import the k6/http library into your script.js.

Line 2 – Import sleep, a function that gives a time difference after every hit.

Line 4 – Start your basic function where you write your load test case.

Line 5 – Include your web page link in the get function, which will be under load test.

How to Run It

Run your k6 script using the command below:

JL 4

After running, you will notice something like this on your terminal. You can go to the official link to learn how to study the output of your k6 script.

JL 5

How to Increase the Load

Now we can increase the load testing by increasing its virtual users and its running duration. The command below will increase VUs to 10 and the duration to 30 sec.

JL 6

Sample Advanced Load Script

JL 7

Above is an advanced load testing script. Let's go through it line by line.

Lines 4 – 10: When you want to increase and decrease the load according to your own requirements, you cannot do it with the k6 run command. In this case, the options function can be used to create basic or advanced stages for your test. Your target shows your number of VUs, which can be varied according to your requirements for any duration of time.

Lines 13- 26: When you want to test a group of web pages together, you can use this function, and batch is used to fire all requests in parallel so that we can test a high load. Checks are used for studying your result outputs.

Conclusion

Today I outlined a basic introduction to performance/load testing using k6.  k6 is completely developer-centric. It can be used with Influx-Db and Grafana to see graphical output. This tool is very easy to use and configure and provides a great way to check performance and load testing!


Author

Joseph Lawrence

Joseph Lawrence (Joe) is a Senior UI Developer at Avenue Code. While he never wanted to spend his life in front of a computer, he finds this new era of technology exciting and thinks coding is very fun, especially when focusing on logic rather than learning syntaxes. Coding, coffee, and guitar are his three hobbies. He also has expertise in React JS and Angular JS.


How to Build Unit Tests Using Jest

READ MORE

How Heuristics Can Improve Your Tests

READ MORE

How to Use WireMock for Integration Testing

READ MORE

The Best Way to Use Request Validation in Laravel REST API

READ MORE