From 37e5f39dc18d13138bdf08eb6fa958fb44586662 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 6 Jan 2011 09:15:26 -0500 Subject: [PATCH] Ping-pong --- java/hop/TestPingPong.java | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 java/hop/TestPingPong.java diff --git a/java/hop/TestPingPong.java b/java/hop/TestPingPong.java new file mode 100644 index 0000000..c8e7107 --- /dev/null +++ b/java/hop/TestPingPong.java @@ -0,0 +1,69 @@ +/* + * 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); + } + } + + 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); + //System.out.println("Reply: " + 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"); + } + } + } +}