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


26 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. You made a few good points there. I did a search on the issue and found the majority of folks will go along with with your blog. Lucilia Antin Ximenez

  3. 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

  4. Fusce convallis metus id felis luctus adipiscing. Maecenas nec odio et ante tincidunt tempus. Nulla porta dolor. Aenean imperdiet. Fusce neque. Winnie Alasdair Schlosser

  5. 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

  6. 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

  7. I was more than happy to uncover this web site. I want to to thank you for ones time due to this wonderful read!! I definitely really liked every little bit of it and i also have you saved as a favorite to look at new things on your blog. Jobi Gary Russian

Leave a Reply

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