Showing
5 changed files
with
98 additions
and
17 deletions
| ... | @@ -4,8 +4,6 @@ import java.io.IOException; | ... | @@ -4,8 +4,6 @@ import java.io.IOException; |
| 4 | 4 | ||
| 5 | import org.onlab.netty.Message; | 5 | import org.onlab.netty.Message; |
| 6 | import org.onlab.netty.MessageHandler; | 6 | import org.onlab.netty.MessageHandler; |
| 7 | -import org.slf4j.Logger; | ||
| 8 | -import org.slf4j.LoggerFactory; | ||
| 9 | 7 | ||
| 10 | 8 | ||
| 11 | /** | 9 | /** |
| ... | @@ -13,11 +11,8 @@ import org.slf4j.LoggerFactory; | ... | @@ -13,11 +11,8 @@ import org.slf4j.LoggerFactory; |
| 13 | */ | 11 | */ |
| 14 | public class NettyEchoHandler implements MessageHandler { | 12 | public class NettyEchoHandler implements MessageHandler { |
| 15 | 13 | ||
| 16 | - private final Logger log = LoggerFactory.getLogger(getClass()); | ||
| 17 | - | ||
| 18 | @Override | 14 | @Override |
| 19 | public void handle(Message message) throws IOException { | 15 | public void handle(Message message) throws IOException { |
| 20 | - //log.info("Received message. Echoing it back to the sender."); | ||
| 21 | message.respond(message.payload()); | 16 | message.respond(message.payload()); |
| 22 | } | 17 | } |
| 23 | } | 18 | } | ... | ... |
| 1 | +package org.onlab.onos.foo; | ||
| 2 | + | ||
| 3 | +import org.onlab.netty.Message; | ||
| 4 | +import org.onlab.netty.MessageHandler; | ||
| 5 | +import org.slf4j.Logger; | ||
| 6 | +import org.slf4j.LoggerFactory; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * A MessageHandler that simply logs the information. | ||
| 10 | + */ | ||
| 11 | +public class NettyNothingHandler implements MessageHandler { | ||
| 12 | + | ||
| 13 | + private final Logger log = LoggerFactory.getLogger(getClass()); | ||
| 14 | + | ||
| 15 | + @Override | ||
| 16 | + public void handle(Message message) { | ||
| 17 | + // Do nothing | ||
| 18 | + } | ||
| 19 | +} |
| ... | @@ -2,6 +2,7 @@ package org.onlab.onos.foo; | ... | @@ -2,6 +2,7 @@ package org.onlab.onos.foo; |
| 2 | 2 | ||
| 3 | import java.io.IOException; | 3 | import java.io.IOException; |
| 4 | import java.util.concurrent.ExecutionException; | 4 | import java.util.concurrent.ExecutionException; |
| 5 | +import java.util.concurrent.TimeUnit; | ||
| 5 | import java.util.concurrent.TimeoutException; | 6 | import java.util.concurrent.TimeoutException; |
| 6 | 7 | ||
| 7 | import org.onlab.metrics.MetricsComponent; | 8 | import org.onlab.metrics.MetricsComponent; |
| ... | @@ -15,14 +16,29 @@ import org.slf4j.LoggerFactory; | ... | @@ -15,14 +16,29 @@ import org.slf4j.LoggerFactory; |
| 15 | 16 | ||
| 16 | import com.codahale.metrics.Timer; | 17 | import com.codahale.metrics.Timer; |
| 17 | 18 | ||
| 19 | +/** | ||
| 20 | + * The Simple netty client test. | ||
| 21 | + */ | ||
| 18 | // FIXME: Should be move out to test or app | 22 | // FIXME: Should be move out to test or app |
| 19 | public final class SimpleNettyClient { | 23 | public final class SimpleNettyClient { |
| 20 | 24 | ||
| 21 | private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); | 25 | private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
| 22 | 26 | ||
| 27 | + static NettyMessagingService messaging; | ||
| 28 | + static MetricsManager metrics; | ||
| 29 | + | ||
| 23 | private SimpleNettyClient() { | 30 | private SimpleNettyClient() { |
| 24 | } | 31 | } |
| 25 | 32 | ||
| 33 | + /** | ||
| 34 | + * The entry point of application. | ||
| 35 | + * | ||
| 36 | + * @param args the input arguments | ||
| 37 | + * @throws IOException the iO exception | ||
| 38 | + * @throws InterruptedException the interrupted exception | ||
| 39 | + * @throws ExecutionException the execution exception | ||
| 40 | + * @throws TimeoutException the timeout exception | ||
| 41 | + */ | ||
| 26 | public static void main(String[] args) | 42 | public static void main(String[] args) |
| 27 | throws IOException, InterruptedException, ExecutionException, | 43 | throws IOException, InterruptedException, ExecutionException, |
| 28 | TimeoutException { | 44 | TimeoutException { |
| ... | @@ -34,13 +50,20 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); | ... | @@ -34,13 +50,20 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
| 34 | 50 | ||
| 35 | System.exit(0); | 51 | System.exit(0); |
| 36 | } | 52 | } |
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * Start standalone. | ||
| 56 | + * | ||
| 57 | + * @param args the args | ||
| 58 | + * @throws Exception the exception | ||
| 59 | + */ | ||
| 37 | public static void startStandalone(String[] args) throws Exception { | 60 | public static void startStandalone(String[] args) throws Exception { |
| 38 | String host = args.length > 0 ? args[0] : "localhost"; | 61 | String host = args.length > 0 ? args[0] : "localhost"; |
| 39 | int port = args.length > 1 ? Integer.parseInt(args[1]) : 8081; | 62 | int port = args.length > 1 ? Integer.parseInt(args[1]) : 8081; |
| 40 | int warmup = args.length > 2 ? Integer.parseInt(args[2]) : 1000; | 63 | int warmup = args.length > 2 ? Integer.parseInt(args[2]) : 1000; |
| 41 | int iterations = args.length > 3 ? Integer.parseInt(args[3]) : 50 * 100000; | 64 | int iterations = args.length > 3 ? Integer.parseInt(args[3]) : 50 * 100000; |
| 42 | - NettyMessagingService messaging = new TestNettyMessagingService(9081); | 65 | + messaging = new TestNettyMessagingService(9081); |
| 43 | - MetricsManager metrics = new MetricsManager(); | 66 | + metrics = new MetricsManager(); |
| 44 | Endpoint endpoint = new Endpoint(host, port); | 67 | Endpoint endpoint = new Endpoint(host, port); |
| 45 | messaging.activate(); | 68 | messaging.activate(); |
| 46 | metrics.activate(); | 69 | metrics.activate(); |
| ... | @@ -53,6 +76,7 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); | ... | @@ -53,6 +76,7 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
| 53 | Response response = messaging | 76 | Response response = messaging |
| 54 | .sendAndReceive(endpoint, "echo", | 77 | .sendAndReceive(endpoint, "echo", |
| 55 | "Hello World".getBytes()); | 78 | "Hello World".getBytes()); |
| 79 | + response.get(100000, TimeUnit.MILLISECONDS); | ||
| 56 | } | 80 | } |
| 57 | 81 | ||
| 58 | log.info("measuring async sender"); | 82 | log.info("measuring async sender"); |
| ... | @@ -64,19 +88,47 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); | ... | @@ -64,19 +88,47 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
| 64 | context.stop(); | 88 | context.stop(); |
| 65 | } | 89 | } |
| 66 | 90 | ||
| 91 | + log.info("measuring round-trip send & receive"); | ||
| 67 | Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); | 92 | Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); |
| 93 | + int timeouts = 0; | ||
| 94 | + | ||
| 68 | for (int i = 0; i < iterations; i++) { | 95 | for (int i = 0; i < iterations; i++) { |
| 96 | + Response response; | ||
| 69 | Timer.Context context = sendAndReceiveTimer.time(); | 97 | Timer.Context context = sendAndReceiveTimer.time(); |
| 70 | - Response response = messaging | 98 | + try { |
| 71 | - .sendAndReceive(endpoint, "echo", | 99 | + response = messaging |
| 72 | - "Hello World".getBytes()); | 100 | + .sendAndReceive(endpoint, "echo", |
| 101 | + "Hello World".getBytes()); | ||
| 102 | + response.get(10000, TimeUnit.MILLISECONDS); | ||
| 103 | + } catch (TimeoutException e) { | ||
| 104 | + timeouts++; | ||
| 105 | + log.info("timeout:" + timeouts + " at iteration:" + i); | ||
| 106 | + } finally { | ||
| 107 | + context.stop(); | ||
| 108 | + } | ||
| 73 | // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); | 109 | // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); |
| 74 | - context.stop(); | ||
| 75 | } | 110 | } |
| 76 | - metrics.deactivate(); | ||
| 77 | } | 111 | } |
| 78 | 112 | ||
| 113 | + public static void stop() { | ||
| 114 | + try { | ||
| 115 | + messaging.deactivate(); | ||
| 116 | + metrics.deactivate(); | ||
| 117 | + } catch (Exception e) { | ||
| 118 | + log.info("Unable to stop client %s", e); | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + /** | ||
| 123 | + * The type Test netty messaging service. | ||
| 124 | + */ | ||
| 79 | public static class TestNettyMessagingService extends NettyMessagingService { | 125 | public static class TestNettyMessagingService extends NettyMessagingService { |
| 126 | + /** | ||
| 127 | + * Instantiates a new Test netty messaging service. | ||
| 128 | + * | ||
| 129 | + * @param port the port | ||
| 130 | + * @throws Exception the exception | ||
| 131 | + */ | ||
| 80 | public TestNettyMessagingService(int port) throws Exception { | 132 | public TestNettyMessagingService(int port) throws Exception { |
| 81 | super(port); | 133 | super(port); |
| 82 | } | 134 | } | ... | ... |
| 1 | package org.onlab.onos.foo; | 1 | package org.onlab.onos.foo; |
| 2 | 2 | ||
| 3 | import static org.onlab.onos.foo.SimpleNettyClient.startStandalone; | 3 | import static org.onlab.onos.foo.SimpleNettyClient.startStandalone; |
| 4 | +import static org.onlab.onos.foo.SimpleNettyClient.stop; | ||
| 4 | 5 | ||
| 5 | import org.apache.karaf.shell.commands.Argument; | 6 | import org.apache.karaf.shell.commands.Argument; |
| 6 | import org.apache.karaf.shell.commands.Command; | 7 | import org.apache.karaf.shell.commands.Command; |
| ... | @@ -24,11 +25,11 @@ public class SimpleNettyClientCommand extends AbstractShellCommand { | ... | @@ -24,11 +25,11 @@ public class SimpleNettyClientCommand extends AbstractShellCommand { |
| 24 | 25 | ||
| 25 | @Argument(index = 2, name = "warmupCount", description = "Warm-up count", | 26 | @Argument(index = 2, name = "warmupCount", description = "Warm-up count", |
| 26 | required = false, multiValued = false) | 27 | required = false, multiValued = false) |
| 27 | - String warmupCount = "1000"; | 28 | + String warmupCount = "10000"; |
| 28 | 29 | ||
| 29 | @Argument(index = 3, name = "messageCount", description = "Message count", | 30 | @Argument(index = 3, name = "messageCount", description = "Message count", |
| 30 | required = false, multiValued = false) | 31 | required = false, multiValued = false) |
| 31 | - String messageCount = "100000"; | 32 | + String messageCount = "1000000"; |
| 32 | 33 | ||
| 33 | @Override | 34 | @Override |
| 34 | protected void execute() { | 35 | protected void execute() { |
| ... | @@ -37,5 +38,6 @@ public class SimpleNettyClientCommand extends AbstractShellCommand { | ... | @@ -37,5 +38,6 @@ public class SimpleNettyClientCommand extends AbstractShellCommand { |
| 37 | } catch (Exception e) { | 38 | } catch (Exception e) { |
| 38 | error("Unable to start client %s", e); | 39 | error("Unable to start client %s", e); |
| 39 | } | 40 | } |
| 41 | + stop(); | ||
| 40 | } | 42 | } |
| 41 | } | 43 | } | ... | ... |
| ... | @@ -12,17 +12,30 @@ import org.slf4j.LoggerFactory; | ... | @@ -12,17 +12,30 @@ import org.slf4j.LoggerFactory; |
| 12 | 12 | ||
| 13 | private SimpleNettyServer() {} | 13 | private SimpleNettyServer() {} |
| 14 | 14 | ||
| 15 | - public static void main(String... args) throws Exception { | 15 | + /** |
| 16 | + * The entry point of application. | ||
| 17 | + * | ||
| 18 | + * @param args the input arguments | ||
| 19 | + * @throws Exception the exception | ||
| 20 | + */ | ||
| 21 | + public static void main(String... args) throws Exception { | ||
| 16 | startStandalone(args); | 22 | startStandalone(args); |
| 17 | System.exit(0); | 23 | System.exit(0); |
| 18 | } | 24 | } |
| 19 | 25 | ||
| 20 | - public static void startStandalone(String[] args) throws Exception { | 26 | + /** |
| 27 | + * Start standalone server. | ||
| 28 | + * | ||
| 29 | + * @param args the args | ||
| 30 | + * @throws Exception the exception | ||
| 31 | + */ | ||
| 32 | + public static void startStandalone(String[] args) throws Exception { | ||
| 21 | int port = args.length > 0 ? Integer.parseInt(args[0]) : 8081; | 33 | int port = args.length > 0 ? Integer.parseInt(args[0]) : 8081; |
| 22 | NettyMessagingService server = new NettyMessagingService(port); | 34 | NettyMessagingService server = new NettyMessagingService(port); |
| 23 | server.activate(); | 35 | server.activate(); |
| 24 | - server.registerHandler("simple", new NettyLoggingHandler()); | 36 | + server.registerHandler("simple", new NettyNothingHandler()); |
| 25 | server.registerHandler("echo", new NettyEchoHandler()); | 37 | server.registerHandler("echo", new NettyEchoHandler()); |
| 38 | + log.info("Netty Server server on port " + port); | ||
| 26 | } | 39 | } |
| 27 | } | 40 | } |
| 28 | 41 | ... | ... |
-
Please register or login to post a comment