Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 16 | package org.onlab.onos.foo; |
| 17 | |
pankaj | 6245a7a | 2014-10-10 17:50:55 -0700 | [diff] [blame] | 18 | import static java.lang.Thread.sleep; |
| 19 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 20 | import java.io.IOException; |
| 21 | import java.util.concurrent.ExecutionException; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 22 | import java.util.concurrent.Future; |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 23 | import java.util.concurrent.TimeUnit; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 24 | import java.util.concurrent.TimeoutException; |
| 25 | |
| 26 | import org.onlab.metrics.MetricsComponent; |
| 27 | import org.onlab.metrics.MetricsFeature; |
| 28 | import org.onlab.metrics.MetricsManager; |
| 29 | import org.onlab.netty.Endpoint; |
| 30 | import org.onlab.netty.NettyMessagingService; |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 33 | |
| 34 | import com.codahale.metrics.Timer; |
| 35 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 36 | /** |
| 37 | * The Simple netty client test. |
| 38 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 39 | // FIXME: Should be move out to test or app |
| 40 | public final class SimpleNettyClient { |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 41 | |
| 42 | private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
| 43 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 44 | static NettyMessagingService messaging; |
| 45 | static MetricsManager metrics; |
| 46 | |
pankaj | b847eae | 2014-10-08 14:39:25 -0700 | [diff] [blame] | 47 | private SimpleNettyClient() { |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 48 | } |
| 49 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 50 | /** |
| 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 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 59 | 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 | } |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Start standalone. |
| 73 | * |
| 74 | * @param args the args |
| 75 | * @throws Exception the exception |
| 76 | */ |
pankaj | 31b8eab | 2014-10-08 18:14:08 -0700 | [diff] [blame] | 77 | public static void startStandalone(String[] args) throws Exception { |
pankaj | 13373b5 | 2014-10-07 18:26:07 -0700 | [diff] [blame] | 78 | 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; |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 82 | messaging = new TestNettyMessagingService(9081); |
| 83 | metrics = new MetricsManager(); |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 84 | Endpoint endpoint = new Endpoint(host, port); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 85 | messaging.activate(); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 86 | MetricsFeature feature = new MetricsFeature("latency"); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 87 | MetricsComponent component = metrics.registerComponent("NettyMessaging"); |
pankaj | 31b8eab | 2014-10-08 18:14:08 -0700 | [diff] [blame] | 88 | log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 89 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 90 | for (int i = 0; i < warmup; i++) { |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 91 | messaging.sendAsync(endpoint, "simple", "Hello World".getBytes()); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 92 | Future<byte[]> responseFuture = messaging |
pankaj | 28c3564 | 2014-10-08 16:42:36 -0700 | [diff] [blame] | 93 | .sendAndReceive(endpoint, "echo", |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 94 | "Hello World".getBytes()); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 95 | responseFuture.get(100000, TimeUnit.MILLISECONDS); |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 96 | } |
| 97 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 98 | log.info("measuring round-trip send & receive"); |
pankaj | f0f80b2 | 2014-10-07 18:37:32 -0700 | [diff] [blame] | 99 | Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 100 | int timeouts = 0; |
| 101 | |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 102 | for (int i = 0; i < iterations; i++) { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 103 | Future<byte[]> responseFuture; |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 104 | Timer.Context context = sendAndReceiveTimer.time(); |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 105 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 106 | responseFuture = messaging |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 107 | .sendAndReceive(endpoint, "echo", |
| 108 | "Hello World".getBytes()); |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 109 | responseFuture.get(10000, TimeUnit.MILLISECONDS); |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 110 | } catch (TimeoutException e) { |
| 111 | timeouts++; |
| 112 | log.info("timeout:" + timeouts + " at iteration:" + i); |
| 113 | } finally { |
| 114 | context.stop(); |
| 115 | } |
pankaj | 366ce8b | 2014-10-07 17:18:37 -0700 | [diff] [blame] | 116 | // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 117 | } |
pankaj | 6245a7a | 2014-10-10 17:50:55 -0700 | [diff] [blame] | 118 | |
pankaj | 16c9a22 | 2014-10-10 18:24:38 -0700 | [diff] [blame] | 119 | //sleep(1000); |
pankaj | 6245a7a | 2014-10-10 17:50:55 -0700 | [diff] [blame] | 120 | 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 | } |
pankaj | 16c9a22 | 2014-10-10 18:24:38 -0700 | [diff] [blame] | 128 | sleep(10000); |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 129 | } |
| 130 | |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 131 | public static void stop() { |
| 132 | try { |
| 133 | messaging.deactivate(); |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 134 | } catch (Exception e) { |
| 135 | log.info("Unable to stop client %s", e); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * The type Test netty messaging service. |
| 141 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 142 | public static class TestNettyMessagingService extends NettyMessagingService { |
pankaj | 9fc8759 | 2014-10-10 15:40:43 -0700 | [diff] [blame] | 143 | /** |
| 144 | * Instantiates a new Test netty messaging service. |
| 145 | * |
| 146 | * @param port the port |
| 147 | * @throws Exception the exception |
| 148 | */ |
pankaj | f49b45e | 2014-10-07 14:24:22 -0700 | [diff] [blame] | 149 | public TestNettyMessagingService(int port) throws Exception { |
| 150 | super(port); |
| 151 | } |
| 152 | } |
| 153 | } |