blob: a14c568395cad81c2fdd2a3be823d2d3caee34ec [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;
Madan Jampani24f9efb2014-10-24 18:56:23 -07007import java.util.concurrent.Future;
pankaj9fc87592014-10-10 15:40:43 -07008import java.util.concurrent.TimeUnit;
pankajf49b45e2014-10-07 14:24:22 -07009import java.util.concurrent.TimeoutException;
10
11import org.onlab.metrics.MetricsComponent;
12import org.onlab.metrics.MetricsFeature;
13import org.onlab.metrics.MetricsManager;
14import org.onlab.netty.Endpoint;
15import org.onlab.netty.NettyMessagingService;
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();
pankaj366ce8b2014-10-07 17:18:37 -070071 MetricsFeature feature = new MetricsFeature("latency");
pankajf49b45e2014-10-07 14:24:22 -070072 MetricsComponent component = metrics.registerComponent("NettyMessaging");
pankaj31b8eab2014-10-08 18:14:08 -070073 log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations);
pankaj366ce8b2014-10-07 17:18:37 -070074
pankajf49b45e2014-10-07 14:24:22 -070075 for (int i = 0; i < warmup; i++) {
pankaj28c35642014-10-08 16:42:36 -070076 messaging.sendAsync(endpoint, "simple", "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -070077 Future<byte[]> responseFuture = messaging
pankaj28c35642014-10-08 16:42:36 -070078 .sendAndReceive(endpoint, "echo",
pankaj366ce8b2014-10-07 17:18:37 -070079 "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -070080 responseFuture.get(100000, TimeUnit.MILLISECONDS);
pankaj366ce8b2014-10-07 17:18:37 -070081 }
82
pankaj9fc87592014-10-10 15:40:43 -070083 log.info("measuring round-trip send & receive");
pankajf0f80b22014-10-07 18:37:32 -070084 Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive");
pankaj9fc87592014-10-10 15:40:43 -070085 int timeouts = 0;
86
pankajf49b45e2014-10-07 14:24:22 -070087 for (int i = 0; i < iterations; i++) {
Madan Jampani24f9efb2014-10-24 18:56:23 -070088 Future<byte[]> responseFuture;
pankajf49b45e2014-10-07 14:24:22 -070089 Timer.Context context = sendAndReceiveTimer.time();
pankaj9fc87592014-10-10 15:40:43 -070090 try {
Madan Jampani24f9efb2014-10-24 18:56:23 -070091 responseFuture = messaging
pankaj9fc87592014-10-10 15:40:43 -070092 .sendAndReceive(endpoint, "echo",
93 "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -070094 responseFuture.get(10000, TimeUnit.MILLISECONDS);
pankaj9fc87592014-10-10 15:40:43 -070095 } catch (TimeoutException e) {
96 timeouts++;
97 log.info("timeout:" + timeouts + " at iteration:" + i);
98 } finally {
99 context.stop();
100 }
pankaj366ce8b2014-10-07 17:18:37 -0700101 // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS)));
pankajf49b45e2014-10-07 14:24:22 -0700102 }
pankaj6245a7a2014-10-10 17:50:55 -0700103
pankaj16c9a222014-10-10 18:24:38 -0700104 //sleep(1000);
pankaj6245a7a2014-10-10 17:50:55 -0700105 log.info("measuring async sender");
106 Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender");
107
108 for (int i = 0; i < iterations; i++) {
109 Timer.Context context = sendAsyncTimer.time();
110 messaging.sendAsync(endpoint, "simple", "Hello World".getBytes());
111 context.stop();
112 }
pankaj16c9a222014-10-10 18:24:38 -0700113 sleep(10000);
pankajf49b45e2014-10-07 14:24:22 -0700114 }
115
pankaj9fc87592014-10-10 15:40:43 -0700116 public static void stop() {
117 try {
118 messaging.deactivate();
pankaj9fc87592014-10-10 15:40:43 -0700119 } catch (Exception e) {
120 log.info("Unable to stop client %s", e);
121 }
122 }
123
124 /**
125 * The type Test netty messaging service.
126 */
pankajf49b45e2014-10-07 14:24:22 -0700127 public static class TestNettyMessagingService extends NettyMessagingService {
pankaj9fc87592014-10-10 15:40:43 -0700128 /**
129 * Instantiates a new Test netty messaging service.
130 *
131 * @param port the port
132 * @throws Exception the exception
133 */
pankajf49b45e2014-10-07 14:24:22 -0700134 public TestNettyMessagingService(int port) throws Exception {
135 super(port);
136 }
137 }
138}