blob: cff9f3b0c1bafce8617ff84198e13e28cfa761cf [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
pankajf49b45e2014-10-07 14:24:22 -070016package org.onlab.onos.foo;
17
pankaj6245a7a2014-10-10 17:50:55 -070018import static java.lang.Thread.sleep;
19
pankajf49b45e2014-10-07 14:24:22 -070020import java.io.IOException;
21import java.util.concurrent.ExecutionException;
Madan Jampani24f9efb2014-10-24 18:56:23 -070022import java.util.concurrent.Future;
pankaj9fc87592014-10-10 15:40:43 -070023import java.util.concurrent.TimeUnit;
pankajf49b45e2014-10-07 14:24:22 -070024import java.util.concurrent.TimeoutException;
25
26import org.onlab.metrics.MetricsComponent;
27import org.onlab.metrics.MetricsFeature;
28import org.onlab.metrics.MetricsManager;
29import org.onlab.netty.Endpoint;
30import org.onlab.netty.NettyMessagingService;
pankajb847eae2014-10-08 14:39:25 -070031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
pankajf49b45e2014-10-07 14:24:22 -070033
34import com.codahale.metrics.Timer;
35
pankaj9fc87592014-10-10 15:40:43 -070036/**
37 * The Simple netty client test.
38 */
pankajf49b45e2014-10-07 14:24:22 -070039// FIXME: Should be move out to test or app
40public final class SimpleNettyClient {
pankajb847eae2014-10-08 14:39:25 -070041
42private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class);
43
pankaj9fc87592014-10-10 15:40:43 -070044 static NettyMessagingService messaging;
45 static MetricsManager metrics;
46
pankajb847eae2014-10-08 14:39:25 -070047 private SimpleNettyClient() {
pankajf49b45e2014-10-07 14:24:22 -070048 }
49
pankaj9fc87592014-10-10 15:40:43 -070050 /**
51 * The entry point of application.
52 *
53 * @param args the input arguments
54 * @throws IOException the iO exception
55 * @throws InterruptedException the interrupted exception
56 * @throws ExecutionException the execution exception
57 * @throws TimeoutException the timeout exception
58 */
pankajf49b45e2014-10-07 14:24:22 -070059 public static void main(String[] args)
60 throws IOException, InterruptedException, ExecutionException,
61 TimeoutException {
62 try {
63 startStandalone(args);
64 } catch (Exception e) {
65 e.printStackTrace();
66 }
67
68 System.exit(0);
69 }
pankaj9fc87592014-10-10 15:40:43 -070070
71 /**
72 * Start standalone.
73 *
74 * @param args the args
75 * @throws Exception the exception
76 */
pankaj31b8eab2014-10-08 18:14:08 -070077 public static void startStandalone(String[] args) throws Exception {
pankaj13373b52014-10-07 18:26:07 -070078 String host = args.length > 0 ? args[0] : "localhost";
79 int port = args.length > 1 ? Integer.parseInt(args[1]) : 8081;
80 int warmup = args.length > 2 ? Integer.parseInt(args[2]) : 1000;
81 int iterations = args.length > 3 ? Integer.parseInt(args[3]) : 50 * 100000;
pankaj9fc87592014-10-10 15:40:43 -070082 messaging = new TestNettyMessagingService(9081);
83 metrics = new MetricsManager();
pankaj28c35642014-10-08 16:42:36 -070084 Endpoint endpoint = new Endpoint(host, port);
pankajf49b45e2014-10-07 14:24:22 -070085 messaging.activate();
pankaj366ce8b2014-10-07 17:18:37 -070086 MetricsFeature feature = new MetricsFeature("latency");
pankajf49b45e2014-10-07 14:24:22 -070087 MetricsComponent component = metrics.registerComponent("NettyMessaging");
pankaj31b8eab2014-10-08 18:14:08 -070088 log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations);
pankaj366ce8b2014-10-07 17:18:37 -070089
pankajf49b45e2014-10-07 14:24:22 -070090 for (int i = 0; i < warmup; i++) {
pankaj28c35642014-10-08 16:42:36 -070091 messaging.sendAsync(endpoint, "simple", "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -070092 Future<byte[]> responseFuture = messaging
pankaj28c35642014-10-08 16:42:36 -070093 .sendAndReceive(endpoint, "echo",
pankaj366ce8b2014-10-07 17:18:37 -070094 "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -070095 responseFuture.get(100000, TimeUnit.MILLISECONDS);
pankaj366ce8b2014-10-07 17:18:37 -070096 }
97
pankaj9fc87592014-10-10 15:40:43 -070098 log.info("measuring round-trip send & receive");
pankajf0f80b22014-10-07 18:37:32 -070099 Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive");
pankaj9fc87592014-10-10 15:40:43 -0700100 int timeouts = 0;
101
pankajf49b45e2014-10-07 14:24:22 -0700102 for (int i = 0; i < iterations; i++) {
Madan Jampani24f9efb2014-10-24 18:56:23 -0700103 Future<byte[]> responseFuture;
pankajf49b45e2014-10-07 14:24:22 -0700104 Timer.Context context = sendAndReceiveTimer.time();
pankaj9fc87592014-10-10 15:40:43 -0700105 try {
Madan Jampani24f9efb2014-10-24 18:56:23 -0700106 responseFuture = messaging
pankaj9fc87592014-10-10 15:40:43 -0700107 .sendAndReceive(endpoint, "echo",
108 "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -0700109 responseFuture.get(10000, TimeUnit.MILLISECONDS);
pankaj9fc87592014-10-10 15:40:43 -0700110 } catch (TimeoutException e) {
111 timeouts++;
112 log.info("timeout:" + timeouts + " at iteration:" + i);
113 } finally {
114 context.stop();
115 }
pankaj366ce8b2014-10-07 17:18:37 -0700116 // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS)));
pankajf49b45e2014-10-07 14:24:22 -0700117 }
pankaj6245a7a2014-10-10 17:50:55 -0700118
pankaj16c9a222014-10-10 18:24:38 -0700119 //sleep(1000);
pankaj6245a7a2014-10-10 17:50:55 -0700120 log.info("measuring async sender");
121 Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender");
122
123 for (int i = 0; i < iterations; i++) {
124 Timer.Context context = sendAsyncTimer.time();
125 messaging.sendAsync(endpoint, "simple", "Hello World".getBytes());
126 context.stop();
127 }
pankaj16c9a222014-10-10 18:24:38 -0700128 sleep(10000);
pankajf49b45e2014-10-07 14:24:22 -0700129 }
130
pankaj9fc87592014-10-10 15:40:43 -0700131 public static void stop() {
132 try {
133 messaging.deactivate();
pankaj9fc87592014-10-10 15:40:43 -0700134 } catch (Exception e) {
135 log.info("Unable to stop client %s", e);
136 }
137 }
138
139 /**
140 * The type Test netty messaging service.
141 */
pankajf49b45e2014-10-07 14:24:22 -0700142 public static class TestNettyMessagingService extends NettyMessagingService {
pankaj9fc87592014-10-10 15:40:43 -0700143 /**
144 * Instantiates a new Test netty messaging service.
145 *
146 * @param port the port
147 * @throws Exception the exception
148 */
pankajf49b45e2014-10-07 14:24:22 -0700149 public TestNettyMessagingService(int port) throws Exception {
150 super(port);
151 }
152 }
153}