Stress testing in nodejs with k6, influxdb and grafana

So, I deployed my application code written in nodejs on an ec2 instance and wanted to see how well it was going to hold against a surge of users. I started searching for tools that would help me with it. Its a common practise in software development to test your code against load before deploying it into production. I finally stumbled on K6, its a powerful opensource load testing tool. The scripts can be purely written in es6. There are other gui based load testing tools like jmeter, but I personally love K6. One note – K6 doesnot come with gui out of the box, you can opt for cloud based solutions, or just follow this tutorial to get K6 working with grafana to build custom dashboards. We will be using K6 for load testing, grafana to create custom dashboards and influxdb to stich these together. The way it works is – k6 performs the load tests and stores the logs into influxdb, grafana can then read these logs from influxdb and we can then create the dashboards. Its actually easier than it sounds. Just stick with me a little longer. Did I tell you that we will be doing all of this in docker ? Here’s the drill –

  1. Open the website you want to perform stress test on, or open your app on localhost
  2. Open chrome developer tools -> Network
  3. Browse app the endpoints you want to perform stress test on
  4. In the networks panel, right click on any endpoint, In the dropdown you will see an option called save as HAR (Http Archive file)

Now its time for setting up our docker environment 1. mkdir stress-test 2. cd stress-test 3. Download grafana-influxdb docker compose file from here or run this command in your terminal
curl > docker-compose.yml
4. docker-compose up -d --build 5. install k6 on your system with  sudo apt-get install k6, you can also do it using docker but its a small 6 mb package so you might as well install it in your system. 6. Now we will convert the har file into es6 script using
k6 convert har-file-you-downloaded.har > fosstech-testscript.js
7. Edit options section with these lines export let options = { maxRedirects: 0,   stages: [      { target: 50, duration: “30s” },      { target: 50, duration: “1m” },      { target: 0, duration: “30s” },   ], }; 8. Finally run k6 k6 run --out influxdb=http://localhost:8086/myk6db fosstech-testscript.js
9. K6 will automatically created a datasource for you with the name myk6db
10. So the next step is to connect grafana with this datasource,
for your connection – dbname: myk6db, user: admin, password: influxdb, username and password was provided in docker-compose.yml. Below is the recording for my sample webserver load test

Grafana dashboard


23 thoughts on “Stress testing in nodejs with k6, influxdb and grafana

  1. If you want to use the photo it would also be good to check with the artist beforehand in case it is subject to copyright. Best wishes. Aaren Reggis Sela

  2. Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also make comment due to this sensible article. Juline Murdock Hennahane

  3. You made some decent factors there. I searched the net for the concern and discovered most people will certainly accompany with your website. Kirsteni Alejoa Resee

  4. I have read so many articles or reviews about the blogger lovers except this article is actually a pleasant piece of writing, keep it up. Albertina Travis Mikel

Leave a Reply

Your email address will not be published. Required fields are marked *