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(); |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 44 | Endpoint endpoint = new Endpoint(host, port); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 45 | messaging.activate(); |
| 46 | metrics.activate(); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 47 | MetricsFeature feature = new MetricsFeature("latency"); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 48 | MetricsComponent component = metrics.registerComponent("NettyMessaging"); |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 49 | log.info("warmup...."); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 50 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 51 | for (int i = 0; i < warmup; i++) { |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 52 | messaging.sendAsync(endpoint, "simple", "Hello World".getBytes()); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 53 | Response response = messaging |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 54 | .sendAndReceive(endpoint, "echo", |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 55 | "Hello World".getBytes()); |
| 56 | } |
| 57 | |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 58 | log.info("measuring async sender"); |
pankaj | 4face40 | 2014-10-07 18:05:52 -0700 | [diff] [blame] | 59 | Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender"); |
pankaj | 4face40 | 2014-10-07 18:05:52 -0700 | [diff] [blame] | 60 | |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 61 | for (int i = 0; i < iterations; i++) { |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 62 | Timer.Context context = sendAsyncTimer.time(); |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 63 | messaging.sendAsync(endpoint, "simple", "Hello World".getBytes()); |
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 | |
pankaj | f0f80b2 | 2014-10-07 18:37:32 -0700 | [diff] [blame] | 67 | Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 68 | for (int i = 0; i < iterations; i++) { |
| 69 | Timer.Context context = sendAndReceiveTimer.time(); |
| 70 | Response response = messaging |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 71 | .sendAndReceive(endpoint, "echo", |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 72 | "Hello World".getBytes()); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 73 | // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 74 | context.stop(); |
| 75 | } |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public static class TestNettyMessagingService extends NettyMessagingService { |
| 79 | public TestNettyMessagingService(int port) throws Exception { |
| 80 | super(port); |
| 81 | } |
| 82 | } |
| 83 | } |