pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 1 | package org.onlab.onos.foo; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.concurrent.ExecutionException; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 5 | import java.util.concurrent.TimeoutException; |
| 6 | |
| 7 | import org.onlab.metrics.MetricsComponent; |
| 8 | import org.onlab.metrics.MetricsFeature; |
| 9 | import org.onlab.metrics.MetricsManager; |
| 10 | import org.onlab.netty.Endpoint; |
| 11 | import org.onlab.netty.NettyMessagingService; |
| 12 | import org.onlab.netty.Response; |
| 13 | |
| 14 | import com.codahale.metrics.Timer; |
| 15 | |
| 16 | // FIXME: Should be move out to test or app |
| 17 | public final class SimpleNettyClient { |
| 18 | private SimpleNettyClient() { |
| 19 | } |
| 20 | |
| 21 | public static void main(String[] args) |
| 22 | throws IOException, InterruptedException, ExecutionException, |
| 23 | TimeoutException { |
| 24 | try { |
| 25 | startStandalone(args); |
| 26 | } catch (Exception e) { |
| 27 | e.printStackTrace(); |
| 28 | } |
| 29 | |
| 30 | System.exit(0); |
| 31 | } |
| 32 | public static void startStandalone(String... args) throws Exception { |
| 33 | NettyMessagingService messaging = new TestNettyMessagingService(9081); |
| 34 | MetricsManager metrics = new MetricsManager(); |
| 35 | messaging.activate(); |
| 36 | metrics.activate(); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame^] | 37 | MetricsFeature feature = new MetricsFeature("latency"); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 38 | MetricsComponent component = metrics.registerComponent("NettyMessaging"); |
| 39 | Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender"); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame^] | 40 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 41 | final int warmup = 100; |
| 42 | for (int i = 0; i < warmup; i++) { |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame^] | 43 | messaging.sendAsync(new Endpoint("localhost", 8081), "simple", "Hello World".getBytes()); |
| 44 | Response response = messaging |
| 45 | .sendAndReceive(new Endpoint("localhost", 8081), "echo", |
| 46 | "Hello World".getBytes()); |
| 47 | } |
| 48 | |
| 49 | final int iterations = 10000; |
| 50 | for (int i = 0; i < iterations; i++) { |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 51 | Timer.Context context = sendAsyncTimer.time(); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame^] | 52 | messaging.sendAsync(new Endpoint("localhost", 8081), "simple", "Hello World".getBytes()); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 53 | context.stop(); |
| 54 | } |
| 55 | metrics.registerMetric(component, feature, "AsyncTimer", sendAsyncTimer); |
| 56 | |
| 57 | Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame^] | 58 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 59 | for (int i = 0; i < iterations; i++) { |
| 60 | Timer.Context context = sendAndReceiveTimer.time(); |
| 61 | Response response = messaging |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame^] | 62 | .sendAndReceive(new Endpoint("localhost", 8081), "echo", |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 63 | "Hello World".getBytes()); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame^] | 64 | // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 65 | context.stop(); |
| 66 | } |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame^] | 67 | metrics.registerMetric(component, feature, "PingPong", sendAndReceiveTimer); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | public static class TestNettyMessagingService extends NettyMessagingService { |
| 71 | public TestNettyMessagingService(int port) throws Exception { |
| 72 | super(port); |
| 73 | } |
| 74 | } |
| 75 | } |