AutoCannon

fast HTTP/1.1 benchmarking tool written in Node.js

README

banner

autocannon


Node.js CI

demo

An HTTP/1.1 benchmarking tool written in node, greatly inspired by [wrk][wrk]
and [wrk2][wrk2], with support for HTTP pipelining and HTTPS.
On _my_ box, autocannon can produce more load than wrk and wrk2, see limitations for more details.


Install


  1. ```
  2. npm i autocannon -g
  3. ```

or if you want to use the API or as a dependency:

  1. ```
  2. npm i autocannon --save
  3. ```

Usage


Command Line


  1. ```
  2. Usage: autocannon [opts] URL

  3. URL is any valid HTTP or HTTPS URL.
  4. If the PORT environment variable is set, the URL can be a path. In that case 'http://localhost:$PORT/path' will be used as the URL.

  5. Available options:

  6.   -c/--connections NUM
  7.         The number of concurrent connections to use. default: 10.
  8.   -p/--pipelining NUM
  9.         The number of pipelined requests to use. default: 1.
  10.   -d/--duration SEC
  11.         The number of seconds to run the autocannon. default: 10.
  12.   -a/--amount NUM
  13.         The number of requests to make before exiting the benchmark. If set, duration is ignored.
  14.   -S/--socketPath
  15.         A path to a Unix Domain Socket or a Windows Named Pipe. A URL is still required to send the correct Host header and path.
  16.   -w/--workers
  17.         Number of worker threads to use to fire requests.
  18.   -W/--warmup
  19.        Use a warm up interval before starting sampling.
  20.        This enables startup processes to finish and traffic to normalize before sampling begins
  21.        use -c and -d sub args e.g. `--warmup [ -c 1 -d 3 ]`
  22.   --on-port
  23.         Start the command listed after -- on the command line. When it starts listening on a port,
  24.         start sending requests to that port. A URL is still required to send requests to
  25.         the correct path. The hostname can be omitted, `localhost` will be used by default.
  26.   -m/--method METHOD
  27.         The HTTP method to use. default: 'GET'.
  28.   -t/--timeout NUM
  29.         The number of seconds before timing out and resetting a connection. default: 10
  30.   -T/--title TITLE
  31.         The title to place in the results for identification.
  32.   -b/--body BODY
  33.         The body of the request.
  34.         NOTE: This option needs to be used with the '-H/--headers' option in some frameworks
  35.   -F/--form FORM
  36.         Upload a form (multipart/form-data). The form options can be a JSON string like
  37.         '{ "field 1": { "type": "text", "value": "a text value"}, "field 2": { "type": "file", "path": "path to the file" } }'
  38.         or a path to a JSON file containing the form options.
  39.         When uploading a file the default filename value can be overridden by using the corresponding option:
  40.         '{ "field name": { "type": "file", "path": "path to the file", "options": { "filename": "myfilename" } } }'
  41.         Passing the filepath to the form can be done by using the corresponding option:
  42.         '{ "field name": { "type": "file", "path": "path to the file", "options": { "filepath": "/some/path/myfilename" } } }'
  43.   -i/--input FILE
  44.         The body of the request. See '-b/body' for more details.
  45.   -H/--headers K=V
  46.         The request headers.
  47.   --har FILE
  48.         When provided, Autocannon will use requests from the HAR file.
  49.         CAUTION: you have to specify one or more domains using URL option: only the HAR requests to the same domains will be considered.
  50.         NOTE: you can still add extra headers with -H/--headers but -m/--method, -F/--form, -i/--input -b/--body will be ignored.
  51.   -B/--bailout NUM
  52.         The number of failures before initiating a bailout.
  53.   -M/--maxConnectionRequests NUM
  54.         The max number of requests to make per connection to the server.
  55.   -O/--maxOverallRequests NUM
  56.         The max number of requests to make overall to the server.
  57.   -r/--connectionRate NUM
  58.         The max number of requests to make per second from an individual connection.
  59.   -R/--overallRate NUM
  60.         The max number of requests to make per second from all connections.
  61.         connection rate will take precedence if both are set.
  62.         NOTE: if using rate limiting and a very large rate is entered which cannot be met,
  63.               Autocannon will do as many requests as possible per second. Also, latency data will be corrected to compensate for the effects of the coordinated omission issue. If you are not familiar with the coordinated omission issue, you should probably read [this article](http://highscalability.com/blog/2015/10/5/your-load-generator-is-probably-lying-to-you-take-the-red-pi.html) or watch this [Gil Tene's talk](https://www.youtube.com/watch?v=lJ8ydIuPFeU) on the topic.
  64.   -C/--ignoreCoordinatedOmission
  65.         Ignore the coordinated omission issue when requests should be sent at a fixed rate using 'connectionRate' or 'overallRate'.
  66.         NOTE: it is not recommended to enable this option.
  67.               When the request rate cannot be met because the server is too slow, many request latencies might be missing and Autocannon might report a misleading latency distribution.
  68.   -D/--reconnectRate NUM
  69.         The number of requests to make before resetting a connections connection to the
  70.         server.
  71.   -n/--no-progress
  72.         Don't render the progress bar. default: false.
  73.   -l/--latency
  74.         Print all the latency data. default: false.
  75.   -I/--idReplacement
  76.         Enable replacement of [<id>] with a randomly generated ID within the request body. default: false.
  77.   -j/--json
  78.         Print the output as newline delimited JSON. This will cause the progress bar and results not to be rendered. default: false.
  79.   -f/--forever
  80.         Run the benchmark forever. Efficiently restarts the benchmark on completion. default: false.
  81.   -s/--servername
  82.         Server name for the SNI (Server Name Indication) TLS extension. Defaults to the hostname of the URL when it is not an IP address.
  83.   -x/--excludeErrorStats
  84.         Exclude error statistics (non-2xx HTTP responses) from the final latency and bytes per second averages. default: false.
  85.   -E/--expectBody EXPECTED
  86.         Ensure the body matches this value. If enabled, mismatches count towards bailout.
  87.         Enabling this option will slow down the load testing.
  88.   --renderStatusCodes
  89.         Print status codes and their respective statistics.
  90.   --cert
  91.         Path to cert chain in pem format
  92.   --key
  93.         Path to private key for specified cert in pem format
  94.   --ca
  95.         Path to trusted ca certificates for the test. This argument accepts both a single file as well as a list of files
  96.   --debug
  97.         Print connection errors to stderr.
  98.   -v/--version
  99.         Print the version number.
  100.   -h/--help
  101.         Print this menu.
  102. ```

autocannon outputs data in tables like this:

  1. ```
  2. Running 10s test @ http://localhost:3000
  3. 10 connections

  4. Stat     2.5% 50%   97.5% 99%   Avg     Stdev   Max      
  5. Latency 0 ms 0 ms 0 ms   1 ms 0.02 ms 0.16 ms 16.45 ms
  6. Stat       1%       2.5%     50%     97.5%   Avg     Stdev   Min    
  7. Req/Sec   20623   20623   25583   26271   25131.2 1540.94 20615  
  8. Bytes/Sec 2.29 MB 2.29 MB 2.84 MB 2.92 MB 2.79 MB 171 kB   2.29 MB

  9. Req/Bytes counts sampled once per second.

  10. 251k requests in 10.05s, 27.9 MB read
  11. ```

There are two tables: one for the request latency, and one for the request volume.

The latency table lists the request times at the 2.5% percentile, the fast outliers; at 50%, the median; at 97.5%, the slow outliers; at 99%, the very slowest outliers. Here, lower means faster.

The request volume table lists the number of requests sent and the number of bytes downloaded. These values are sampled once per second. Higher values mean more requests were processed. In the above example, 2.29 MB was downloaded in 1 second in the worst case (slowest 1%). Since we only ran for 10 seconds, there are just 10 samples, the Min value and the 1% and 2.5% percentiles are all the same sample. With longer durations these numbers will differ more.

When passing the -l flag, a third table lists all the latency percentiles recorded by autocannon:

  1. ```
  2. Percentile Latency (ms)
  3. 0.001       0            
  4. 0.01       0            
  5. 0.1         0            
  6. 1           0            
  7. 2.5         0            
  8. 10         0            
  9. 25         0            
  10. 50         0            
  11. 75         0            
  12. 90         0            
  13. 97.5       0            
  14. 99         1            
  15. 99.9       1            
  16. 99.99       3            
  17. 99.999     15          
  18. ```

This can give some more insight if a lot (millions) of requests were sent.

Programmatically


  1. ``` js
  2. 'use strict'

  3. const autocannon = require('autocannon')

  4. autocannon({
  5.   url: 'http://localhost:3000',
  6.   connections: 10, //default
  7.   pipelining: 1, // default
  8.   duration: 10 // default
  9. }, console.log)

  10. // async/await
  11. async function foo () {
  12.   const result = await autocannon({
  13.     url: 'http://localhost:3000',
  14.     connections: 10, //default
  15.     pipelining: 1, // default
  16.     duration: 10 // default
  17.   })
  18.   console.log(result)
  19. }

  20. ```

Workers


  1. ``` js
  2. 'use strict'

  3. const autocannon = require('autocannon')

  4. autocannon({
  5.   url: 'http://localhost:3000',
  6.   connections: 10, //default
  7.   pipelining: 1, // default
  8.   duration: 10, // default
  9.   workers: 4
  10. }, console.log)
  11. ```

NOTE: When in workers mode, you need to pass in an absolute file path to all the options that accept a function. This is because a function passed into the main process can not be cloned and passed to the worker. So instead, it needs a file that it can require. The options with this behaviour are shown in the below example

  1. ``` js
  2. 'use strict'

  3. const autocannon = require('autocannon')

  4. autocannon({
  5.   // ...
  6.   workers: 4,
  7.   setupClient: '/full/path/to/setup-client.js',
  8.   verifyBody: '/full/path/to/verify-body.js'
  9.   requests: [
  10.     {
  11.       // ...
  12.       onResponse: '/full/path/to/on-response.js'
  13.     },
  14.     {
  15.       // ...
  16.       setupRequest: '/full/path/to/setup-request.js'
  17.     }
  18.   ]
  19. }, console.log)
  20. ```

API


autocannon(opts[, cb])


Start autocannon against the given target.

opts: Configuration options for the autocannon instance. This can have the following attributes. _REQUIRED_.
    url: The given target. Can be HTTP or HTTPS. More than one URL is allowed, but it is recommended that the number of connections is an integer multiple of the URL. _REQUIRED_.
    socketPath: A path to a Unix Domain Socket or a Windows Named Pipe. A url is still required to send the correct Host header and path. _OPTIONAL_.
    workers: Number of worker threads to use to fire requests.
    connections: The number of concurrent connections. _OPTIONAL_ default: 10.
    duration: The number of seconds to run the autocannon. Can be a timestring. _OPTIONAL_ default:10.
    amount: A Number stating the number of requests to make before ending the test. This overrides duration and takes precedence, so the test won't end until the number of requests needed to be completed is completed. _OPTIONAL_.
    timeout: The number of seconds to wait for a response before. _OPTIONAL_ default: 10.
    pipelining: The number of pipelined requests for each connection. Will cause theClient API to throw when greater than 1. _OPTIONAL_ default: 1.
    bailout: The threshold of the number of errors when making the requests to the server before this instance bail's out. This instance will take all existing results so far and aggregate them into the results. If none passed here, the instance will ignore errors and never bail out. _OPTIONAL_ default: undefined.
    method: The HTTP method to use. _OPTIONAL_ default: 'GET'.
    title: A String to be added to the results for identification. _OPTIONAL_ default: undefined.
* `body`: A `String` or a `Buffer` containing the body of the request. Insert one or more randomly generated IDs into the body by including `[]` where the randomly generated ID should be inserted (Must also set idReplacement to true). This can be useful in soak testing POST endpoints where one or more fields must be unique. Leave undefined for an empty body. _OPTIONAL_ default: `undefined`.
    form: A String or an Object containing the multipart/form-data options or a path to the JSON file containing them
    headers: An Object containing the headers of the request. _OPTIONAL_ default: {}.
    initialContext: An object that you'd like to initialize your context with. Check out an example of initializing context. _OPTIONAL_
    setupClient: A Function which will be passed the Client object for each connection to be made. This can be used to customise each individual connection headers and body using the API shown below. The changes you make to the client in this function will take precedence over the default body and headers you pass in here. There is an example of this in the samples folder. _OPTIONAL_ default: function noop () {}. When using workers, you need to supply a file path that default exports a function instead (Check out the workers section for more details).
    verifyBody: A Function which will be passed the response body for each completed request. Each request, whose verifyBody function does not return a truthy value, is counted in mismatches. This function will take precedence over the expectBody. There is an example of this in the samples folder. When using workers, you need to supply a file path that default exports a function (Check out the workers section for more details).
    maxConnectionRequests: A Number stating the max requests to make per connection. amount takes precedence if both are set. _OPTIONAL_
    maxOverallRequests: A Number stating the max requests to make overall. Can't be less than connections. maxConnectionRequests takes precedence if both are set. _OPTIONAL_
    connectionRate: A Number stating the rate of requests to make per second from each individual connection. No rate limiting by default. _OPTIONAL_
    overallRate: A Number stating the rate of requests to make per second from all connections. connectionRate takes precedence if both are set. No rate limiting by default. _OPTIONAL_
    ignoreCoordinatedOmission: A Boolean which disables the correction of latencies to compensate for the coordinated omission issue. Does not make sense when no rate of requests has been specified (connectionRate or overallRate). _OPTIONAL_ default: false.
    reconnectRate: A Number that makes the individual connections disconnect and reconnect to the server whenever it has sent that number of requests. _OPTIONAL_
    requests: An Array of Objects which represents the sequence of requests to make while benchmarking. Can be used in conjunction with the body, headers and method params above. Check the samples folder for an example of how this might be used. _OPTIONAL_. Contained objects can have these attributes:
       body: When present, will override opts.body. _OPTIONAL_
       headers: When present, will override opts.headers. _OPTIONAL_
       method: When present, will override opts.method. _OPTIONAL_
       path: When present, will override opts.path. _OPTIONAL_
       setupRequest: A Function you may provide to mutate the raw request object, e.g. request.method = 'GET'. It takes request (Object) and context (Object) parameters, and must return the modified request. When it returns a falsey value, autocannon will restart from first request. When using workers, you need to supply a file path that default exports a function instead (Check out workers section for more details) _OPTIONAL_
       onResponse: A Function you may provide to process the received response. It takes status (Number), body (String) context (Object) parameters and headers (Key-Value Object). When using workers, you need to supply a file path that default exports a function instead (Check out workers section for more details) _OPTIONAL_
    har: an Object of parsed HAR content. Autocannon will extra and useentries.request: requests, method, form and body options will be ignored. _NOTE_: you must ensure that entries are targeting the same domain as url option. _OPTIONAL_
* `idReplacement`: A `Boolean` which enables the replacement of `[]` tags within the request body with a randomly generated ID, allowing for unique fields to be sent with requests. Check out [an example of programmatic usage](./samples/using-id-replacement.js) that can be found in the samples. _OPTIONAL_ default: `false`
    forever: A Boolean which allows you to setup an instance of autocannon that restarts indefinitely after emitting results with the done event. Useful for efficiently restarting your instance. To stop running forever, you must cause a SIGINT or call the .stop() function on your instance. _OPTIONAL_ default: false
    servername: A String identifying the server name for the SNI (Server Name Indication) TLS extension. _OPTIONAL_ default: Defaults to the hostname of the URL when it is not an IP address.
    excludeErrorStats: A Boolean which allows you to disable tracking non-2xx code responses in latency and bytes per second calculations. _OPTIONAL_ default: false.
    expectBody: A String representing the expected response body. Each request whose response body is not equal to expectBodyis counted in mismatches. If enabled, mismatches count towards bailout. _OPTIONAL_
    tlsOptions: An Object that is passed into tls.connect call (Full list of options). Note: this only applies if your URL is secure.
cb: The callback which is called on completion of a benchmark. Takes the following params. _OPTIONAL_.
    err: If there was an error encountered with the run.
    results: The results of the run.

Returns an instance/event emitter for tracking progress, etc. If cb is omitted, the return value can also be used as a Promise.

Customizing sent requests


When running, autocannon will create as many Client objects as desired connections. They will run in parallel until the benchmark is over (duration or total number of requests).
Each client will loop over the requests array, would it contain one or several requests.

While going through available requests, the client will maintain a context: an object you can use in onResponse and setupRequest functions, to store and read some contextual data.
Please check the request-context.js file in samples.

Note that context object will be reset to initialContext (or {} it is not provided) when restarting to the first available request, ensuring similar runs.

Combining connections, overallRate and amount


When combining a fixed amount of requests with concurrent connections and an overallRate limit, autocannon will distribute the requests and the intended rate over all connections. If the overallRate is not integer divisible, autocannon will configure some connection clients with a higher and some with a lower number of requests/second rate. If now the amount is integer divisible, all connection clients get the same number of requests. This means that the clients with a higher request rate will finish earlier, than the others, leading to a drop in the perceived request rate.

Example: connections = 10, overallRate = 17, amount = 5000


autocannon.track(instance[, opts])


Track the progress of your autocannon, programmatically.

instance: The instance of autocannon. _REQUIRED_.
opts: Configuration options for tracking. This can have the following attributes. _OPTIONAL_.
    outputStream: The stream to output to. default: process.stderr.
    renderProgressBar: A truthy value to enable the rendering of the progress bar. default: true.
    renderResultsTable: A truthy value to enable the rendering of the results table. default: true.
    renderLatencyTable: A truthy value to enable the rendering of the advanced latency table. default: false.
    progressBarString: A string defining the format of the progress display output. Must be valid input for the progress bar module. default:'running [:bar] :percent'.

Example that just prints the table of results on completion:

  1. ``` js
  2. 'use strict'

  3. const autocannon = require('autocannon')

  4. const instance = autocannon({
  5.   url: 'http://localhost:3000'
  6. }, console.log)

  7. // this is used to kill the instance on CTRL-C
  8. process.once('SIGINT', () => {
  9.   instance.stop()
  10. })

  11. // just render results
  12. autocannon.track(instance, {renderProgressBar: false})
  13. ```

Check out this example to see it in use, as well.

autocannon.printResult(resultObject[, opts])


Print the result tables to the terminal, programmatically.

resultObject: The result object of autocannon. _REQUIRED_.
opts: Configuration options for printing the tables. This can have the following attributes. _OPTIONAL_.
    outputStream: The stream to output to. default: process.stderr.
    renderResultsTable: A truthy value to enable the rendering of the results table. default: true.
    renderLatencyTable: A truthy value to enable the rendering of the latency table. default: false.

Autocannon events


Because an autocannon instance is an EventEmitter, it emits several events. these are below:

start: Emitted once everything has been setup in your autocannon instance and it has started. Useful for if running the instance forever.
tick: Emitted every second this autocannon is running a benchmark. Useful for displaying stats, etc. Used by the track function. The tick event propagates  an object containing the counter and bytes values, which can be used for extended reports.
done: Emitted when the autocannon finishes a benchmark. passes the results as an argument to the callback.
response: Emitted when the autocannons http-client gets an HTTP response from the server. This passes the following arguments to the callback:
    client: The http-client itself. Can be used to modify the headers and body the client will send to the server. API below.
    statusCode: The HTTP status code of the response.
    resBytes: The response byte length.
    responseTime: The time taken to get a response after initiating the request.
reqError: Emitted in the case of a request error e.g. a timeout.
error: Emitted if there is an error during the setup phase of autocannon.

Results


The results object emitted by done and passed to the autocannon() callback has these properties:

title: Value of the title option passed to autocannon().
url: The URL that was targeted.
socketPath: The UNIX Domain Socket or Windows Named Pipe that was targeted, or undefined.
requests: A histogram object containing statistics about the number of requests that were sent per second.
latency: A histogram object containing statistics about response latency.
throughput: A histogram object containing statistics about the response data throughput per second.
duration: The amount of time the test took, in seconds.
errors: The number of connection errors (including timeouts) that occurred.
timeouts: The number of connection timeouts that occurred.
mismatches: The number of requests with a mismatched body.
start: A Date object representing when the test started.
finish: A Date object representing when the test ended.
connections: The amount of connections used (value of opts.connections).
pipelining: The number of pipelined requests used per connection (value of opts.pipelining).
non2xx: The number of non-2xx response status codes received.
resets: How many times the requests pipeline was reset due to setupRequest returning a falsey value.

The histogram objects for requests, latency and throughput are hdr-histogram-percentiles-obj objects and have this shape:

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

Client API


This object is passed as the first parameter of both the setupClient function and the response event from an autocannon instance. You can use this to modify the requests you are sending while benchmarking. This is also an EventEmitter, with the events and their params listed below.

client.setHeaders(headers): Used to modify the headers of the request this client iterator is currently on. headers should be an Object, or undefined if you want to remove your headers.
client.setBody(body): Used to modify the body of the request this client iterator is currently on. body should be a String or Buffer, or undefined if you want to remove the body.
client.setHeadersAndBody(headers, body): Used to modify both the headers and body this client iterator is currently on. headers and body should take the same form as above.
client.setRequest(request): Used to modify the entire request that this client iterator is currently on. Can have headers, body, method, or path as attributes. Defaults to the values passed into the autocannon instance when it was created. Note: call this when modifying multiple request values for faster encoding
client.setRequests(newRequests): Used to overwrite the entire requests array that was passed into the instance on initiation. Note: call this when modifying multiple requests for faster encoding

Client events


The events a Client can emit are listed here:

headers: Emitted when a request sent from this client has received the headers of its reply. This received an Object as the parameter.
body: Emitted when a request sent from this client has received the body of a reply. This receives a Buffer as the parameter.
response: Emitted when the client has received a completed response for a request it made. This is passed the following arguments:
    statusCode: The HTTP status code of the response.
    resBytes: The response byte length.
    responseTime: The time taken to get a response after initiating the request.
reset: Emitted when the requests pipeline was reset due to setupRequest returning a falsey value.

Example using the autocannon events and the client API and events:

  1. ``` js
  2. 'use strict'

  3. const autocannon = require('autocannon')

  4. const instance = autocannon({
  5.   url: 'http://localhost:3000',
  6.   setupClient: setupClient
  7. }, (err, result) => handleResults(result))
  8. // results passed to the callback are the same as those emitted from the done events
  9. instance.on('done', handleResults)

  10. instance.on('tick', () => console.log('ticking'))

  11. instance.on('response', handleResponse)

  12. function setupClient (client) {
  13.   client.on('body', console.log) // console.log a response body when its received
  14. }

  15. function handleResponse (client, statusCode, resBytes, responseTime) {
  16.   console.log(`Got response with code ${statusCode} in ${responseTime} milliseconds`)
  17.   console.log(`response: ${resBytes.toString()}`)

  18.   //update the body or headers
  19.   client.setHeaders({new: 'header'})
  20.   client.setBody('new body')
  21.   client.setHeadersAndBody({new: 'header'}, 'new body')
  22. }

  23. function handleResults(result) {
  24.   // ...
  25. }
  26. ```

Limitations


Autocannon is written in JavaScript for the Node.js runtime and it is CPU-bound.
We have verified that it yields comparable results with wrk when benchmarking Node.js
applications using the http module.
Nevertheless, it uses significantly more CPU than other tools that compiles to a binary such as wrk.
Autocannon can saturate the CPU, e.g. the autocannon process reaches 100%: in those cases,
we recommend using wrk2.

As an example, let's consider a run with 1000 connections on a server
with 4 cores with hyperthreading:

wrk uses 2 threads (by default) and an auxiliary one to collect the
  metrics with a total load of the CPU of 20% + 20% + 40%.
autocannon uses a single thread at 80% CPU load.

Both saturates a Node.js process at around 41k req/sec, however,
autocannon can saturate sooner because it is single-threaded.

Note that wrk does not support HTTP/1.1 pipelining. As a result, autocannon can create
more load on the server than wrk for each open connection.

Acknowledgements


This project was kindly sponsored by nearForm.

Logo and identity designed by Cosmic Fox Design: https://www.behance.net/cosmicfox.

[wrk][wrk] and [wrk2][wrk2] provided great inspiration.

Chat on Gitter


If you are using autocannon or you have any questions, let us know: Gitter

Contributors



License


Copyright Matteo Collina and other contributors, Licensed under MIT.

[node-gyp]: https://github.com/nodejs/node-gyp#installation
[wrk]: https://github.com/wg/wrk
[wrk2]: https://github.com/giltene/wrk2