一、前言
本文主要总结 emqtt_bench 快速上手,让大家对 MQTT消息中间 BenchMark 测试工具有个整体了解,方便平常在性能测试工作查阅。
二、什么是emqtt_bench?
emqtt-bench 是 emqx 编写的用于测试MQTT服务器性能的测试程序,使用 Erlang 语言编写,与其它工具(如:JMeter)相比,emqtt_bench 的优点是安装和使用简单,占用的计算资源较少。但它支持的场景比较有限,需要结合其他监控工具测试指标数据。
三、如何安装?
emqtt-bench 支持 3 种方式来安装:
- Docker 镜像
- 二进制包
- 源代码构建
这里我演示二进制包如何安装:
前往 Releases 页面查看具体的 emqtt-bench 发布版本信息。
emqtt-bench的二进制包支撑的平台:
- Amazon Linux 2
- Amazon Linux 2023
- CentOS 7
- Rocky Linux 8
- Rocky Linux 9
- Debian 9
- Debian 10
- Debian 11
- Debian 12
- Ubuntu 16.04
- Ubuntu 18.04
- Ubuntu 20.04
- Ubuntu 22.04
- MacOS 11 (Intel)
- MacOS 12 (Intel)
- MacOS 12 (Apple Silicon)
这里我以 CentOS 7 为例:
- 工具地址:https://github.com/emqx/emqtt-bench/releases/download/0.4.18/emqtt-bench-0.4.18-el7-amd64.tar.gz
- 版本号:v0.4.18
##创建安装目录
mkdir emqtt-bench && cd emqtt-bench
##在线下载二进制包
wget https://github.com/emqx/emqtt-bench/releases/download/0.4.18/emqtt-bench-0.4.18-el7-amd64.tar.gz
##解压包
tar xfz emqtt-bench-0.4.18-el7-amd64.tar.gz
##删除文件
rm emqtt-bench-0.4.18-el7-amd64.tar.gz
##运行
cd bin/
./emqtt_bench
Usage: emqtt_bench pub | sub | conn [--help]
四、如何使用?
emqtt_bench 共三个子命令:
- pub:用于创建大量客户端执行发布消息的操作。
- sub:用于创建大量客户端执行订阅主题,并接受消息的操作。
- conn:用于创建大量的连接。
1、发布
执行 ./emqtt_bench pub --help 会得到可用的参数输出:
[root@data-server bin]# ./emqtt_bench pub --help
Usage: emqtt_bench pub [--help <help>] [-d <dist>] [-h [<host>]] [-p [<port>]] [-V [<version>]] [-c [<count>]]
[-n [<startnumber>]] [-i [<interval>]] [-I [<interval_of_msg>]] [-u <username>] [-P <password>]
[-t <topic>] [--payload-hdrs [<payload_hdrs>]] [-s [<size>]] [-m <message>] [-q [<qos>]]
[-Q [<qoe>]] [-r [<retain>]] [-k [<keepalive>]] [-C [<clean>]] [--reconnect [<reconnect>]]
[-x [<expiry>]] [-L [<limit>]] [-S [<ssl>]] [--certfile <certfile>] [--keyfile <keyfile>]
[--ws [<ws>]] [--quic [<quic>]] [--load-qst <nst_dets_file>] [--ifaddr <ifaddr>] [--prefix <prefix>]
[-s [<shortids>]] [-l <lowmem>] [-F [<inflight>]] [-w [<wait_before_publishing>]]
[--max-random-wait [<max_random_wait>]] [--min-random-wait [<min_random_wait>]]
[--num-retry-connect [<num_retry_connect>]] [-R [<conn_rate>]]
[--force-major-gc-interval [<force_major_gc_interval>]] [--log_to [<log_to>]]
[--retry-interval [<retry_interval>]]
--help help information
-d, --dist enable distribution port
-h, --host mqtt server hostname or comma-separated hostnames [default: localhost]
-p, --port mqtt server port number [default: 1883]
-V, --version mqtt protocol version: 3 | 4 | 5 [default: 5]
-c, --count max count of clients [default: 200]
-n, --startnumber The start point when assigning sequence numbers to clients. This is useful when running
multiple emqtt-bench instances to test the same broker (cluster), so the start number can be
planned to avoid client ID collision [default: 0]
-i, --interval interval of connecting to the broker [default: 10]
-I, --interval_of_msg interval of publishing message(ms) [default: 1000]
-u, --username username for connecting to server
-P, --password password for connecting to server
-t, --topic topic subscribe, support %u, %c, %i, %s variables
--payload-hdrs If set, add optional payload headers. cnt64: strictly increasing counter(64bit) per
publisher ts: Timestamp when emit example: --payload-hdrs cnt64,ts [default: ]
-s, --size payload size [default: 256]
-m, --message Set the message content for publish. Either a literal message content, or path to a file
with payload template specified via 'template://<file_path>'. Available variables:
%TIMESTAMP%, %TIMESTAMPMS%, %TIMESTAMPUS%, %TIMESTAMPNS%, %UNIQUE%, %RANDOM%. When using
'template://', --size option does not have effect except for when %RANDOM% placeholder is
used.
-q, --qos subscribe qos [default: 0]
-Q, --qoe Enable QoE tracking [default: false]
-r, --retain retain message [default: false]
-k, --keepalive keep alive in seconds [default: 300]
-C, --clean clean start [default: true]
--reconnect max retries of reconnects. 0: disabled [default: 0]
-x, --session-expiry Set 'Session-Expiry' for persistent sessions (seconds) [default: 0]
-L, --limit The max message count to publish, 0 means unlimited [default: 0]
-S, --ssl ssl socoket for connecting to server [default: false]
--certfile client certificate for authentication, if required by server
--keyfile client private key for authentication, if required by server
--ws websocket transport [default: false]
--quic QUIC transport [default: false]
--load-qst load quic session tickets from dets file
--ifaddr One or multiple (comma-separated) source IP addresses
--prefix Client ID prefix. If not provided '$HOST_bench_(pub|sub)_$RANDOM_$N' is used, where $HOST is
either the host name or the IP address provided in the --ifaddr option, $RANDOM is a random
number and $N is the sequence number assigned for each client. If provided, the $RANDOM
suffix will not be added.
-s, --shortids Use short client ID. If --prefix is provided, the prefix is added otherwise client ID is the
assigned sequence number. [default: false]
-l, --lowmem enable low mem mode, but use more CPU
-F, --inflight maximum inflight messages for QoS 1 an 2, value 0 for 'infinity' [default: 1]
-w, --wait-before-publishing wait for all publishers to have (at least tried to) connected before starting publishing
[default: false]
--max-random-wait maximum randomized period in ms that each publisher will wait before starting to publish
(uniform distribution) [default: 0]
--min-random-wait minimum randomized period in ms that each publisher will wait before starting to publish
(uniform distribution) [default: 0]
--num-retry-connect number of times to retry estabilishing a connection before giving up [default: 0]
-R, --connrate connection rate(/s), default: 0, fallback to use --interval [default: 0]
--force-major-gc-interval interval in milliseconds in which a major GC will be forced on the bench processes. a value
of 0 means disabled (default). this only takes effect when used together with --lowmem.
[default: 0]
--log_to Control where the log output goes. console: directly to the console null: quietly,
don't output any logs. [default: console]
--retry-interval Publisher's resend interval (in seconds) if the expected acknowledgement for a inflight
packet is not received within this interval. Default value 0 means no resend. [default: 0]
参数解释如下:
启动 10 个连接,分别每秒向主题 t 发送 100 条 Qos0 消息,其中每个消息体的大小为 16 字节大小:
./emqtt_bench pub -t t -h emqx-server -s 16 -q 0 -c 10 -I 10
客户端截图:
服务端截图:
2、订阅
启动 500 个连接,每个都以 Qos0 订阅 t 主题:
./emqtt_bench sub -t t -h emqx-server -c 500
客户端截图:
服务端截图:
3、连接
建立大量连接,启动 1000 个连接:
./emqtt_bench conn -h emqx-server -c 1000
客户端截图:
服务端截图:
五、总结
MQTT 协议是目前最适合物联网场景数据平台搭建的通信协议。基于此,BenchMark 联网场景中的 MQTT 消息采集与传递,以及如何构建一个百万级,甚至千万级 MQTT 消息平台,可以为物联网业务的企业用户提供平台架构设计参考。
参考资料: