hop-2012/java/hop/TestPingPong.java

72 lines
2.2 KiB
Java

/*
* Copyright (c) 2011 Tony Garnock-Jones. All rights reserved.
*/
package hop;
import java.io.IOException;
/**
*/
public class TestPingPong {
public static void main(final String[] args) {
try {
new Thread(new Runnable() { public void run() {
try {
run1(args[0]);
} catch (Exception e) {
e.printStackTrace();
}
} }).start();
run2(args[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void run1(String hostname) throws IOException, InterruptedException {
NodeContainer nc = new NodeContainer();
System.out.println("Hostname: " + hostname);
System.out.println("Container: " + nc.getName());
Relay r = new Relay(nc, hostname);
ServerApi api = new ServerApi(nc, r.getRemoteName());
api.createQueue("req");
Subscription sub = api.subscribe("req", null);
while (true) {
Object x = sub.getQueue().take();
//System.out.println("Message: " + x);
api.post("rep", "reply", SexpList.with("ok").and(x), null);
api.flush();
}
}
public static void run2(String hostname) throws IOException, InterruptedException {
NodeContainer nc = new NodeContainer();
System.out.println("Hostname: " + hostname);
System.out.println("Container: " + nc.getName());
Relay r = new Relay(nc, hostname);
ServerApi api = new ServerApi(nc, r.getRemoteName());
api.createQueue("req");
api.createQueue("rep");
Subscription sub = api.subscribe("rep", null);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
api.post("req", "request", Integer.toString(i), null);
api.flush();
sub.getQueue().take();
int j = i + 1;
if ((j % 100) == 0) {
long now = System.currentTimeMillis();
double delta = (now - startTime) / 1000.0;
System.out.println("Message " + j + ": " + (j / delta) + " Hz");
}
}
}
}