blob: 8f94ec1ef5aac6fbb44c7e695f99ed1369f9bf4c [file] [log] [blame]
pankajf49b45e2014-10-07 14:24:22 -07001package org.onlab.onos.foo;
2
3import java.io.IOException;
4import java.util.concurrent.ExecutionException;
5import java.util.concurrent.TimeUnit;
6import java.util.concurrent.TimeoutException;
7
8import org.onlab.metrics.MetricsComponent;
9import org.onlab.metrics.MetricsFeature;
10import org.onlab.metrics.MetricsManager;
11import org.onlab.netty.Endpoint;
12import org.onlab.netty.NettyMessagingService;
13import org.onlab.netty.Response;
14
15import com.codahale.metrics.Timer;
16
17// FIXME: Should be move out to test or app
18public final class SimpleNettyClient {
19 private SimpleNettyClient() {
20 }
21
22 public static void main(String[] args)
23 throws IOException, InterruptedException, ExecutionException,
24 TimeoutException {
25 try {
26 startStandalone(args);
27 } catch (Exception e) {
28 e.printStackTrace();
29 }
30
31 System.exit(0);
32 }
33 public static void startStandalone(String... args) throws Exception {
34 NettyMessagingService messaging = new TestNettyMessagingService(9081);
35 MetricsManager metrics = new MetricsManager();
36 messaging.activate();
37 metrics.activate();
38 MetricsFeature feature = new MetricsFeature("timers");
39 MetricsComponent component = metrics.registerComponent("NettyMessaging");
40 Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender");
41 final int warmup = 100;
42 for (int i = 0; i < warmup; i++) {
43 Timer.Context context = sendAsyncTimer.time();
44 messaging.sendAsync(new Endpoint("localhost", 8080), "simple", "Hello World".getBytes());
45 context.stop();
46 }
47 metrics.registerMetric(component, feature, "AsyncTimer", sendAsyncTimer);
48
49 Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive");
50 final int iterations = 1000000;
51 for (int i = 0; i < iterations; i++) {
52 Timer.Context context = sendAndReceiveTimer.time();
53 Response response = messaging
54 .sendAndReceive(new Endpoint("localhost", 8080), "echo",
55 "Hello World".getBytes());
56 System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS)));
57 context.stop();
58 }
59 metrics.registerMetric(component, feature, "AsyncTimer", sendAndReceiveTimer);
60 }
61
62 public static class TestNettyMessagingService extends NettyMessagingService {
63 public TestNettyMessagingService(int port) throws Exception {
64 super(port);
65 }
66 }
67}