pankaj

Added simple netty client/servers to foo app to measure performance

...@@ -28,6 +28,11 @@ ...@@ -28,6 +28,11 @@
28 <version>${project.version}</version> 28 <version>${project.version}</version>
29 </dependency> 29 </dependency>
30 <dependency> 30 <dependency>
31 + <groupId>org.onlab.onos</groupId>
32 + <artifactId>onlab-netty</artifactId>
33 + <version>${project.version}</version>
34 + </dependency>
35 + <dependency>
31 <groupId>org.apache.karaf.shell</groupId> 36 <groupId>org.apache.karaf.shell</groupId>
32 <artifactId>org.apache.karaf.shell.console</artifactId> 37 <artifactId>org.apache.karaf.shell.console</artifactId>
33 </dependency> 38 </dependency>
......
1 +package org.onlab.onos.foo;
2 +
3 +import java.io.IOException;
4 +import java.util.concurrent.ExecutionException;
5 +import java.util.concurrent.TimeUnit;
6 +import java.util.concurrent.TimeoutException;
7 +
8 +import org.onlab.metrics.MetricsComponent;
9 +import org.onlab.metrics.MetricsFeature;
10 +import org.onlab.metrics.MetricsManager;
11 +import org.onlab.netty.Endpoint;
12 +import org.onlab.netty.NettyMessagingService;
13 +import org.onlab.netty.Response;
14 +
15 +import com.codahale.metrics.Timer;
16 +
17 +// FIXME: Should be move out to test or app
18 +public 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 +}
1 +package org.onlab.onos.foo;
2 +
3 +import static org.onlab.onos.foo.SimpleNettyClient.startStandalone;
4 +
5 +import org.apache.karaf.shell.commands.Argument;
6 +import org.apache.karaf.shell.commands.Command;
7 +import org.onlab.onos.cli.AbstractShellCommand;
8 +
9 +/**
10 + * Test Netty client performance.
11 + */
12 +@Command(scope = "onos", name = "simple-netty-client",
13 + description = "Starts the simple Netty client")
14 +public class SimpleNettyClientCommand extends AbstractShellCommand {
15 +
16 + @Argument(index = 0, name = "serverIp", description = "Server IP address",
17 + required = false, multiValued = false)
18 + String serverIp = "127.0.0.1";
19 +
20 + @Argument(index = 1, name = "workers", description = "IO workers",
21 + required = false, multiValued = false)
22 + String workers = "6";
23 +
24 + @Argument(index = 2, name = "messageCount", description = "Message count",
25 + required = false, multiValued = false)
26 + String messageCount = "1000000";
27 +
28 + @Argument(index = 3, name = "messageLength", description = "Message length (bytes)",
29 + required = false, multiValued = false)
30 + String messageLength = "128";
31 +
32 + @Argument(index = 4, name = "timeoutSecs", description = "Test timeout (seconds)",
33 + required = false, multiValued = false)
34 + String timeoutSecs = "60";
35 +
36 + @Override
37 + protected void execute() {
38 + try {
39 + startStandalone(new String[]{serverIp, workers, messageCount, messageLength, timeoutSecs});
40 + } catch (Exception e) {
41 + error("Unable to start client %s", e);
42 + }
43 + }
44 +
45 +}
1 package org.onlab.onos.foo; 1 package org.onlab.onos.foo;
2 2
3 -import java.io.IOException;
4 -
5 -import org.jboss.netty.handler.logging.LoggingHandler;
6 import org.onlab.netty.EchoHandler; 3 import org.onlab.netty.EchoHandler;
7 -import org.onlab.netty.KryoSerializer;
8 import org.onlab.netty.NettyMessagingService; 4 import org.onlab.netty.NettyMessagingService;
9 import org.slf4j.Logger; 5 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
...@@ -22,17 +18,11 @@ import org.slf4j.LoggerFactory; ...@@ -22,17 +18,11 @@ import org.slf4j.LoggerFactory;
22 System.exit(0); 18 System.exit(0);
23 } 19 }
24 20
25 - public static void startStandalone(String[] args) throws IOException { 21 + public static void startStandalone(String[] args) throws Exception {
26 - NettyMessagingService server = new NettyMessagingService(8080); 22 + NettyMessagingService server = new NettyMessagingService(8080);
27 - try { 23 + server.activate();
28 - server.activate(); 24 + server.registerHandler("simple", new org.onlab.netty.LoggingHandler());
29 - } catch (Exception e) { 25 + server.registerHandler("echo", new EchoHandler());
30 - e.printStackTrace();
31 - }
32 - server.setSerializer(new KryoSerializer());
33 - server.registerHandler("simple",
34 - (org.onlab.netty.MessageHandler) new LoggingHandler());
35 - server.registerHandler("echo", new EchoHandler());
36 } 26 }
37 } 27 }
38 28
......
1 +package org.onlab.onos.foo;
2 +
3 +import static org.onlab.onos.foo.SimpleNettyServer.startStandalone;
4 +
5 +import org.apache.karaf.shell.commands.Argument;
6 +import org.apache.karaf.shell.commands.Command;
7 +import org.onlab.onos.cli.AbstractShellCommand;
8 +
9 +/**
10 + * Starts the Simple Netty server.
11 + */
12 +@Command(scope = "onos", name = "test-netty-server",
13 + description = "Starts the simple netty server")
14 +public class SimpleNettyServerCommand extends AbstractShellCommand {
15 +
16 + @Argument(index = 0, name = "serverIp", description = "Server IP address",
17 + required = false, multiValued = false)
18 + String serverIp = "127.0.0.1";
19 +
20 + @Argument(index = 1, name = "workers", description = "IO workers",
21 + required = false, multiValued = false)
22 + String workers = "6";
23 +
24 + @Argument(index = 2, name = "messageLength", description = "Message length (bytes)",
25 + required = false, multiValued = false)
26 + String messageLength = "128";
27 +
28 + @Override
29 + protected void execute() {
30 + try {
31 + startStandalone(new String[]{serverIp, workers, messageLength});
32 + } catch (Exception e) {
33 + error("Unable to start server %s", e);
34 + }
35 + }
36 +
37 +}