Logo Vincent
返回文章列表

Node.js-WebServer开发实践:使用autocannon进行接口压测

Node.js
Node.js-WebServer开发实践:使用autocannon进行接口压测

【前言】

AutoCannon是基于Node.js的接口压测工具,

https://www.npmjs.com/package/autocannon

【安装】

npm i autocannon -g

【cli使用】

AutoCannon可以通过cli的方式使用,

其中各参数的含义可以直接输入autocannon查看,

Usage: autocannon [opts] URL

URL is any valid HTTP or HTTPS URL.
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.

Available options:

  -c/--connections NUM
        The number of concurrent connections to use. default: 10.
  -p/--pipelining NUM
        The number of pipelined requests to use. default: 1.
  -d/--duration SEC
        The number of seconds to run the autocannon. default: 10.
  -a/--amount NUM
        The number of requests to make before exiting the benchmark. If set, duration is ignored.
  -L NUM
        The number of milliseconds to elapse between taking samples. This controls the sample interval, & therefore the total number of samples, which affects statistical analyses. default: 1.
  -S/--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.
  -w/--workers
        Number of worker threads to use to fire requests.
  -W/--warmup
       Use a warm up interval before starting sampling.
       This enables startup processes to finish and traffic to normalize before sampling begins
       use -c and -d sub args e.g. `--warmup [ -c 1 -d 3 ]`
  --on-port
        Start the command listed after -- on the command line. When it starts listening on a port,
        start sending requests to that port. A URL is still required to send requests to
        the correct path. The hostname can be omitted, `localhost` will be used by default.
  -m/--method METHOD
        The HTTP method to use. default: 'GET'.
  -t/--timeout NUM
        The number of seconds before timing out and resetting a connection. default: 10
  -T/--title TITLE
        The title to place in the results for identification.
  -b/--body BODY
        The body of the request.
        NOTE: This option needs to be used with the '-H/--headers' option in some frameworks
  -F/--form FORM
        Upload a form (multipart/form-data). The form options can be a JSON string like
        '{ "field 1": { "type": "text", "value": "a text value"}, "field 2": { "type": "file", "path": "path to the file" } }'
        or a path to a JSON file containing the form options.
        When uploading a file the default filename value can be overridden by using the corresponding option:
        '{ "field name": { "type": "file", "path": "path to the file", "options": { "filename": "myfilename" } } }'
        Passing the filepath to the form can be done by using the corresponding option:
        '{ "field name": { "type": "file", "path": "path to the file", "options": { "filepath": "/some/path/myfilename" } } }'
  -i/--input FILE
        The body of the request. See '-b/body' for more details.
  -H/--headers K=V
        The request headers.
  --har FILE
        When provided, Autocannon will use requests from the HAR file.
        CAUTION: you have to specify one or more domains using URL option: only the HAR requests to the same domains will be considered.
        NOTE: you can still add extra headers with -H/--headers but -m/--method, -F/--form, -i/--input -b/--body will be ignored.
  -B/--bailout NUM
        The number of failures before initiating a bailout.
  -M/--maxConnectionRequests NUM
        The max number of requests to make per connection to the server.
  -O/--maxOverallRequests NUM
        The max number of requests to make overall to the server.
  -r/--connectionRate NUM
        The max number of requests to make per second from an individual connection.
  -R/--overallRate NUM
        The max number of requests to make per second from all connections.
        connection rate will take precedence if both are set.
        NOTE: if using rate limiting and a very large rate is entered which cannot be met,
              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.
  -C/--ignoreCoordinatedOmission
        Ignore the coordinated omission issue when requests should be sent at a fixed rate using 'connectionRate' or 'overallRate'.
        NOTE: it is not recommended to enable this option.
              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.
  -D/--reconnectRate NUM
        The number of requests to make before resetting a connections connection to the
        server.
  -n/--no-progress
        Don't render the progress bar. default: false.
  -l/--latency
        Print all the latency data. default: false.
  -I/--idReplacement
        Enable replacement of [<id>] with a randomly generated ID within the request body. default: false.
  -j/--json
        Print the output as newline delimited JSON. This will cause the progress bar and results not to be rendered. default: false.
  -f/--forever
        Run the benchmark forever. Efficiently restarts the benchmark on completion. default: false.
  -s/--servername
        Server name for the SNI (Server Name Indication) TLS extension. Defaults to the hostname of the URL when it is not an IP address.
  -x/--excludeErrorStats
        Exclude error statistics (non-2xx HTTP responses) from the final latency and bytes per second averages. default: false.
  -E/--expectBody EXPECTED
        Ensure the body matches this value. If enabled, mismatches count towards bailout.
        Enabling this option will slow down the load testing.
  --renderStatusCodes
        Print status codes and their respective statistics.
  --cert
        Path to cert chain in pem format
  --key
        Path to private key for specified cert in pem format
  --ca
        Path to trusted ca certificates for the test. This argument accepts both a single file as well as a list of files
  --debug
        Print connection errors to stderr.
  -v/--version
        Print the version number.
  -h/--help
        Print this menu.

例如10个并发连接,持续10s的用法如下,

autocannon -c 10 -p 1 -d 10 http://localhost:9999

结果如下,比较关键的是

1.响应时间的平均耗时

2.qps的平均值

【api使用】

除了cli的用法,autocannon也支持api使用,

例如下面代码实现效果同上,

详细用法可以看文档:https://www.npmjs.com/package/autocannon

// autocannon
const autocannon = require("autocannon");

// options
const options = {
  url: "http://localhost:9999",
  connections: 10,
  pipelining: 1,
  duration: 10,
};

// go
const instance = autocannon(options);
autocannon.track(instance);

相关推荐

Node.js-开发实践:下载文件

【前言】 下载文件是Node.js中最常见的功能, 但实际开发中下载文件也会隐藏各种各样的坑。 【原始代码】 如果在网络搜索Node.js下载文件代码, 大概会搜到类似下面的代码片段, 本文从这里开始,陆续优化下载文件这个功能。 上面的代码片段可以看到: 1.兼容了Node.js原生的http和ht

Node.js-开发实践:使用健壮的FS

【前言】 fs模块是nodejs中最常见的模块, 可是fs的使用经常会有各种意想不到的坑。 【高性能FS】 其中之一是没有使用高性能的fs, 导致在electron应用中造成卡顿, fs模块有3种使用方式, callback方式 1\. 书写会导致回调地狱 2\. 体现nodejs事件驱动,非阻塞i

Node.js-开发实践:高性能FS

【前言】 nodejs的fs模块相信大家都不陌生, 本文对比一下fs模块的三种使用方式。 【fs的三种使用方式】 nodejs官方提供了fs的三种使用方式, https://nodejs.org/dist/latestv18.x/docs/api/fs.htmlpromiseexample call

Node.js-WebServer开发实践:使用PM2-Cluster模式提升接口QPS

【前言】 pm2是nodejs进程管理工具, https://pm2.keymetrics.io/ 介绍详见之前的一篇文章: https://blog.csdn.net/uikoo9/article/details/79018750 , 本文介绍下pm2的cluster模式, 并使用pm2的clus

Node.js-WebServer开发实践:定时任务

【前言】 定时任务是服务端开发中的必备能力, 在nodejs web server的开发过程中, 可以使用cron实现定时任务能力, 【qiaotimer】 cron的使用可以查看官网文档, 这里封装了一个npm包,欢迎使用:https://code.insistime.com//qiaotimer

Node.js-WebServer开发实践:获取公网IP

【前言】 在nodejs server开发实践中,在一些场景下需要获取公网ip, 而nodejs中默认的获取ip的方法,只能获取本地的ip, 而无法获取公网ip,本文介绍如何获取公网ip 【服务端获取公网ip】 在服务端获取公网ip比较简单, nodejs下可以通过下述方法获取, 但是如果有使用ng

Node.js-WebServer开发实践:本地日志

【前言】 本地日志是服务端开发中必备的能力, 在nodejs web server的开发过程中, 可以使用log4js实现本地日志能力, 【qiaolog】 log4js的使用可以查看官网文档, 这里封装了一个npm包,欢迎使用: https://www.npmjs.com/package/qiao

Node.js-WebServer开发实践:上传文件

【前言】 文件上传是服务端开发中的必备能力, 在nodejs web server的开发过程中, 可以使用formidable实现文件上传能力, 【qiaozupload】 formidable的使用可以查看官网文档, 这里封装了一个npm包,欢迎使用: https://code.insistime

Node.js-开发实践:图片处理

【前言】 使用nodejs的过程中会遇到一些处理图片的场景, 比如上传图片时进行压缩, 或者nodejs开发客户端本处理图片等, 本文介绍下nodejs常见的图片处理操作。 【常见图片处理库】 nodejs常见的图片处理库如下, 可以看到sharp从各方面都遥遥领先 npm包 github地址 gi

一篇文章开发Node.js-WebServer

【前言】 Node.js的服务端框架很多,耳熟能详的有express,koa等, 本文从零到一开发一个Node.js的web server。 https://nodejs.org/en/ https://expressjs.com/ https://koa.bootcss.com/ 【http】 要

© 2026 vincentqiao.com . 保留所有权利。