# 单机千万级连接测试

{% hint style="info" %}

```
服务器硬件配置
Dell Inc.
型号	PowerEdge R430
CPU	8 CPUs x Intel(R) Xeon(R) CPU E5-2620 v4 @ 2.10GHz
内存	64 GB //测试客户端机器内存升级到96 GB
操作系统 Ubuntu 16.04
```

{% endhint %}

{% hint style="warning" %}
需要测试千万级连接，确保测试工具独占服务器，并硬件内存大于或等于256GB，由于测试硬件资源受限，以400万连接数据测试结果作为参考依据，单机千万连接完全有可能达到。当商业应用时需要考滤高连接的self-ddos问题，应该尽量设置更长的客户端keepalive值。
{% endhint %}

### 服务端系统参数调优(Linux)

修改文件`/etc/sysctl.conf`,配置网络参数。

```
net.ipv4.tcp_wmem = 4096 87380 4161536
net.ipv4.tcp_rmem = 4096 87380 4161536
net.ipv4.tcp_mem = 786432 2097152 3145728
fs.file-max = 12000000 #一千两百万
```

执行`/sbin/sysctl -p`即时生效。

```
echo 12000000 > /proc/sys/fs/nr_open #需root权限
```

&#x20;/etc/systemd/system.conf 设置服务最大文件句柄数:

```
DefaultLimitNOFILE=12000000
```

修改`/etc/security/limits.conf`文件

```bash
* hard nofile  12000000
* soft nofile  12000000
* soft nproc   12000000
* hard nproc   12000000
```

{% hint style="info" %}
需重启系统生效，服务器操作系统参数调优完成。关于启动CP7服务器端程序请参阅本章中的《程序安装》小节
{% endhint %}

#### 启动CP7

```
# 下载服务器端
wget https://github.com/Coolpy7/Coolpy7/raw/master/go_build_Coolpy7_go_linux.zip
# 解压文件
sudo unzip go_build_Coolpy7_go_linux.zip
# 提权
sudo chmod -R 777 go_build_Coolpy7_go_linux
# 启动Coolpy7 启动参数
# l 当前服务Host地址 (默认为:1883即本地1883端口,此参数一般默认即可，无需配置)
# a 连接接入调度器最大线程，此值可防止暴力连接攻击，对已连接客户端进行优先保护 (默认值128)
./go_build_Coolpy7_go_linux

# 启动成功后会打印如下信息，即说明服务端已正常启动，host于1883端口，请确保相关防火墙配置可用
2018/10/29 12:59:55 Coolpy7 tcp is listening on [::]:1883
```

### 客户端的参数调优(Linux)

根据TCP/IP协议，由于端口是16位整数，也就只能是0到 65535，而0到1023是预留端口，所以能分配的端口只是1024到65534，也就是64511个。一台机器一个IP只能创建六万多个长连接。 要想达到更多的客户端连接，可以用更多的机器或者网卡，也可以使用虚拟IP来实现，比如下面的命令增加了19个IP地址，其中一个给服务器用，其它18个给client,这样可以产生18 \* 60000 = 1080000个连接。本测试用例需要增加到一千万或以上的足够IP。

```
root@:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:16:3e:0b:2e:a0  
          inet addr:x.x.x.x  Bcast:x.x.x.x  Mask:255.255.240.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4706185 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4705832 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1005604068 (1.0 GB)  TX bytes:5556912863 (5.5 GB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:5875416 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5875416 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:3566710813 (3.5 GB)  TX bytes:3566710813 (3.5 GB)
```

以上指令查看到网卡名称为 eth0

```
ifconfig eth0:0 192.168.77.10 netmask 255.255.255.0 up
ifconfig eth0:1 192.168.77.11 netmask 255.255.255.0 up
ifconfig eth0:2 192.168.77.12 netmask 255.255.255.0 up
ifconfig eth0:3 192.168.77.13 netmask 255.255.255.0 up
ifconfig eth0:4 192.168.77.14 netmask 255.255.255.0 up
ifconfig eth0:5 192.168.77.15 netmask 255.255.255.0 up
ifconfig eth0:6 192.168.77.16 netmask 255.255.255.0 up
ifconfig eth0:7 192.168.77.17 netmask 255.255.255.0 up
ifconfig eth0:8 192.168.77.18 netmask 255.255.255.0 up
ifconfig eth0:9 192.168.77.19 netmask 255.255.255.0 up
```

以上指令操作增加虚拟IP，添加完成后通过 ifconfig 指令查看确认所有虚拟IP最已列出。

修改`/etc/sysctl.conf`文件：

```
fs.file-max = 12000000
fs.nr_open = 12000000
net.ipv4.ip_local_port_range = 1024 65535
```

执行`/sbin/sysctl -p`即时生效。

```
echo 12000000 > /proc/sys/fs/nr_open #需root权限
```

&#x20;*`/etc/security/limits.conf`配置*

```
* soft    nofile      12000000
* hard    nofile      12000000
* soft    nproc       12000000
* hard    nproc       12000000
```

{% hint style="info" %}
1.通过ulimit -a指令查看配置是否生效

2.通过ifconfig指令查看虚拟ip是否生效

3.如果出现ip配置错误，执行 $sudo ip addr flush dev eth0
{% endhint %}

### 使用Coolpy7\_benchmark测试客户端测试四百万级连接

```
# 下载测试工具
git clone https://github.com/Coolpy7/coolpy7_benchmark.git
# 进入bin文件夹
cd bin

# 启动Coolpy7静连接测试工具
# 参数url: Coolpy7服务端所在服务器ip地址，默认端口号：1883
# 参数workers: 需要创建的客户端连接数量
# 参数cid: mqtt连接客户端(ClientId)前缀，工具会自动以cid+WorkersId作为连接clientid值
# 参数topic: 连接成功后订阅的topic主题（支持以%i取得workersid：例: mytopic%i）不进行订阅测试填写null字符串
# 参数keepalive: 连接ping时间间隔
# 参数clear: mqtt客户端连接的clearsession属性，当设置为true时重连时不能再次订阅qos0以上的主题
# 以下例示建立连接到192.168.200.238所在的Coolpy7服务器端，并创建400万个MQTT客户端连接
./go_build_cp7_bench_sub_go_linux -workers=4000000 -cid=tqy -topic=null -qos=0 -url=tcp://192.168.200.238:1883 -keepalive=60000s -clear=true
```

| 客户端连接数 | 系统使用内存 | RES使用内存 | VIRT使用内存 | CPU使用    | 网络(进) | 网络(出) |
| ------ | ------ | ------- | -------- | -------- | ----- | ----- |
| 100万   | 4.5G   | 2633M   | 2789M    | 260/1600 | 2.8M  | 2M    |
| 200万   | 10.3G  | 4009M   | 4152M    | 277/1600 | 2.98M | 2.15M |
| 300万   | 14.7G  | 5517M   | 5682M    | 278/1600 | 3.05M | 2.21M |
| 400万   | 19.3G  | 7055M   | 7231M    | 270/1600 | 2.91M | 2.18M |

{% hint style="info" %}
测试时需时刻关注服务器端网络资源使用情况，测试数据显示在高连接数下ping/pong状态下已占用极大的网络带宽，需根据当前设备的硬件资源合理设置最大连接数量以保证系统的稳定长期运行
{% endhint %}

### 相关测试截图

<div align="left"><img src="/files/-LSO--mm1E4Bn3lfHtTj" alt="服务器端启动并host于1883 TCP端口"></div>

<div align="left"><img src="/files/-LSO5IwJXI8VkUK54m9u" alt="通过HTOP工具监测Coolpy7(连接时)硬件资源占用情况"></div>

<div align="left"><img src="/files/-LSO4ulY435XICIWNN-G" alt="通过NLOAD工具监测Coolpy7(连接时)网络资源占用情况"></div>

<div align="left"><img src="/files/-LSOTmIJCGeyE-8_mamF" alt="使用Coolpy_benchmark工具测试连接400万连接"></div>

{% hint style="info" %}
从服务器端的硬件资源使用情况得出结论单机千万级连接即可实现，如阁下有相关硬件设备实测千万级用例，请把测试结果告知Coolpy7官方。感谢！
{% endhint %}

1. 测试工具开源地址：<https://github.com/Coolpy7/coolpy7_benchmark>
2. 工具会以1000为单位打印连接信息
3. 工具启动后会一直创建参数指定的客户端连接数

#### 消息推送压力测试

1. 测试环境：1客户端->Coolpy7->扩展服务
2. 客户端测试源代码：<https://github.com/Coolpy7/coolpy7_benchmark/blob/master/test_bench_pub/bench_pub_test.go>
3. 测试结果

```
goos: darwin
goarch: amd64
BenchmarkPub
BenchmarkPub-8   	  305800	    694157 ns/op	     545 B/op	       8 allocs/op
PASS
```

### 按应用场景测试

MQTT 是一个设计得非常出色的传输层协议，在移动消息、物联网、车联网、智能硬件甚至能源勘探等领域有着广泛的应用。1个字节报头、2个字节心跳、消息 QoS 支持等设计，非常适合在低带宽、不可靠网络、嵌入式设备上应用。

不同的应用有不同的系统要求，用户使用Coolpy7消息服务器前，可以按自己的应用场景进行测试，而不是简单的连接压力测试:

1. Android 消息推送: 推送消息广播测试。
2. 移动即时消息应用: 消息收发确认测试。
3. 智能硬件应用: 消息的往返时延测试。
4. 物联网数据采集: 并发连接与吞吐测试。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coolpy7.gitbook.io/coolpy7book/kai-shi-shi-yong/dan-ji-qian-wan-ji-lian-jie-ce-shi-shuo-ming.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
