NODEJS SERIES

Load Testing NodeJS APIs with Autocannon

A guide to how we can load test NodeJS APIs using Autocannon

Chikku George
Globant
Published in
4 min readOct 11, 2022

--

As a developer, we always have a concern regarding the API’s response time such that it can’t be a bottleneck for the consumers. So we need to ensure that the response time will not affect the application’s performance even if multiple users are using it concurrently.

Tests are always an integral part of an application to ensure its smooth functioning. How can we test our APIs that will support concurrent user requests? — Autocannon, a load testing package, can simulate high traffic in our application.

Autocannon

Autocannon is an HTTP/1.1 benchmarking tool written in NodeJS, widely used to measure the performance of an application. With autocannon, we can simulate multiple requests per second to load-test our application.

Installation

Install autocannon globally in your project.

npm install -g autocannon

Testing Command

autocannon [opts] URL

Available Options

More options you can find here.

-c | --connections
Number of concurrent connections to use
By default, its value is 10
-p | --pipeline
Number of pipelined requests to use
By default, its value is 1
-d | --duration
Number of seconds to run the autocannon
By default, its value is 10
-w | --workers
Number of worker threads to fire requests
-m | --method
HTTP method to use
By default, its value is 'GET'
-t | --timeout
Number of seconds before timing out and resetting a connection
By default, its value is 10
-j | --json
Print the output as newline delimited JSON
By default, its value is false
-f | --forever
Run the benchmark forever

When you run the command, if you come across any error like running scripts disabled on the system, set the execution policy as follows:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

Let’s play with some code examples.

I set up a sample project where my database contains only one collection called hobbies.

Hobbies Model
Get all Hobbies & Save new Hobbies

Autocannon command for my GET Request

autocannon -c 500 -d 5 http://localhost:8000/getAll // Running 500 connections in 5 seconds

Test Result

Credit: Author

Autocannon command for my POST Request

autocannon -m POST -H “Content-Type:application/x-www-form-urlencoded” -b “name=swimming&active=true” http://localhost:8000/save  // specify Header(-H) and Body (-b)

Test Result

Credit: Author

Autocannon outputs two tables of data:

  1. Request Latency — It shows the request time at 2.5%(fast), 50%(median), 97.5%(slow), and 99%(slowest) along with an average time.
  2. Request Volume — It shows the number of requests sent and number of bytes downloaded per second.

Instead of running test commands from the command line, we can also run them programmatically.

autocannon(opts[, cb])

opts is the configuration options for the autocannon instance and cb, the callback which is called on completion of the test.

Autocannon GET & POST requests programmatically

Test Result

Credit: Author

If you closely watch the above test result, many things got displayed as below:

start
Date object when test started
finish
Date object when test ended
errors
Number of connection errors
timeouts
Number of connection timeouts
mismatches
Number of requests with a mismatched body
non2xx
Number of non-2xx response status codes received
requests
Number of requests per second
latency
Request time
throughput
Response data throughput per second

request, latency and throughput are percentile-objects with the following values:

min
The lowest value.
max
The highest value.
average
The average (mean) value.
stddev
The standard deviation.
p*
The percentile value. The percentile properties are: p2_5, p50, p75, p90, p97_5, p99, p99_9, p99_99, p99_999.

Autocannon Events

An autocannon instance is an Event Emitter. So it emits several events. Some are given below. More can find here.

Credit: Author
start
Emitted when autocannon instance is setup and started.
done
Emitted when the autocannon finishes a benchmark.
track
Track the progress of autocannon.
Configuration options for tracking are:
1. outputStream
The stream to output to.
2. renderProgressBar
A boolean value indicating whether to display progress bar or not. By default, it’s value is true.
3. renderResultsTable
A boolean value indicating whether to display result table or not. By default, it’s value is true.
4. renderLatencyTable
A boolean value indicating whether to display latency table. By default, it’s value is false.
5. progressBarString
A string defining the format of the progress display output.

Have fun load testing your APIs with Autocannon. Thank You:)

--

--

Chikku George
Globant

Software Engineer | ReactJS | NodeJS | Blockchain Enthusiast