pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 1 | package org.onlab.onos.foo; |
| 2 | |
pankaj | 6245a7a | 2014-10-10 17:50:55 -0700 | [diff] [blame] | 3 | import static java.lang.Thread.sleep; |
| 4 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 5 | import java.io.IOException; |
| 6 | import java.util.concurrent.ExecutionException; |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 7 | import java.util.concurrent.TimeUnit; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 8 | import java.util.concurrent.TimeoutException; |
| 9 | |
| 10 | import org.onlab.metrics.MetricsComponent; |
| 11 | import org.onlab.metrics.MetricsFeature; |
| 12 | import org.onlab.metrics.MetricsManager; |
| 13 | import org.onlab.netty.Endpoint; |
| 14 | import org.onlab.netty.NettyMessagingService; |
| 15 | import org.onlab.netty.Response; |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 16 | import org.slf4j.Logger; |
| 17 | import org.slf4j.LoggerFactory; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 18 | |
| 19 | import com.codahale.metrics.Timer; |
| 20 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 21 | /** |
| 22 | * The Simple netty client test. |
| 23 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 24 | // FIXME: Should be move out to test or app |
| 25 | public final class SimpleNettyClient { |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 26 | |
| 27 | private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
| 28 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 29 | static NettyMessagingService messaging; |
| 30 | static MetricsManager metrics; |
| 31 | |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 32 | private SimpleNettyClient() { |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 33 | } |
| 34 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 35 | /** |
| 36 | * The entry point of application. |
| 37 | * |
| 38 | * @param args the input arguments |
| 39 | * @throws IOException the iO exception |
| 40 | * @throws InterruptedException the interrupted exception |
| 41 | * @throws ExecutionException the execution exception |
| 42 | * @throws TimeoutException the timeout exception |
| 43 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 44 | public static void main(String[] args) |
| 45 | throws IOException, InterruptedException, ExecutionException, |
| 46 | TimeoutException { |
| 47 | try { |
| 48 | startStandalone(args); |
| 49 | } catch (Exception e) { |
| 50 | e.printStackTrace(); |
| 51 | } |
| 52 | |
| 53 | System.exit(0); |
| 54 | } |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Start standalone. |
| 58 | * |
| 59 | * @param args the args |
| 60 | * @throws Exception the exception |
| 61 | */ |
pankaj | 31b8eab | 2014-10-08 18:14:08 -0700 | [diff] [blame] | 62 | public static void startStandalone(String[] args) throws Exception { |
pankaj | 13373b5 | 2014-10-07 18:26:07 -0700 | [diff] [blame] | 63 | String host = args.length > 0 ? args[0] : "localhost"; |
| 64 | int port = args.length > 1 ? Integer.parseInt(args[1]) : 8081; |
| 65 | int warmup = args.length > 2 ? Integer.parseInt(args[2]) : 1000; |
| 66 | int iterations = args.length > 3 ? Integer.parseInt(args[3]) : 50 * 100000; |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 67 | messaging = new TestNettyMessagingService(9081); |
| 68 | metrics = new MetricsManager(); |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 69 | Endpoint endpoint = new Endpoint(host, port); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 70 | messaging.activate(); |
| 71 | metrics.activate(); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 72 | MetricsFeature feature = new MetricsFeature("latency"); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 73 | MetricsComponent component = metrics.registerComponent("NettyMessaging"); |
pankaj | 31b8eab | 2014-10-08 18:14:08 -0700 | [diff] [blame] | 74 | log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 75 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 76 | for (int i = 0; i < warmup; i++) { |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 77 | messaging.sendAsync(endpoint, "simple", "Hello World".getBytes()); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 78 | Response response = messaging |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 79 | .sendAndReceive(endpoint, "echo", |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 80 | "Hello World".getBytes()); |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 81 | response.get(100000, TimeUnit.MILLISECONDS); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 82 | } |
| 83 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 84 | log.info("measuring round-trip send & receive"); |
pankaj | f0f80b2 | 2014-10-07 18:37:32 -0700 | [diff] [blame] | 85 | Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 86 | int timeouts = 0; |
| 87 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 88 | for (int i = 0; i < iterations; i++) { |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 89 | Response response; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 90 | Timer.Context context = sendAndReceiveTimer.time(); |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 91 | try { |
| 92 | response = messaging |
| 93 | .sendAndReceive(endpoint, "echo", |
| 94 | "Hello World".getBytes()); |
| 95 | response.get(10000, TimeUnit.MILLISECONDS); |
| 96 | } catch (TimeoutException e) { |
| 97 | timeouts++; |
| 98 | log.info("timeout:" + timeouts + " at iteration:" + i); |
| 99 | } finally { |
| 100 | context.stop(); |
| 101 | } |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 102 | // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 103 | } |
pankaj | 6245a7a | 2014-10-10 17:50:55 -0700 | [diff] [blame] | 104 | |
pankaj | 16c9a22 | 2014-10-10 18:24:38 -0700 | [diff] [blame] | 105 | //sleep(1000); |
pankaj | 6245a7a | 2014-10-10 17:50:55 -0700 | [diff] [blame] | 106 | log.info("measuring async sender"); |
| 107 | Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender"); |
| 108 | |
| 109 | for (int i = 0; i < iterations; i++) { |
| 110 | Timer.Context context = sendAsyncTimer.time(); |
| 111 | messaging.sendAsync(endpoint, "simple", "Hello World".getBytes()); |
| 112 | context.stop(); |
| 113 | } |
pankaj | 16c9a22 | 2014-10-10 18:24:38 -0700 | [diff] [blame] | 114 | sleep(10000); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 115 | } |
| 116 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 117 | public static void stop() { |
| 118 | try { |
| 119 | messaging.deactivate(); |
| 120 | metrics.deactivate(); |
| 121 | } catch (Exception e) { |
| 122 | log.info("Unable to stop client %s", e); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * The type Test netty messaging service. |
| 128 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 129 | public static class TestNettyMessagingService extends NettyMessagingService { |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Instantiates a new Test netty messaging service. |
| 132 | * |
| 133 | * @param port the port |
| 134 | * @throws Exception the exception |
| 135 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 136 | public TestNettyMessagingService(int port) throws Exception { |
| 137 | super(port); |
| 138 | } |
| 139 | } |
| 140 | } |