You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2013/01/10 14:46:48 UTC

git commit: Added the NettyServer test

Updated Branches:
  refs/heads/trunk a87f50def -> fe3d2e8ec


Added the NettyServer test

Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/fe3d2e8e
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/fe3d2e8e
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/fe3d2e8e

Branch: refs/heads/trunk
Commit: fe3d2e8ec2c0a20ce2517b14b47699a2bb62ae8a
Parents: a87f50d
Author: Emmanuel Lécharny <el...@apache.org>
Authored: Thu Jan 10 13:33:35 2013 +0100
Committer: Emmanuel Lécharny <el...@apache.org>
Committed: Thu Jan 10 13:33:35 2013 +0100

----------------------------------------------------------------------
 .../java/org/apache/mina/core/BenchmarkClient.java |    4 +
 .../apache/mina/core/BenchmarkServerFactory.java   |    2 +-
 ...MinaClientVsNettyServerBenchmarkBinaryTest.java |   66 ++++++
 .../org/apache/mina/core/NettyBenchmarkServer.java |  175 +++++++++++++++
 ...ettyClientVsNettyServerBenchmarkBinaryTest.java |   67 ++++++
 5 files changed, 313 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/fe3d2e8e/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClient.java
index 11ab518..393eea6 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClient.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClient.java
@@ -23,10 +23,14 @@ import java.io.IOException;
 import java.util.concurrent.CountDownLatch;
 
 /**
+ * An interface for a server
+ * 
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
 public interface BenchmarkClient {
+    /** Starts the client */
     public void start(int port, CountDownLatch counter, byte[] data) throws IOException;
 
+    /** Stops the client */
     public void stop() throws IOException;
 }

http://git-wip-us.apache.org/repos/asf/mina/blob/fe3d2e8e/benchmarks/src/test/java/org/apache/mina/core/BenchmarkServerFactory.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkServerFactory.java b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkServerFactory.java
index e7cd869..623ba52 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkServerFactory.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkServerFactory.java
@@ -34,7 +34,7 @@ public class BenchmarkServerFactory implements BenchmarkFactory<BenchmarkServer>
         case Mina:
             return new Mina3BenchmarkServer();
         case Netty:
-            return null;
+            return new NettyBenchmarkServer();
         default:
             throw new IllegalArgumentException("Invalid type " + type);
         }

http://git-wip-us.apache.org/repos/asf/mina/blob/fe3d2e8e/benchmarks/src/test/java/org/apache/mina/core/MinaClientVsNettyServerBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/MinaClientVsNettyServerBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/MinaClientVsNettyServerBenchmarkBinaryTest.java
new file mode 100644
index 0000000..f1ceb83
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/MinaClientVsNettyServerBenchmarkBinaryTest.java
@@ -0,0 +1,66 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.core;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.mina.core.BenchmarkFactory.Type;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class MinaClientVsNettyServerBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+    /**
+     * @param numberOfMessages
+     * @param messageSize
+     */
+    public MinaClientVsNettyServerBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+        super(numberOfMessages, messageSize, timeout);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Type getClientType() {
+        return Type.Mina;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Type getServerType() {
+        return Type.Netty;
+    }
+
+    @Parameters
+    public static Collection<Object[]> getParameters() {
+        Object[][] parameters = new Object[][] { 
+                { 1000000, 10, 2 * 60 }, 
+                { 1000000, 1 * 1024, 2 * 60 },
+                { 1000000, 10 * 1024, 2 * 60 }, 
+                { 100, 64 * 1024 * 1024, 10 * 60 } };
+        return Arrays.asList(parameters);
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/fe3d2e8e/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkServer.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkServer.java b/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkServer.java
new file mode 100644
index 0000000..62229b9
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkServer.java
@@ -0,0 +1,175 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.core;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.netty.bootstrap.ServerBootstrap;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.jboss.netty.channel.ChannelFactory;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.channel.ChannelPipeline;
+import org.jboss.netty.channel.ChannelPipelineFactory;
+import org.jboss.netty.channel.ChannelStateEvent;
+import org.jboss.netty.channel.Channels;
+import org.jboss.netty.channel.ChildChannelStateEvent;
+import org.jboss.netty.channel.ExceptionEvent;
+import org.jboss.netty.channel.MessageEvent;
+import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
+import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class NettyBenchmarkServer implements BenchmarkServer {
+
+    private static enum State {
+        WAIT_FOR_FIRST_BYTE_LENGTH, WAIT_FOR_SECOND_BYTE_LENGTH, WAIT_FOR_THIRD_BYTE_LENGTH, WAIT_FOR_FOURTH_BYTE_LENGTH, READING
+    }
+
+    private static final ChannelBuffer ACK = ChannelBuffers.buffer(1);
+
+    static {
+        ACK.writeByte(0);
+    }
+
+    private static final String STATE_ATTRIBUTE = NettyBenchmarkServer.class.getName() + ".state";
+
+    private static final String LENGTH_ATTRIBUTE = NettyBenchmarkServer.class.getName() + ".length";
+
+    private ChannelFactory factory;
+
+    /**
+     * Allocate a map as attachment for storing attributes.
+     * 
+     * @param ctx the channel context
+     * @return the map from the attachment
+     */
+    protected static Map<String, Object> getAttributesMap(ChannelHandlerContext ctx) {
+        Map<String, Object> map = (Map<String, Object>) ctx.getAttachment();
+        if (map == null) {
+            map = new HashMap<String, Object>();
+            ctx.setAttachment(map);
+        }
+        return map;
+    }
+
+    private static void setAttribute(ChannelHandlerContext ctx, String name, Object value) {
+        getAttributesMap(ctx).put(name, value);
+    }
+
+    private static Object getAttribute(ChannelHandlerContext ctx, String name) {
+        return getAttributesMap(ctx).get(name);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void start(int port) throws IOException {
+        factory = new NioServerSocketChannelFactory();
+        ServerBootstrap bootstrap = new ServerBootstrap(factory);
+        bootstrap.setOption("receiveBufferSize", 128 * 1024);
+        bootstrap.setOption("tcpNoDelay", true);
+        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
+            public ChannelPipeline getPipeline() throws Exception {
+                return Channels.pipeline(new SimpleChannelUpstreamHandler() {
+                    @Override
+                    public void childChannelOpen(ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
+                        System.out.println("childChannelOpen");
+                        setAttribute(ctx, STATE_ATTRIBUTE, State.WAIT_FOR_FIRST_BYTE_LENGTH);
+                    }
+
+                    @Override
+                    public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
+                        System.out.println("channelOpen");
+                        setAttribute(ctx, STATE_ATTRIBUTE, State.WAIT_FOR_FIRST_BYTE_LENGTH);
+                    }
+
+                    @Override
+                    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
+                        if (e.getMessage() instanceof ChannelBuffer) {
+                            ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
+
+                            State state = (State) getAttribute(ctx, STATE_ATTRIBUTE);
+                            int length = 0;
+                            if (getAttributesMap(ctx).containsKey(LENGTH_ATTRIBUTE)) {
+                                length = (Integer) getAttribute(ctx, LENGTH_ATTRIBUTE);
+                            }
+                            while (buffer.readableBytes() > 0) {
+                                switch (state) {
+                                case WAIT_FOR_FIRST_BYTE_LENGTH:
+                                    length = (buffer.readByte() & 255) << 24;
+                                    state = State.WAIT_FOR_SECOND_BYTE_LENGTH;
+                                    break;
+                                case WAIT_FOR_SECOND_BYTE_LENGTH:
+                                    length += (buffer.readByte() & 255) << 16;
+                                    state = State.WAIT_FOR_THIRD_BYTE_LENGTH;
+                                    break;
+                                case WAIT_FOR_THIRD_BYTE_LENGTH:
+                                    length += (buffer.readByte() & 255) << 8;
+                                    state = State.WAIT_FOR_FOURTH_BYTE_LENGTH;
+                                    break;
+                                case WAIT_FOR_FOURTH_BYTE_LENGTH:
+                                    length += (buffer.readByte() & 255);
+                                    state = State.READING;
+                                    if ((length == 0) && (buffer.readableBytes() == 0)) {
+                                        ctx.getChannel().write(ACK.slice());
+                                        state = State.WAIT_FOR_FIRST_BYTE_LENGTH;
+                                    }
+                                    break;
+                                case READING:
+                                    int remaining = buffer.readableBytes();
+                                    if (length > remaining) {
+                                        length -= remaining;
+                                        buffer.skipBytes(remaining);
+                                    } else {
+                                        buffer.skipBytes(length);
+                                        ctx.getChannel().write(ACK.slice());
+                                        state = State.WAIT_FOR_FIRST_BYTE_LENGTH;
+                                        length = 0;
+                                    }
+                                }
+                            }
+                            setAttribute(ctx, STATE_ATTRIBUTE, state);
+                            setAttribute(ctx, LENGTH_ATTRIBUTE, length);
+                        }
+                    }
+
+                    @Override
+                    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
+                        e.getCause().printStackTrace();
+                    }
+                });
+            }
+        });
+        bootstrap.bind(new InetSocketAddress(port));
+    }
+
+    /**
+     * {@inheritedDoc}
+     */
+    public void stop() throws IOException {
+        factory.releaseExternalResources();
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/fe3d2e8e/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsNettyServerBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsNettyServerBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsNettyServerBenchmarkBinaryTest.java
new file mode 100644
index 0000000..1921ca4
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsNettyServerBenchmarkBinaryTest.java
@@ -0,0 +1,67 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.core;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.mina.core.BenchmarkFactory.Type;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class NettyClientVsNettyServerBenchmarkBinaryTest
+    extends BenchmarkBinaryTest {
+
+    /**
+     * @param numberOfMessages
+     * @param messageSize
+     */
+    public NettyClientVsNettyServerBenchmarkBinaryTest( int numberOfMessages, int messageSize, int timeout ) {
+        super( numberOfMessages, messageSize, timeout );
+    }
+
+    /** {@inheritDoc}
+     */
+    @Override
+    public Type getClientType() {
+        return Type.Netty;
+    }
+
+    /** {@inheritDoc}
+     */
+    @Override
+    public Type getServerType() {
+        return Type.Netty;
+    }
+    
+    //TODO: analyze with Netty is so slow on large message: last test lower to 100 messages
+    @Parameters
+    public static Collection<Object[]> getParameters() {
+        Object[][] parameters = new Object[][] { 
+                { 1000000, 10, 2 * 60 }, 
+                { 1000000, 1 * 1024, 2 * 60 }, 
+                { 1000000, 10 * 1024, 2 * 60 },
+                { 100, 64 * 1024 * 1024, 10 * 60 }
+        };
+        return Arrays.asList(parameters);
+    }
+}