MqttBenchmark.java 1.7 KB
Newer Older
如梦技术's avatar
如梦技术 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.net.dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.dreamlu.iot.mqtt.benchmark;

import net.dreamlu.iot.mqtt.core.client.MqttClient;

浅梦2013's avatar
浅梦2013 已提交
21
import java.util.UUID;
浅梦2013's avatar
浅梦2013 已提交
22 23
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
24

如梦技术's avatar
如梦技术 已提交
25 26 27 28 29 30 31
/**
 * mqtt 压力测试
 *
 * @author L.cm
 */
public class MqttBenchmark {

32
	public static void main(String[] args) {
如梦技术's avatar
如梦技术 已提交
33
		// 1. 模拟 1w 连接,在开发机(i5-7500 4核4线程 win10 MqttServer 6G)1万连连接很轻松。
如梦技术's avatar
如梦技术 已提交
34
		// 注意: windows 上需要修改最大的 Tcp 连接数,不然超不过 2W。
35 36
		// 《修改Windows服务器最大的Tcp连接数》:https://www.jianshu.com/p/00136a97d2d8
		int connCount = 2_0000;
浅梦2013's avatar
浅梦2013 已提交
37 38 39 40 41 42
		int threadCount = 1000;
		String ip = "8.130.24.89";
		ExecutorService executor = Executors.newFixedThreadPool(threadCount);
		for (int i = 0; i < connCount; i++) {
			int num = i;
			executor.submit(() -> newClient(ip, num));
如梦技术's avatar
如梦技术 已提交
43
		}
浅梦2013's avatar
浅梦2013 已提交
44 45 46 47 48
	}

	private static void newClient(String ip, int i) {
		MqttClient.create()
			.ip(ip)
浅梦2013's avatar
浅梦2013 已提交
49
			.clientId(UUID.randomUUID().toString() + '-' + i)
浅梦2013's avatar
浅梦2013 已提交
50 51 52 53
			.username("admin")
			.password("123456")
			.readBufferSize(512)
			.connect();
如梦技术's avatar
如梦技术 已提交
54 55 56
	}

}