性能压测
This content is not available in your language yet.
很多用户比较关心 smart-mqtt 的性能表现,为了得出一个相对准确的结论。我们通过自研的压测工具分别对 smart-mqtt 和同类产品进行压测。
一、环境准备
Section titled “一、环境准备”一台装有 Docker 的 Linux 服务器、Windows 或者 Macbook。
支持主流的 amd64 和 arm64 架构。
二、压测结果
Section titled “二、压测结果”场景一:消息订阅
Section titled “场景一:消息订阅”场景设计:
- MQTT Client 订阅者数量:2000
- Topic 数量:128
- 消息 payload 大小:128 字节
| 消息质量 | smart-mqtt | emqx | bifromq |
|---|---|---|---|
| QoS0 | 790W/s | - | - |
| QoS1 | 320W/s | - | - |
| QoS2 | 190W/s | - | - |
场景二:消息发布
Section titled “场景二:消息发布”场景设计:
- MQTT Client 发布者数量:2000
- Topic 数量:128
- 消息 payload 大小:128 字节
| 消息质量 | smart-mqtt | emqx | bifromq |
|---|---|---|---|
| QoS0 | 97W/s | - | - |
| QoS1 | 63W/s | - | - |
| QoS2 | 52W/s | - | - |
场景三:海量连接
Section titled “场景三:海量连接”场景设计:
- Topic 订阅数:2 个
- 消息 payload 大小:128 字节
| 连接量 | smart-mqtt | emqx | bifromq |
|---|---|---|---|
| 10万 | - | - | - |
| 30万 | - | - | - |
| 50万 | - | - | - |
| 80万 | - | - | - |
| 100万 | - | - | - |
三、压测过程
Section titled “三、压测过程”在 smart-mqtt 的仓库根目录下,我们提供了用于性能测试的 docker-compose.yml 文件。
在这个文件中包含了多款 MQTT Broker,对应的 docker 服务名皆为:mqtt-broker。当需要对某个 broker 进行测试时,只需移除其注释,将另外的同名服务注释掉便可(即同时刻只能存在一个服务名为 mqtt-broker 的 docker 容器)。
目前已支持的有:
- smart-mqtt
- emqx
- BifroMQ
3.1 压测场景
Section titled “3.1 压测场景”docker-compose.yml 中的 smart-mqtt-bench 是用于压测的客户端服务,需要针对不同的压测场景配置压测参数。
消息订阅压测
Section titled “消息订阅压测”services: smart-mqtt-bench: # 省略其他配置... command: java -cp smart-mqtt-bench.jar -Dhost=mqtt-broker -Dconnect=2000 -Dqos=2 -Dpublisher=10 -Dcount=1 -Dpayload=128 org.smartboot.bench.mqtt.Subscribe参数说明:
| 参数名 | 参数描述 |
|---|---|
| host | MQTT Broker 地址,已针对压测对象配置完成,无需调整 |
| port | MQTT Broker 服务的端口号,默认:1883,不推荐调整 |
| connect | 压测客户端数量,默认:1000,可按需调整 |
| payload | 压测消息的 payload 字节数,默认:128,可按需调整 |
| topic | 压测订阅的 Topic 数量,默认:128,可按需调整 |
| publisher | 负责生产消息的客户端数量,默认:1 |
| count | 每个 publisher 周期内发送的消息数量,默认:1 |
每秒中发送的最多消息数为:publisher * count * 500,所以 connect 个订阅者产生订阅量理论值为:publisher * count * 500 * connect。
消息发布压测
Section titled “消息发布压测”services: smart-mqtt-bench: # 省略其他配置... command: java -cp smart-mqtt-bench.jar -Dhost=mqtt-broker -Dconnect=2000 -Dqos=0 -Dcount=10 -Dpayload=128 org.smartboot.bench.mqtt.Publish参数说明:
| 参数名 | 参数描述 |
|---|---|
| host | MQTT Broker 地址,已针对压测对象配置完成,无需调整 |
| port | MQTT Broker 服务的端口号,默认:1883 |
| connect | 压测客户端数量,默认:1000 |
| payload | 压测消息的 payload 字节数,默认:128 |
| topic | 压测发布的 Topic 数量,默认:128 |
| count | 每个客户端每次发送的消息数量,默认:1 |
| qos | 消息质量等级,默认:0 |
3.2 执行压测
Section titled “3.2 执行压测”- 启动 Broker 和压测客户端:
docker-compose up -d- 查看压测结果:
# 查看 smart-mqtt-bench 容器的日志docker-compose logs -f smart-mqtt-bench- 停止压测:
docker-compose down四、压测工具 smart-mqtt-bench
Section titled “四、压测工具 smart-mqtt-bench”smart-mqtt-bench 是专门为 smart-mqtt 设计的压测工具,具有以下特点:
- 轻量级:基于 Java 开发,无外部依赖
- 高性能:采用异步 IO 模型,单机可模拟数万连接
- 易用性:命令行参数配置,支持多种压测场景
- 可扩展:开源代码,可根据需求定制
4.1 使用方式
Section titled “4.1 使用方式”# 压测订阅性能java -cp smart-mqtt-bench.jar \ -Dhost=localhost \ -Dport=1883 \ -Dconnect=1000 \ -Dtopic=128 \ -Dpublisher=10 \ -Dpayload=128 \ org.smartboot.bench.mqtt.Subscribe
# 压测发布性能java -cp smart-mqtt-bench.jar \ -Dhost=localhost \ -Dport=1883 \ -Dconnect=1000 \ -Dtopic=128 \ -Dcount=10 \ -Dpayload=128 \ -Dqos=1 \ org.smartboot.bench.mqtt.Publish
# 压测连接性能java -cp smart-mqtt-bench.jar \ -Dhost=localhost \ -Dport=1883 \ -Dconnect=100000 \ org.smartboot.bench.mqtt.Connect4.2 自定义压测
Section titled “4.2 自定义压测”如果需要更复杂的压测场景,可以直接使用 smart-mqtt-bench 的 Java API:
public class CustomBenchmark { public static void main(String[] args) { // 创建压测配置 BenchOptions options = new BenchOptions(); options.setHost("localhost"); options.setPort(1883); options.setConnectCount(1000); options.setTopicCount(128); options.setPayloadSize(128); options.setQos(1);
// 创建并启动压测 SubscribeBenchmark benchmark = new SubscribeBenchmark(options); benchmark.start(); }}