我想使用我发现的这个库,它是 zeromq 的纯 Java 端口(不是包装器)。我正在尝试对其进行测试,虽然它声称有一些不错的数字,但我正在执行的测试结果相当糟糕,甚至在本地执行(客户端和在同一台机器上服务)。我确定这是我做错了什么。大约需要。5 秒来执行这 10.000 条消息循环。
我所做的只是采用 Hello world 示例并删除了暂停和系统输出。这是代码:
服务器:
package guide;
import org.jeromq.ZMQ;
public class hwserver{
public static void main(String[] args) throws Exception{
// Prepare our context and socket
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.REP);
System.out.println("Binding hello world server");
socket.bind ("tcp://*:5555");
while (true) {
byte[] reply = socket.recv(0);
String requestString = "Hello" ;
byte[] request = requestString.getBytes();
socket.send(request, 0);
}
}
}
客户端:
package guide;
import org.jeromq.ZMQ;
public class hwclient{
public static void main(String[] args){
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.REQ);
socket.connect ("tcp://localhost:5555");
System.out.println("Connecting to hello world server");
long start = System.currentTimeMillis();
for(int request_nbr = 0; request_nbr != 10_000; request_nbr++) {
String requestString = "Hello" ;
byte[] request = requestString.getBytes();
socket.send(request, 0);
byte[] reply = socket.recv(0);
}
long end = System.currentTimeMillis();
System.out.println(end-start);
socket.close();
context.term();
}
}
是否可以修复此代码并获得一些不错的数字?