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