我正在尝试与mqtt代理建立多个(100k)mqtt连接,但使用paho java客户端不能超过5000。
我从另一个帖子得到了很好的指针,但是当我越过5000线程限制时遇到错误。 错误:java.lang.OutOfMemoryError:无法创建新的本机线程
我在云中有一个16GB的RAM / 16VCPU ubuntu实例,所以资源在这里看起来不是问题。
我当前的ulimit设置如下。
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 128311
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 30000000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 200000
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
此外,该帖子还谈到了添加21个虚拟地址。在这个云实例上,我将eth0.cfg设置为:
----------------------
# The primary network interface
auto eth0
iface eth0 inet dhcp
---------------------------
如何在此处添加虚拟IP地址?
这里的任何指示都会有所帮助。
感谢。
编辑:这是我的线程类代码 - public class SendMessage extends Thread { public String clientId; public String topicId; public String用户名; public String密码; public String payloadType;
SendMessage(String topicId,String clientId,String username,String password,String payloadType) {
this.topicId = topicId;
this.password = password;
this.username = username;
this.clientId = clientId;
this.payloadType = payloadType;
}
SendMessage(String topicId) {
this.topicId = topicId;
}
public void run() {
// Perform the requested action
try {
ArrayList<MqttClient> sampleClient = new ArrayList<MqttClient>();
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setPassword(password.toCharArray());
connOpts.setUserName(username);
for (int i = 0; i < connectionCountPerThread ; i++) {
MqttClient newClient = new MqttClient("tcp://" + url, clientId + "_" +i);
sampleClient.add(newClient);
newClient.connect(connOpts);
}
MqttMessage message1;
int qos = 0;
MockObservations mo = new MockObservations();
for (int i = 0 ; i < msgCount ; i++) {
for (int conn = 0; conn < connectionCountPerThread ; conn++) {
message1 = new MqttMessage(mo.getSensorReading(payloadType).getBytes());
message1.setQos(qos);
sampleClient.get(conn).publish(topicId, message1);
}
}
for (int conn = 0; conn < connectionCountPerThread ; conn++) {
sampleClient.get(conn).disconnect();
}
} catch(MqttException me) {
System.out.println("reason "+me.getReasonCode());
System.out.println("msg "+me.getMessage());
System.out.println("loc "+me.getLocalizedMessage());
System.out.println("cause "+me.getCause());
System.out.println("excep "+me);
me.printStackTrace();
}
}
}
答案 0 :(得分:1)
你创造了太多线程。据我所知,Paho每个连接使用2个线程。使用“每个连接的线程”模型不会太过分。我相信自己创建这样一个负载测试工具的唯一方法是使用Java的NIO方法。
要获得更高的连接(以及非常糟糕的延迟),您可以尝试增加JVM堆大小。
这可能会对您有所帮助:https://en.wikipedia.org/wiki/C10k_problem