blob: 1178c9d4ea135e4d8024ae140f8b6fa2b8ea1ecd [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
pankajf49b45e2014-10-07 14:24:22 -070019package org.onlab.onos.foo;
20
pankaj6245a7a2014-10-10 17:50:55 -070021import static java.lang.Thread.sleep;
22
pankajf49b45e2014-10-07 14:24:22 -070023import java.io.IOException;
24import java.util.concurrent.ExecutionException;
Madan Jampani24f9efb2014-10-24 18:56:23 -070025import java.util.concurrent.Future;
pankaj9fc87592014-10-10 15:40:43 -070026import java.util.concurrent.TimeUnit;
pankajf49b45e2014-10-07 14:24:22 -070027import java.util.concurrent.TimeoutException;
28
29import org.onlab.metrics.MetricsComponent;
30import org.onlab.metrics.MetricsFeature;
31import org.onlab.metrics.MetricsManager;
32import org.onlab.netty.Endpoint;
33import org.onlab.netty.NettyMessagingService;
pankajb847eae2014-10-08 14:39:25 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
pankajf49b45e2014-10-07 14:24:22 -070036
37import com.codahale.metrics.Timer;
38
pankaj9fc87592014-10-10 15:40:43 -070039/**
40 * The Simple netty client test.
41 */
pankajf49b45e2014-10-07 14:24:22 -070042// FIXME: Should be move out to test or app
43public final class SimpleNettyClient {
pankajb847eae2014-10-08 14:39:25 -070044
45private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class);
46
pankaj9fc87592014-10-10 15:40:43 -070047 static NettyMessagingService messaging;
48 static MetricsManager metrics;
49
pankajb847eae2014-10-08 14:39:25 -070050 private SimpleNettyClient() {
pankajf49b45e2014-10-07 14:24:22 -070051 }
52
pankaj9fc87592014-10-10 15:40:43 -070053 /**
54 * The entry point of application.
55 *
56 * @param args the input arguments
57 * @throws IOException the iO exception
58 * @throws InterruptedException the interrupted exception
59 * @throws ExecutionException the execution exception
60 * @throws TimeoutException the timeout exception
61 */
pankajf49b45e2014-10-07 14:24:22 -070062 public static void main(String[] args)
63 throws IOException, InterruptedException, ExecutionException,
64 TimeoutException {
65 try {
66 startStandalone(args);
67 } catch (Exception e) {
68 e.printStackTrace();
69 }
70
71 System.exit(0);
72 }
pankaj9fc87592014-10-10 15:40:43 -070073
74 /**
75 * Start standalone.
76 *
77 * @param args the args
78 * @throws Exception the exception
79 */
pankaj31b8eab2014-10-08 18:14:08 -070080 public static void startStandalone(String[] args) throws Exception {
pankaj13373b52014-10-07 18:26:07 -070081 String host = args.length > 0 ? args[0] : "localhost";
82 int port = args.length > 1 ? Integer.parseInt(args[1]) : 8081;
83 int warmup = args.length > 2 ? Integer.parseInt(args[2]) : 1000;
84 int iterations = args.length > 3 ? Integer.parseInt(args[3]) : 50 * 100000;
pankaj9fc87592014-10-10 15:40:43 -070085 messaging = new TestNettyMessagingService(9081);
86 metrics = new MetricsManager();
pankaj28c35642014-10-08 16:42:36 -070087 Endpoint endpoint = new Endpoint(host, port);
pankajf49b45e2014-10-07 14:24:22 -070088 messaging.activate();
pankaj366ce8b2014-10-07 17:18:37 -070089 MetricsFeature feature = new MetricsFeature("latency");
pankajf49b45e2014-10-07 14:24:22 -070090 MetricsComponent component = metrics.registerComponent("NettyMessaging");
pankaj31b8eab2014-10-08 18:14:08 -070091 log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations);
pankaj366ce8b2014-10-07 17:18:37 -070092
pankajf49b45e2014-10-07 14:24:22 -070093 for (int i = 0; i < warmup; i++) {
pankaj28c35642014-10-08 16:42:36 -070094 messaging.sendAsync(endpoint, "simple", "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -070095 Future<byte[]> responseFuture = messaging
pankaj28c35642014-10-08 16:42:36 -070096 .sendAndReceive(endpoint, "echo",
pankaj366ce8b2014-10-07 17:18:37 -070097 "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -070098 responseFuture.get(100000, TimeUnit.MILLISECONDS);
pankaj366ce8b2014-10-07 17:18:37 -070099 }
100
pankaj9fc87592014-10-10 15:40:43 -0700101 log.info("measuring round-trip send & receive");
pankajf0f80b22014-10-07 18:37:32 -0700102 Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive");
pankaj9fc87592014-10-10 15:40:43 -0700103 int timeouts = 0;
104
pankajf49b45e2014-10-07 14:24:22 -0700105 for (int i = 0; i < iterations; i++) {
Madan Jampani24f9efb2014-10-24 18:56:23 -0700106 Future<byte[]> responseFuture;
pankajf49b45e2014-10-07 14:24:22 -0700107 Timer.Context context = sendAndReceiveTimer.time();
pankaj9fc87592014-10-10 15:40:43 -0700108 try {
Madan Jampani24f9efb2014-10-24 18:56:23 -0700109 responseFuture = messaging
pankaj9fc87592014-10-10 15:40:43 -0700110 .sendAndReceive(endpoint, "echo",
111 "Hello World".getBytes());
Madan Jampani24f9efb2014-10-24 18:56:23 -0700112 responseFuture.get(10000, TimeUnit.MILLISECONDS);
pankaj9fc87592014-10-10 15:40:43 -0700113 } catch (TimeoutException e) {
114 timeouts++;
115 log.info("timeout:" + timeouts + " at iteration:" + i);
116 } finally {
117 context.stop();
118 }
pankaj366ce8b2014-10-07 17:18:37 -0700119 // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS)));
pankajf49b45e2014-10-07 14:24:22 -0700120 }
pankaj6245a7a2014-10-10 17:50:55 -0700121
pankaj16c9a222014-10-10 18:24:38 -0700122 //sleep(1000);
pankaj6245a7a2014-10-10 17:50:55 -0700123 log.info("measuring async sender");
124 Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender");
125
126 for (int i = 0; i < iterations; i++) {
127 Timer.Context context = sendAsyncTimer.time();
128 messaging.sendAsync(endpoint, "simple", "Hello World".getBytes());
129 context.stop();
130 }
pankaj16c9a222014-10-10 18:24:38 -0700131 sleep(10000);
pankajf49b45e2014-10-07 14:24:22 -0700132 }
133
pankaj9fc87592014-10-10 15:40:43 -0700134 public static void stop() {
135 try {
136 messaging.deactivate();
pankaj9fc87592014-10-10 15:40:43 -0700137 } catch (Exception e) {
138 log.info("Unable to stop client %s", e);
139 }
140 }
141
142 /**
143 * The type Test netty messaging service.
144 */
pankajf49b45e2014-10-07 14:24:22 -0700145 public static class TestNettyMessagingService extends NettyMessagingService {
pankaj9fc87592014-10-10 15:40:43 -0700146 /**
147 * Instantiates a new Test netty messaging service.
148 *
149 * @param port the port
150 * @throws Exception the exception
151 */
pankajf49b45e2014-10-07 14:24:22 -0700152 public TestNettyMessagingService(int port) throws Exception {
153 super(port);
154 }
155 }
156}