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"); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 39 | |
pankaj | 4face40 | 2014-10-07 18:05:52 -0700 | [diff] [blame] | 40 | final int warmup = 10000; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 41 | for (int i = 0; i < warmup; i++) { |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 42 | messaging.sendAsync(new Endpoint("localhost", 8081), "simple", "Hello World".getBytes()); |
| 43 | Response response = messaging |
| 44 | .sendAndReceive(new Endpoint("localhost", 8081), "echo", |
| 45 | "Hello World".getBytes()); |
| 46 | } |
| 47 | |
pankaj | 4face40 | 2014-10-07 18:05:52 -0700 | [diff] [blame] | 48 | Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender"); |
| 49 | Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); |
| 50 | |
| 51 | final int iterations = 10000000; |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 52 | for (int i = 0; i < iterations; i++) { |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 53 | Timer.Context context = sendAsyncTimer.time(); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 54 | messaging.sendAsync(new Endpoint("localhost", 8081), "simple", "Hello World".getBytes()); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 55 | context.stop(); |
| 56 | } |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 57 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 58 | for (int i = 0; i < iterations; i++) { |
| 59 | Timer.Context context = sendAndReceiveTimer.time(); |
| 60 | Response response = messaging |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 61 | .sendAndReceive(new Endpoint("localhost", 8081), "echo", |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 62 | "Hello World".getBytes()); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 63 | // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 64 | context.stop(); |
| 65 | } |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | public static class TestNettyMessagingService extends NettyMessagingService { |
| 69 | public TestNettyMessagingService(int port) throws Exception { |
| 70 | super(port); |
| 71 | } |
| 72 | } |
| 73 | } |