Added a benchmark for BIO/BIO 

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

Branch: refs/heads/trunk
Commit: a6531756fddd62a3e57ef54f2229c9134b6d1c46
Parents: a63262e
Author: Emmanuel Lécharny <[email protected]>
Authored: Mon May 27 20:06:12 2013 +0200
Committer: Emmanuel Lécharny <[email protected]>
Committed: Mon May 27 20:06:12 2013 +0200

----------------------------------------------------------------------
 ...BioClientVsBioServerUdpBenchmarkBinaryTest.java |   68 ++++++
 .../mina/core/bio/udp/BioUdpBenchmarkServer.java   |  166 +++++++++++++++
 2 files changed, 234 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/a6531756/benchmarks/src/test/java/org/apache/mina/core/BioClientVsBioServerUdpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git 
a/benchmarks/src/test/java/org/apache/mina/core/BioClientVsBioServerUdpBenchmarkBinaryTest.java
 
b/benchmarks/src/test/java/org/apache/mina/core/BioClientVsBioServerUdpBenchmarkBinaryTest.java
new file mode 100644
index 0000000..f0260eb
--- /dev/null
+++ 
b/benchmarks/src/test/java/org/apache/mina/core/BioClientVsBioServerUdpBenchmarkBinaryTest.java
@@ -0,0 +1,68 @@
+/*
+ *  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 BioClientVsBioServerUdpBenchmarkBinaryTest extends 
BenchmarkBinaryTest {
+
+    /**
+     * @param numberOfMessages
+     * @param messageSize
+     */
+    public BioClientVsBioServerUdpBenchmarkBinaryTest(int numberOfMessages, 
int messageSize, int timeout) {
+        super(numberOfMessages, messageSize, timeout);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Type getClientType() {
+        return Type.Bio_udp;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Type getServerType() {
+        return Type.Bio_udp;
+    }
+
+    @Parameters(name = "{0} messages of size {1}")
+    public static Collection<Object[]> getParameters() {
+        // Note : depending on your OS, the maximum PDU you can send can vary. 
See sysctl net.inet.udp.maxdgram
+        Object[][] parameters = new Object[][] { 
+                { 1000000, 10, 2 * 60 }, 
+                { 1000000, 1 * 1024, 2 * 60 },
+                { 1000000, 2 * 1024, 2 * 60 }, 
+                { 1000000, 4 * 1024, 2 * 60 }, 
+                { 500000, 8 * 1024, 2 * 60 } }; // No need to test any 
further, the maximum size for an UDP message is 64Kb
+        return Arrays.asList(parameters);
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/a6531756/benchmarks/src/test/java/org/apache/mina/core/bio/udp/BioUdpBenchmarkServer.java
----------------------------------------------------------------------
diff --git 
a/benchmarks/src/test/java/org/apache/mina/core/bio/udp/BioUdpBenchmarkServer.java
 
b/benchmarks/src/test/java/org/apache/mina/core/bio/udp/BioUdpBenchmarkServer.java
new file mode 100644
index 0000000..3090565
--- /dev/null
+++ 
b/benchmarks/src/test/java/org/apache/mina/core/bio/udp/BioUdpBenchmarkServer.java
@@ -0,0 +1,166 @@
+/*
+ *  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.bio.udp;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+
+import org.apache.mina.api.IdleStatus;
+import org.apache.mina.api.IoHandler;
+import org.apache.mina.api.IoService;
+import org.apache.mina.api.IoSession;
+import org.apache.mina.core.BenchmarkServer;
+import org.apache.mina.core.nio.udp.Mina3UdpBenchmarkServer;
+import org.apache.mina.session.AttributeKey;
+import org.apache.mina.transport.bio.BioUdpServer;
+import org.apache.mina.transport.udp.DefaultUdpSessionConfig;
+import org.apache.mina.transport.udp.UdpSessionConfig;
+
+/**
+ * A Server that uses a BIO datagram to communicate with the server
+ * 
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class BioUdpBenchmarkServer 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 ByteBuffer ACK = ByteBuffer.allocate(1);
+
+    static {
+        ACK.put((byte) 0);
+        ACK.rewind();
+    }
+
+    private static final AttributeKey<State> STATE_ATTRIBUTE = new 
AttributeKey<State>(State.class,
+            Mina3UdpBenchmarkServer.class.getName() + ".state");
+
+    private static final AttributeKey<Integer> LENGTH_ATTRIBUTE = new 
AttributeKey<Integer>(Integer.class,
+            Mina3UdpBenchmarkServer.class.getName() + ".length");
+
+    private BioUdpServer udpServer;
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void start(int port) throws IOException {
+        UdpSessionConfig config = new DefaultUdpSessionConfig();
+        config.setReadBufferSize(65536);
+        udpServer = new BioUdpServer(config, null);
+        udpServer.setIoHandler(new IoHandler() {
+            @Override
+            public void sessionOpened(IoSession session) {
+                session.setAttribute(STATE_ATTRIBUTE, 
State.WAIT_FOR_FIRST_BYTE_LENGTH);
+            }
+
+            @Override
+            public void messageReceived(IoSession session, Object message) {
+                //System.out.println("Server Message received : " + message);
+                if (message instanceof ByteBuffer) {
+                    ByteBuffer buffer = (ByteBuffer) message;
+
+                    State state = session.getAttribute(STATE_ATTRIBUTE);
+                    int length = 0;
+
+                    if (session.getAttribute(LENGTH_ATTRIBUTE) != null) {
+                        length = session.getAttribute(LENGTH_ATTRIBUTE);
+                    }
+
+                    while (buffer.remaining() > 0) {
+                        switch (state) {
+                        case WAIT_FOR_FIRST_BYTE_LENGTH:
+                            length = (buffer.get() & 255) << 24;
+                            state = State.WAIT_FOR_SECOND_BYTE_LENGTH;
+                            break;
+                        case WAIT_FOR_SECOND_BYTE_LENGTH:
+                            length += (buffer.get() & 255) << 16;
+                            state = State.WAIT_FOR_THIRD_BYTE_LENGTH;
+                            break;
+                        case WAIT_FOR_THIRD_BYTE_LENGTH:
+                            length += (buffer.get() & 255) << 8;
+                            state = State.WAIT_FOR_FOURTH_BYTE_LENGTH;
+                            break;
+                        case WAIT_FOR_FOURTH_BYTE_LENGTH:
+                            length += (buffer.get() & 255);
+                            state = State.READING;
+                            if ((length == 0) && (buffer.remaining() == 0)) {
+                                session.write(ACK.slice());
+                                state = State.WAIT_FOR_FIRST_BYTE_LENGTH;
+                            }
+                            break;
+                        case READING:
+                            int remaining = buffer.remaining();
+                            if (length > remaining) {
+                                length -= remaining;
+                                buffer.position(buffer.position() + remaining);
+                            } else {
+                                buffer.position(buffer.position() + length);
+                                session.write(ACK.slice());
+                                state = State.WAIT_FOR_FIRST_BYTE_LENGTH;
+                                length = 0;
+                            }
+                        }
+                    }
+                    session.setAttribute(LENGTH_ATTRIBUTE, length);
+                    session.setAttribute(STATE_ATTRIBUTE, state);
+                }
+            }
+
+            @Override
+            public void exceptionCaught(IoSession session, Exception cause) {
+                cause.printStackTrace();
+            }
+
+            @Override
+            public void sessionClosed(IoSession session) {
+            }
+
+            @Override
+            public void sessionIdle(IoSession session, IdleStatus status) {
+            }
+
+            @Override
+            public void messageSent(IoSession session, Object message) {
+                //System.out.println("Server message sent :" + message);
+            }
+
+            @Override
+            public void serviceActivated(IoService service) {
+            }
+
+            @Override
+            public void serviceInactivated(IoService service) {
+            }
+        });
+
+        udpServer.bind(new InetSocketAddress(port));
+    }
+
+    /**
+     * {@inheritedDoc}
+     */
+    @Override
+    public void stop() throws IOException {
+        udpServer.unbind();
+    }
+}

Reply via email to