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