pankaj

Create handles and default to console reported for metrics

1 +package org.onlab.onos.foo;
2 +
3 +import java.io.IOException;
4 +
5 +import org.onlab.netty.Message;
6 +import org.onlab.netty.MessageHandler;
7 +import org.slf4j.Logger;
8 +import org.slf4j.LoggerFactory;
9 +
10 +
11 +/**
12 + * Message handler that echos the message back to the sender.
13 + */
14 +public class NettyEchoHandler implements MessageHandler {
15 +
16 + private final Logger log = LoggerFactory.getLogger(getClass());
17 +
18 + @Override
19 + public void handle(Message message) throws IOException {
20 + //log.info("Received message. Echoing it back to the sender.");
21 + message.respond(message.payload());
22 + }
23 +}
1 +package org.onlab.onos.foo;
2 +
3 +import org.onlab.netty.Message;
4 +import org.onlab.netty.MessageHandler;
5 +import org.slf4j.Logger;
6 +import org.slf4j.LoggerFactory;
7 +
8 +/**
9 + * A MessageHandler that simply logs the information.
10 + */
11 +public class NettyLoggingHandler implements MessageHandler {
12 +
13 + private final Logger log = LoggerFactory.getLogger(getClass());
14 +
15 + @Override
16 + public void handle(Message message) {
17 + log.info("Received message. Payload has {} bytes", message.payload().length);
18 + }
19 +}
...@@ -2,7 +2,6 @@ package org.onlab.onos.foo; ...@@ -2,7 +2,6 @@ package org.onlab.onos.foo;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException; 4 import java.util.concurrent.ExecutionException;
5 -import java.util.concurrent.TimeUnit;
6 import java.util.concurrent.TimeoutException; 5 import java.util.concurrent.TimeoutException;
7 6
8 import org.onlab.metrics.MetricsComponent; 7 import org.onlab.metrics.MetricsComponent;
...@@ -35,28 +34,37 @@ public final class SimpleNettyClient { ...@@ -35,28 +34,37 @@ public final class SimpleNettyClient {
35 MetricsManager metrics = new MetricsManager(); 34 MetricsManager metrics = new MetricsManager();
36 messaging.activate(); 35 messaging.activate();
37 metrics.activate(); 36 metrics.activate();
38 - MetricsFeature feature = new MetricsFeature("timers"); 37 + MetricsFeature feature = new MetricsFeature("latency");
39 MetricsComponent component = metrics.registerComponent("NettyMessaging"); 38 MetricsComponent component = metrics.registerComponent("NettyMessaging");
40 Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender"); 39 Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender");
40 +
41 final int warmup = 100; 41 final int warmup = 100;
42 for (int i = 0; i < warmup; i++) { 42 for (int i = 0; i < warmup; i++) {
43 + messaging.sendAsync(new Endpoint("localhost", 8081), "simple", "Hello World".getBytes());
44 + Response response = messaging
45 + .sendAndReceive(new Endpoint("localhost", 8081), "echo",
46 + "Hello World".getBytes());
47 + }
48 +
49 + final int iterations = 10000;
50 + for (int i = 0; i < iterations; i++) {
43 Timer.Context context = sendAsyncTimer.time(); 51 Timer.Context context = sendAsyncTimer.time();
44 - messaging.sendAsync(new Endpoint("localhost", 8080), "simple", "Hello World".getBytes()); 52 + messaging.sendAsync(new Endpoint("localhost", 8081), "simple", "Hello World".getBytes());
45 context.stop(); 53 context.stop();
46 } 54 }
47 metrics.registerMetric(component, feature, "AsyncTimer", sendAsyncTimer); 55 metrics.registerMetric(component, feature, "AsyncTimer", sendAsyncTimer);
48 56
49 Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive"); 57 Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive");
50 - final int iterations = 1000000; 58 +
51 for (int i = 0; i < iterations; i++) { 59 for (int i = 0; i < iterations; i++) {
52 Timer.Context context = sendAndReceiveTimer.time(); 60 Timer.Context context = sendAndReceiveTimer.time();
53 Response response = messaging 61 Response response = messaging
54 - .sendAndReceive(new Endpoint("localhost", 8080), "echo", 62 + .sendAndReceive(new Endpoint("localhost", 8081), "echo",
55 "Hello World".getBytes()); 63 "Hello World".getBytes());
56 - System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); 64 + // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS)));
57 context.stop(); 65 context.stop();
58 } 66 }
59 - metrics.registerMetric(component, feature, "AsyncTimer", sendAndReceiveTimer); 67 + metrics.registerMetric(component, feature, "PingPong", sendAndReceiveTimer);
60 } 68 }
61 69
62 public static class TestNettyMessagingService extends NettyMessagingService { 70 public static class TestNettyMessagingService extends NettyMessagingService {
......
1 package org.onlab.onos.foo; 1 package org.onlab.onos.foo;
2 2
3 -import org.onlab.netty.EchoHandler;
4 import org.onlab.netty.NettyMessagingService; 3 import org.onlab.netty.NettyMessagingService;
5 import org.slf4j.Logger; 4 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 5 import org.slf4j.LoggerFactory;
...@@ -19,10 +18,10 @@ import org.slf4j.LoggerFactory; ...@@ -19,10 +18,10 @@ import org.slf4j.LoggerFactory;
19 } 18 }
20 19
21 public static void startStandalone(String[] args) throws Exception { 20 public static void startStandalone(String[] args) throws Exception {
22 - NettyMessagingService server = new NettyMessagingService(8080); 21 + NettyMessagingService server = new NettyMessagingService(8081);
23 server.activate(); 22 server.activate();
24 - server.registerHandler("simple", new org.onlab.netty.LoggingHandler()); 23 + server.registerHandler("simple", new NettyLoggingHandler());
25 - server.registerHandler("echo", new EchoHandler()); 24 + server.registerHandler("echo", new NettyEchoHandler());
26 } 25 }
27 } 26 }
28 27
......
...@@ -153,6 +153,7 @@ ...@@ -153,6 +153,7 @@
153 description="ONOS sample playground application"> 153 description="ONOS sample playground application">
154 <feature>onos-api</feature> 154 <feature>onos-api</feature>
155 <bundle>mvn:org.onlab.onos/onos-app-foo/1.0.0-SNAPSHOT</bundle> 155 <bundle>mvn:org.onlab.onos/onos-app-foo/1.0.0-SNAPSHOT</bundle>
156 + <bundle>mvn:org.onlab.onos/onlab-netty/1.0.0-SNAPSHOT</bundle>
156 </feature> 157 </feature>
157 158
158 <feature name="onos-app-config" version="1.0.0" 159 <feature name="onos-app-config" version="1.0.0"
......
1 package org.onlab.metrics; 1 package org.onlab.metrics;
2 2
3 -import java.io.File;
4 -import java.util.Locale;
5 import java.util.Map; 3 import java.util.Map;
6 import java.util.concurrent.ConcurrentHashMap; 4 import java.util.concurrent.ConcurrentHashMap;
7 import java.util.concurrent.ConcurrentMap; 5 import java.util.concurrent.ConcurrentMap;
...@@ -11,8 +9,8 @@ import org.apache.felix.scr.annotations.Activate; ...@@ -11,8 +9,8 @@ import org.apache.felix.scr.annotations.Activate;
11 import org.apache.felix.scr.annotations.Component; 9 import org.apache.felix.scr.annotations.Component;
12 import org.apache.felix.scr.annotations.Deactivate; 10 import org.apache.felix.scr.annotations.Deactivate;
13 11
12 +import com.codahale.metrics.ConsoleReporter;
14 import com.codahale.metrics.Counter; 13 import com.codahale.metrics.Counter;
15 -import com.codahale.metrics.CsvReporter;
16 import com.codahale.metrics.Gauge; 14 import com.codahale.metrics.Gauge;
17 import com.codahale.metrics.Histogram; 15 import com.codahale.metrics.Histogram;
18 import com.codahale.metrics.Meter; 16 import com.codahale.metrics.Meter;
...@@ -69,15 +67,14 @@ public final class MetricsManager implements MetricsService { ...@@ -69,15 +67,14 @@ public final class MetricsManager implements MetricsService {
69 /** 67 /**
70 * Default Reporter for this metrics manager. 68 * Default Reporter for this metrics manager.
71 */ 69 */
72 - private final CsvReporter reporter; 70 + private final ConsoleReporter reporter;
73 71
74 public MetricsManager() { 72 public MetricsManager() {
75 this.metricsRegistry = new MetricRegistry(); 73 this.metricsRegistry = new MetricRegistry();
76 - this.reporter = CsvReporter.forRegistry(metricsRegistry) 74 + this.reporter = ConsoleReporter.forRegistry(metricsRegistry)
77 - .formatFor(Locale.US)
78 .convertRatesTo(TimeUnit.SECONDS) 75 .convertRatesTo(TimeUnit.SECONDS)
79 .convertDurationsTo(TimeUnit.MICROSECONDS) 76 .convertDurationsTo(TimeUnit.MICROSECONDS)
80 - .build(new File("/var/onos/log/metrics/")); 77 + .build();
81 } 78 }
82 79
83 @Activate 80 @Activate
......