blob: b2250e1b708336a0b46dd626c2e22c68e284960e [file] [log] [blame]
pankajf49b45e2014-10-07 14:24:22 -07001package org.onlab.onos.foo;
2
pankaj6245a7a2014-10-10 17:50:55 -07003import static java.lang.Thread.sleep;
4
pankajf49b45e2014-10-07 14:24:22 -07005import java.io.IOException;
6import java.util.concurrent.ExecutionException;
pankaj9fc87592014-10-10 15:40:43 -07007import java.util.concurrent.TimeUnit;
pankajf49b45e2014-10-07 14:24:22 -07008import java.util.concurrent.TimeoutException;
9
10import org.onlab.metrics.MetricsComponent;
11import org.onlab.metrics.MetricsFeature;
12import org.onlab.metrics.MetricsManager;
13import org.onlab.netty.Endpoint;
14import org.onlab.netty.NettyMessagingService;
15import org.onlab.netty.Response;
pankajb847eae2014-10-08 14:39:25 -070016import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
pankajf49b45e2014-10-07 14:24:22 -070018
19import com.codahale.metrics.Timer;
20
pankaj9fc87592014-10-10 15:40:43 -070021/**
22 * The Simple netty client test.
23 */
pankajf49b45e2014-10-07 14:24:22 -070024// FIXME: Should be move out to test or app
25public final class SimpleNettyClient {
pankajb847eae2014-10-08 14:39:25 -070026
27private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class);
28
pankaj9fc87592014-10-10 15:40:43 -070029 static NettyMessagingService messaging;
30 static MetricsManager metrics;
31
pankajb847eae2014-10-08 14:39:25 -070032 private SimpleNettyClient() {
pankajf49b45e2014-10-07 14:24:22 -070033 }
34
pankaj9fc87592014-10-10 15:40:43 -070035 /**
36 * The entry point of application.
37 *
38 * @param args the input arguments
39 * @throws IOException the iO exception
40 * @throws InterruptedException the interrupted exception
41 * @throws ExecutionException the execution exception
42 * @throws TimeoutException the timeout exception
43 */
pankajf49b45e2014-10-07 14:24:22 -070044 public static void main(String[] args)
45 throws IOException, InterruptedException, ExecutionException,
46 TimeoutException {
47 try {
48 startStandalone(args);
49 } catch (Exception e) {
50 e.printStackTrace();
51 }
52
53 System.exit(0);
54 }
pankaj9fc87592014-10-10 15:40:43 -070055
56 /**
57 * Start standalone.
58 *
59 * @param args the args
60 * @throws Exception the exception
61 */
pankaj31b8eab2014-10-08 18:14:08 -070062 public static void startStandalone(String[] args) throws Exception {
pankaj13373b52014-10-07 18:26:07 -070063 String host = args.length > 0 ? args[0] : "localhost";
64 int port = args.length > 1 ? Integer.parseInt(args[1]) : 8081;
65 int warmup = args.length > 2 ? Integer.parseInt(args[2]) : 1000;
66 int iterations = args.length > 3 ? Integer.parseInt(args[3]) : 50 * 100000;
pankaj9fc87592014-10-10 15:40:43 -070067 messaging = new TestNettyMessagingService(9081);
68 metrics = new MetricsManager();
pankaj28c35642014-10-08 16:42:36 -070069 Endpoint endpoint = new Endpoint(host, port);
pankajf49b45e2014-10-07 14:24:22 -070070 messaging.activate();
71 metrics.activate();
pankaj366ce8b2014-10-07 17:18:37 -070072 MetricsFeature feature = new MetricsFeature("latency");
pankajf49b45e2014-10-07 14:24:22 -070073 MetricsComponent component = metrics.registerComponent("NettyMessaging");
pankaj31b8eab2014-10-08 18:14:08 -070074 log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations);
pankaj366ce8b2014-10-07 17:18:37 -070075
pankajf49b45e2014-10-07 14:24:22 -070076 for (int i = 0; i < warmup; i++) {
pankaj28c35642014-10-08 16:42:36 -070077 messaging.sendAsync(endpoint, "simple", "Hello World".getBytes());
pankaj366ce8b2014-10-07 17:18:37 -070078 Response response = messaging
pankaj28c35642014-10-08 16:42:36 -070079 .sendAndReceive(endpoint, "echo",
pankaj366ce8b2014-10-07 17:18:37 -070080 "Hello World".getBytes());
pankaj9fc87592014-10-10 15:40:43 -070081 response.get(100000, TimeUnit.MILLISECONDS);
pankaj366ce8b2014-10-07 17:18:37 -070082 }
83
pankaj9fc87592014-10-10 15:40:43 -070084 log.info("measuring round-trip send & receive");
pankajf0f80b22014-10-07 18:37:32 -070085 Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive");
pankaj9fc87592014-10-10 15:40:43 -070086 int timeouts = 0;
87
pankajf49b45e2014-10-07 14:24:22 -070088 for (int i = 0; i < iterations; i++) {
pankaj9fc87592014-10-10 15:40:43 -070089 Response response;
pankajf49b45e2014-10-07 14:24:22 -070090 Timer.Context context = sendAndReceiveTimer.time();
pankaj9fc87592014-10-10 15:40:43 -070091 try {
92 response = messaging
93 .sendAndReceive(endpoint, "echo",
94 "Hello World".getBytes());
95 response.get(10000, TimeUnit.MILLISECONDS);
96 } catch (TimeoutException e) {
97 timeouts++;
98 log.info("timeout:" + timeouts + " at iteration:" + i);
99 } finally {
100 context.stop();
101 }
pankaj366ce8b2014-10-07 17:18:37 -0700102 // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS)));
pankajf49b45e2014-10-07 14:24:22 -0700103 }
pankaj6245a7a2014-10-10 17:50:55 -0700104
pankaj16c9a222014-10-10 18:24:38 -0700105 //sleep(1000);
pankaj6245a7a2014-10-10 17:50:55 -0700106 log.info("measuring async sender");
107 Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender");
108
109 for (int i = 0; i < iterations; i++) {
110 Timer.Context context = sendAsyncTimer.time();
111 messaging.sendAsync(endpoint, "simple", "Hello World".getBytes());
112 context.stop();
113 }
pankaj16c9a222014-10-10 18:24:38 -0700114 sleep(10000);
pankajf49b45e2014-10-07 14:24:22 -0700115 }
116
pankaj9fc87592014-10-10 15:40:43 -0700117 public static void stop() {
118 try {
119 messaging.deactivate();
120 metrics.deactivate();
121 } catch (Exception e) {
122 log.info("Unable to stop client %s", e);
123 }
124 }
125
126 /**
127 * The type Test netty messaging service.
128 */
pankajf49b45e2014-10-07 14:24:22 -0700129 public static class TestNettyMessagingService extends NettyMessagingService {
pankaj9fc87592014-10-10 15:40:43 -0700130 /**
131 * Instantiates a new Test netty messaging service.
132 *
133 * @param port the port
134 * @throws Exception the exception
135 */
pankajf49b45e2014-10-07 14:24:22 -0700136 public TestNettyMessagingService(int port) throws Exception {
137 super(port);
138 }
139 }
140}