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; |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 15 | |
| 16 | import com.codahale.metrics.Timer; |
| 17 | |
| 18 | // FIXME: Should be move out to test or app |
| 19 | public final class SimpleNettyClient { |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 20 | |
| 21 | private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
| 22 | |
| 23 | private SimpleNettyClient() { |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | public static void main(String[] args) |
| 27 | throws IOException, InterruptedException, ExecutionException, |
| 28 | TimeoutException { |
| 29 | try { |
| 30 | startStandalone(args); |
| 31 | } catch (Exception e) { |
| 32 | e.printStackTrace(); |
| 33 | } |
| 34 | |
| 35 | System.exit(0); |
| 36 | } |
| 37 | public static void startStandalone(String... args) throws Exception { |
pankaj | 13373b5 | 2014-10-07 18:26:07 -0700 | [diff] [blame] | 38 | String host = args.length > 0 ? args[0] : "localhost"; |
| 39 | int port = args.length > 1 ? Integer.parseInt(args[1]) : 8081; |
| 40 | int warmup = args.length > 2 ? Integer.parseInt(args[2]) : 1000; |
| 41 | int iterations = args.length > 3 ? Integer.parseInt(args[3]) : 50 * 100000; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 42 | NettyMessagingService messaging = new TestNettyMessagingService(9081); |
| 43 | MetricsManager metrics = new MetricsManager(); |
| 44 | messaging.activate(); |
| 45 | metrics.activate(); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 46 | MetricsFeature feature = new MetricsFeature("latency"); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 47 | MetricsComponent component = metrics.registerComponent("NettyMessaging"); |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 48 | log.info("warmup...."); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 49 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 50 | for (int i = 0; i < warmup; i++) { |
pankaj | 13373b5 | 2014-10-07 18:26:07 -0700 | [diff] [blame] | 51 | messaging.sendAsync(new Endpoint(host, port), "simple", "Hello World".getBytes()); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 52 | Response response = messaging |
pankaj | 13373b5 | 2014-10-07 18:26:07 -0700 | [diff] [blame] | 53 | .sendAndReceive(new Endpoint(host, port), "echo", |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 54 | "Hello World".getBytes()); |
| 55 | } |
| 56 | |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 57 | log.info("measuring async sender"); |
pankaj | 4face40 | 2014-10-07 18:05:52 -0700 | [diff] [blame] | 58 | Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender"); |
pankaj | 4face40 | 2014-10-07 18:05:52 -0700 | [diff] [blame] | 59 | |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 60 | for (int i = 0; i < iterations; i++) { |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 61 | Timer.Context context = sendAsyncTimer.time(); |
pankaj | 13373b5 | 2014-10-07 18:26:07 -0700 | [diff] [blame] | 62 | messaging.sendAsync(new Endpoint(host, port), "simple", "Hello World".getBytes()); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 63 | context.stop(); |
| 64 | } |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 65 | |
pankaj | f0f80b2 | 2014-10-07 18:37:32 -0700 | [diff] [blame] | 66 | Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 67 | for (int i = 0; i < iterations; i++) { |
| 68 | Timer.Context context = sendAndReceiveTimer.time(); |
| 69 | Response response = messaging |
pankaj | 13373b5 | 2014-10-07 18:26:07 -0700 | [diff] [blame] | 70 | .sendAndReceive(new Endpoint(host, port), "echo", |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 71 | "Hello World".getBytes()); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 72 | // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 73 | context.stop(); |
| 74 | } |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public static class TestNettyMessagingService extends NettyMessagingService { |
| 78 | public TestNettyMessagingService(int port) throws Exception { |
| 79 | super(port); |
| 80 | } |
| 81 | } |
| 82 | } |