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/03/20 16:41:25 UTC
[1/2] o Added a .gitgnore in the benchmarks module o Bumped up to
Netty 3.6.3 o Added netty UDP benchmarks o Created sub-package (nio and tcp)
for the various benchmarks o Started to refactor the IoSessionConfig
implementing classes to take care of UDP o
Updated Branches:
refs/heads/trunk 9b47d0d0c -> 626868afa
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java
new file mode 100755
index 0000000..562c087
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java
@@ -0,0 +1,121 @@
+/*
+ * 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.nio.udp;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.mina.api.IdleStatus;
+import org.apache.mina.api.IoFuture;
+import org.apache.mina.api.IoHandler;
+import org.apache.mina.api.IoService;
+import org.apache.mina.api.IoSession;
+import org.apache.mina.core.BenchmarkClient;
+import org.apache.mina.transport.nio.NioUdpClient;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Mina3UdpBenchmarkClient implements BenchmarkClient {
+ // The UDP client
+ private NioUdpClient udpClient;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void start(int port, final CountDownLatch counter, final byte[] data) throws IOException {
+ udpClient = new NioUdpClient();
+ udpClient.setIoHandler(new IoHandler() {
+ private void sendMessage(IoSession session, byte[] data) {
+ ByteBuffer iobuf = ByteBuffer.wrap(data);
+ session.write(iobuf);
+ }
+
+ public void sessionOpened(IoSession session) {
+ //System.out.println("Client session opened");
+ sendMessage(session, data);
+ }
+
+ public void messageReceived(IoSession session, Object message) {
+ //System.out.println("Client message received : " + message);
+ if (message instanceof ByteBuffer) {
+ ByteBuffer buffer = (ByteBuffer) message;
+ //System.out.println("length=" + buffer.remaining());
+ for (int i = 0; i < buffer.remaining(); ++i) {
+ counter.countDown();
+ long count = counter.getCount();
+ if (count > 0) {
+ sendMessage(session, data);
+ if (count % 100000 == 0) {
+ System.out.println("Received " + count);
+ }
+ }
+ }
+ }
+ }
+
+ public void exceptionCaught(IoSession session, Throwable 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("Client message sent : " + message);
+ }
+
+ @Override
+ public void serviceActivated(IoService service) {
+ }
+
+ @Override
+ public void serviceInactivated(IoService service) {
+ }
+ });
+
+ IoFuture<IoSession> future = udpClient.connect(new InetSocketAddress(port));
+
+ try {
+ IoSession session = future.get();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ExecutionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void stop() throws IOException {
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java
new file mode 100755
index 0000000..7ed7cfe
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java
@@ -0,0 +1,160 @@
+/*
+ * 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.nio.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.session.AttributeKey;
+import org.apache.mina.transport.nio.NioUdpServer;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Mina3UdpBenchmarkServer 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 NioUdpServer udpServer;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void start(int port) throws IOException {
+ udpServer = new NioUdpServer();
+ udpServer.setIoHandler(new IoHandler() {
+ public void sessionCreated(IoSession session) {
+ System.out.println("Session created...");
+ }
+
+ public void sessionOpened(IoSession session) {
+ //System.out.println("Server session opened");
+ session.setAttribute(STATE_ATTRIBUTE, State.WAIT_FOR_FIRST_BYTE_LENGTH);
+ }
+
+ 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);
+ }
+ }
+
+ public void exceptionCaught(IoSession session, Throwable 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}
+ */
+ public void stop() throws IOException {
+ udpServer.unbind();
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest.java
new file mode 100644
index 0000000..6ff3fda
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest.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.nio.udp;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.mina.core.BenchmarkBinaryTest;
+import org.apache.mina.core.BenchmarkFactory;
+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 Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+ /**
+ * @param numberOfMessages
+ * @param messageSize
+ */
+ public Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+ super(numberOfMessages, messageSize, timeout);
+ }
+
+ /** {@inheritDoc}
+ */
+ @Override
+ public Type getClientType() {
+ return Type.Netty3_tcp;
+ }
+
+ /** {@inheritDoc}
+ */
+ @Override
+ public Type getServerType() {
+ return Type.Netty3_tcp;
+ }
+
+ //TODO: analyze with Netty is so slow on large message: last test lower to 100 messages
+ @Parameters(name = "{0} messages of size {1}")
+ public static Collection<Object[]> getParameters() {
+ Object[][] parameters = new Object[][] { { 1000000, 10, 2 * 60 }, { 1000000, 1 * 1024, 2 * 60 },
+ { 1000000, 10 * 1024, 2 * 60 }, { 1000000, 20 * 1024, 2 * 60 }, { 500000, 50 * 1024, 2 * 60 },
+ { 200000, 100 * 1024, 2 * 60 }, { 100000, 200 * 1024, 2 * 60 }, { 50000, 500 * 1024, 2 * 60 },
+ { 20000, 1024 * 1024, 2 * 60 }, { 2000, 10 * 1024 * 1024, 2 * 60 }, { 500, 64 * 1024 * 1024, 2 * 60 } };
+ return Arrays.asList(parameters);
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java
new file mode 100644
index 0000000..f7637cf
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java
@@ -0,0 +1,101 @@
+/*
+ * 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.nio.udp;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.concurrent.CountDownLatch;
+
+import org.apache.mina.core.BenchmarkClient;
+import org.jboss.netty.bootstrap.ClientBootstrap;
+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.MessageEvent;
+import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
+import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Netty3UdpBenchmarkClient implements BenchmarkClient {
+
+ private ChannelFactory factory;
+
+ /**
+ *
+ */
+ public Netty3UdpBenchmarkClient() {
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void start(final int port, final CountDownLatch counter, final byte[] data) throws IOException {
+ factory = new NioDatagramChannelFactory();
+ ClientBootstrap bootstrap = new ClientBootstrap(factory);
+ bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
+ public ChannelPipeline getPipeline() throws Exception {
+ return Channels.pipeline(new SimpleChannelUpstreamHandler() {
+ private void sendMessage(ChannelHandlerContext ctx, byte[] data) {
+ ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(data);
+ ctx.getChannel().write(buffer);
+ }
+
+ @Override
+ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
+ if (e.getMessage() instanceof ChannelBuffer) {
+ ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
+ for (int i = 0; i < buffer.readableBytes(); ++i) {
+ counter.countDown();
+ if (counter.getCount() > 0) {
+ sendMessage(ctx, data);
+ } else {
+ ctx.getChannel().close();
+ }
+ }
+ } else {
+ throw new IllegalArgumentException(e.getMessage().getClass().getName());
+ }
+ }
+
+ @Override
+ public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
+ sendMessage(ctx, data);
+ }
+
+ });
+ }
+ });
+ bootstrap.connect(new InetSocketAddress(port));
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void stop() throws IOException {
+ factory.releaseExternalResources();
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkServer.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkServer.java b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkServer.java
new file mode 100644
index 0000000..d6d89ac
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkServer.java
@@ -0,0 +1,185 @@
+/*
+ * 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.nio.udp;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.mina.core.BenchmarkServer;
+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.group.ChannelGroup;
+import org.jboss.netty.channel.group.DefaultChannelGroup;
+import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Netty3UdpBenchmarkServer 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 = Netty3UdpBenchmarkServer.class.getName() + ".state";
+
+ private static final String LENGTH_ATTRIBUTE = Netty3UdpBenchmarkServer.class.getName() + ".length";
+
+ private ChannelFactory factory;
+
+ private ChannelGroup allChannels = new DefaultChannelGroup();
+
+ /**
+ * 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 NioDatagramChannelFactory();
+ ServerBootstrap bootstrap = new ServerBootstrap(factory);
+ 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);
+ allChannels.add(ctx.getChannel());
+ }
+
+ @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 channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
+ allChannels.remove(ctx.getChannel());
+ }
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
+ e.getCause().printStackTrace();
+ }
+ });
+ }
+ });
+ allChannels.add(bootstrap.bind(new InetSocketAddress(port)));
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void stop() throws IOException {
+ allChannels.disconnect().awaitUninterruptibly();
+ factory.releaseExternalResources();
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/api/IoSessionConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/api/IoSessionConfig.java b/core/src/main/java/org/apache/mina/api/IoSessionConfig.java
index 719773a..51a5c44 100644
--- a/core/src/main/java/org/apache/mina/api/IoSessionConfig.java
+++ b/core/src/main/java/org/apache/mina/api/IoSessionConfig.java
@@ -19,6 +19,11 @@
*/
package org.apache.mina.api;
+import java.net.DatagramSocket;
+import java.net.Socket;
+
+import org.apache.mina.session.TrafficClassEnum;
+
/**
* The configuration of {@link IoSession}.
*
@@ -27,6 +32,38 @@ package org.apache.mina.api;
public interface IoSessionConfig {
/**
+ * Returns the size of the read buffer that I/O processor allocates
+ * per each read. It's unusual to adjust this property because
+ * it's often adjusted automatically by the I/O processor.
+ *
+ * @return The buffer size, or null if its not set
+ */
+ Integer getReadBufferSize();
+
+ /**
+ * Sets the size of the read buffer that I/O processor allocates
+ * per each read. It's unusual to adjust this property because
+ * it's often adjusted automatically by the I/O processor.
+ *
+ * @param readBufferSize The buffer size used to read data from the socket
+ */
+ void setReadBufferSize(int readBufferSize);
+
+ /**
+ * @see DatagramSocket#getSendBufferSize()
+ */
+ Integer getSendBufferSize();
+
+ /**
+ * Sets the size of the buffer that I/O processor allocates
+ * per each write. It's unusual to adjust this property because
+ * it's often adjusted automatically by the I/O processor.
+ *
+ * @param sendBufferSize The buffer size used to send data into the socket
+ */
+ void setSendBufferSize(int sendBufferSize);
+
+ /**
* Returns idle time for the specified type of idleness in milli-seconds.
* @see IdleStatus
* @return the idle time in ms or <code>-1</code> if no idle time configured for this status
@@ -41,4 +78,35 @@ public interface IoSessionConfig {
* @param ildeTimeInMilli the timeout in milliseconds (<code>-1</code> for no idle detection on this status)
*/
void setIdleTimeInMillis(IdleStatus status, long ildeTimeInMilli);
+
+ /**
+ * @see Socket#getTrafficClass()
+ * @return The ToS set for this session
+ */
+ int getTrafficClass();
+
+ /**
+ * Set the ToS flag for this session
+ * @see Socket#setTrafficClass(int)
+ * @param trafficClass The ToS to set
+ */
+ void setTrafficClass(TrafficClassEnum trafficClass);
+
+ /**
+ * Set the ToS flag for this session
+ * @see Socket#setTrafficClass(int)
+ * @param trafficClass The ToS to set
+ */
+ void setTrafficClass(int trafficClass);
+
+ /**
+ * @see Socket#getReuseAddress()
+ */
+ Boolean isReuseAddress();
+
+ /**
+ * @see Socket#setReuseAddress(boolean)
+ * return <code>null</code> if the default system value is used
+ */
+ void setReuseAddress(boolean reuseAddress);
}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java b/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
index 0ded158..09cb308 100644
--- a/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
+++ b/core/src/main/java/org/apache/mina/session/AbstractIoSessionConfig.java
@@ -26,20 +26,32 @@ import org.apache.mina.api.IoSessionConfig;
/**
* Base class for session configuration.
- * Implement des session configuration properties commons to all the different transports.
+ * Implements session configuration properties commons to all the different transports.
*
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public abstract class AbstractIoSessionConfig implements IoSessionConfig {
-
//=====================
// idle management
//=====================
-
+ /** The delay we wait for a read before we consider the session is staled */
private long idleTimeRead = -1;
+ /** The delay we wait for a write before we consider the session is staled */
private long idleTimeWrite = -1;
+ /** The SO_RCVBUF socket option. The default buffer size used for Read */
+ private Integer readBufferSize = null;
+
+ /** The SO_SNDBUF socket option. The default buffer size used for Write */
+ private Integer sendBufferSize = null;
+
+ /** The ToS value */
+ private TrafficClassEnum trafficClass = TrafficClassEnum.IPTOS_DEFAULT;
+
+ /** The SO_REUSEADDR socket option */
+ private Boolean reuseAddress = null;
+
/**
* {@inheritDoc}
*/
@@ -72,4 +84,69 @@ public abstract class AbstractIoSessionConfig implements IoSessionConfig {
}
}
+ /**
+ * {@inheritDoc}
+ */
+ public Integer getReadBufferSize() {
+ return readBufferSize;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setReadBufferSize(int readBufferSize) {
+ if (readBufferSize <= 0) {
+ throw new IllegalArgumentException("readBufferSize: " + readBufferSize + " (expected: 1+)");
+ }
+ this.readBufferSize = readBufferSize;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Integer getSendBufferSize() {
+ return sendBufferSize;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setSendBufferSize(int sendBufferSize) {
+ this.sendBufferSize = sendBufferSize;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getTrafficClass() {
+ return trafficClass.getValue();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setTrafficClass(TrafficClassEnum trafficClass) {
+ this.trafficClass = trafficClass;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setTrafficClass(int trafficClass) {
+ this.trafficClass = TrafficClassEnum.valueOf(trafficClass);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Boolean isReuseAddress() {
+ return reuseAddress;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setReuseAddress(boolean reuseAddress) {
+ this.reuseAddress = reuseAddress;
+ }
}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/session/TrafficClassEnum.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/session/TrafficClassEnum.java b/core/src/main/java/org/apache/mina/session/TrafficClassEnum.java
new file mode 100644
index 0000000..bc049ec
--- /dev/null
+++ b/core/src/main/java/org/apache/mina/session/TrafficClassEnum.java
@@ -0,0 +1,70 @@
+/**
+ * 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.session;
+
+/**
+ * Define the list of Trafic Class available. They are used to define the ToS (Type Of Service)
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public enum TrafficClassEnum {
+ IPTOS_DEFAULT(0x00), IPTOS_LOWCOST(0x02), IPTOS_RELIABILITY(0x04), IPTOS_THROUGHPUT(0x08), IPTOS_LOWDELAY(0x10);
+
+ /** The internal value */
+ private int value;
+
+ /**
+ * The private constructor
+ * @param value The interned value
+ */
+ private TrafficClassEnum(int value) {
+ this.value = value;
+ }
+
+ /**
+ * @return Get back the internal value
+ */
+ public int getValue() {
+ return value;
+ }
+
+ /**
+ * Get back the Enum value fro the integer value
+ * @param value The interger value we are looking for
+ * @return The Enum value
+ */
+ public static TrafficClassEnum valueOf(int value) {
+ switch (value) {
+ case 0x02:
+ return IPTOS_LOWCOST;
+
+ case 0x04:
+ return IPTOS_RELIABILITY;
+
+ case 0x08:
+ return IPTOS_THROUGHPUT;
+
+ case 0x10:
+ return IPTOS_LOWDELAY;
+
+ default:
+ return IPTOS_DEFAULT;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/nio/NioTcpClient.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioTcpClient.java b/core/src/main/java/org/apache/mina/transport/nio/NioTcpClient.java
index a7d9e7d..b365ae5 100644
--- a/core/src/main/java/org/apache/mina/transport/nio/NioTcpClient.java
+++ b/core/src/main/java/org/apache/mina/transport/nio/NioTcpClient.java
@@ -27,8 +27,8 @@ import java.nio.channels.SocketChannel;
import org.apache.mina.api.IdleStatus;
import org.apache.mina.api.IoFuture;
import org.apache.mina.api.IoSession;
-import org.apache.mina.service.executor.OrderedHandlerExecutor;
import org.apache.mina.service.executor.IoHandlerExecutor;
+import org.apache.mina.service.executor.OrderedHandlerExecutor;
import org.apache.mina.service.idlechecker.IdleChecker;
import org.apache.mina.service.idlechecker.IndexedIdleChecker;
import org.apache.mina.transport.tcp.AbstractTcpClient;
@@ -153,10 +153,10 @@ public class NioTcpClient extends AbstractTcpClient {
session.getConfig().setTcpNoDelay(tcpNoDelay);
}
- Integer receiveBufferSize = config.getReceiveBufferSize();
+ Integer receiveBufferSize = config.getReadBufferSize();
if (receiveBufferSize != null) {
- session.getConfig().setReceiveBufferSize(receiveBufferSize);
+ session.getConfig().setReadBufferSize(receiveBufferSize);
}
Integer sendBufferSize = config.getSendBufferSize();
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java b/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
index 09c40e2..919b222 100644
--- a/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
+++ b/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
@@ -28,8 +28,8 @@ import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import org.apache.mina.api.IdleStatus;
-import org.apache.mina.service.executor.OrderedHandlerExecutor;
import org.apache.mina.service.executor.IoHandlerExecutor;
+import org.apache.mina.service.executor.OrderedHandlerExecutor;
import org.apache.mina.service.idlechecker.IdleChecker;
import org.apache.mina.service.idlechecker.IndexedIdleChecker;
import org.apache.mina.transport.tcp.AbstractTcpServer;
@@ -258,10 +258,10 @@ public class NioTcpServer extends AbstractTcpServer implements SelectorListener
session.getConfig().setTcpNoDelay(tcpNoDelay);
}
- final Integer receiveBufferSize = config.getReceiveBufferSize();
+ final Integer receiveBufferSize = config.getReadBufferSize();
if (receiveBufferSize != null) {
- session.getConfig().setReceiveBufferSize(receiveBufferSize);
+ session.getConfig().setReadBufferSize(receiveBufferSize);
}
final Integer sendBufferSize = config.getSendBufferSize();
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/nio/NioUdpClient.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioUdpClient.java b/core/src/main/java/org/apache/mina/transport/nio/NioUdpClient.java
index 5e931e7..66b646f 100644
--- a/core/src/main/java/org/apache/mina/transport/nio/NioUdpClient.java
+++ b/core/src/main/java/org/apache/mina/transport/nio/NioUdpClient.java
@@ -21,6 +21,7 @@ package org.apache.mina.transport.nio;
import java.io.IOException;
import java.net.SocketAddress;
+import java.nio.channels.DatagramChannel;
import org.apache.mina.api.IoFuture;
import org.apache.mina.api.IoSession;
@@ -34,11 +35,23 @@ import org.apache.mina.transport.udp.AbstractUdpClient;
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class NioUdpClient extends AbstractUdpClient {
+ /** the SelectorLoop for connecting the sessions */
+ // This is final, so that we know if it's not initialized
+ private final SelectorLoop connectSelectorLoop;
+
+ /**
+ * Create a new instance of NioUdpClient
+ */
+ public NioUdpClient() {
+ this(null);
+ }
+
/**
* Create a new instance of NioUdpClient
*/
public NioUdpClient(IoHandlerExecutor ioHandlerExecutor) {
super(ioHandlerExecutor);
+ connectSelectorLoop = new NioSelectorLoop("connect", 0);
}
@Override
@@ -49,7 +62,13 @@ public class NioUdpClient extends AbstractUdpClient {
@Override
public IoFuture<IoSession> connect(SocketAddress remoteAddress) throws IOException {
- // TODO Auto-generated method stub
+ DatagramChannel ch = DatagramChannel.open();
+
+ if (remoteAddress != null) {
+ ch.socket().bind(remoteAddress);
+ ch.connect(remoteAddress);
+ }
+
return null;
}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java b/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
index 5b032ab..c38cb55 100644
--- a/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
+++ b/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
@@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.apache.mina.api.IoSessionConfig;
import org.apache.mina.service.executor.IoHandlerExecutor;
+import org.apache.mina.service.executor.OrderedHandlerExecutor;
import org.apache.mina.service.idlechecker.IdleChecker;
import org.apache.mina.service.idlechecker.IndexedIdleChecker;
import org.apache.mina.transport.udp.AbstractUdpServer;
@@ -64,6 +65,14 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
private final Map<SocketAddress /* remote socket address */, NioUdpSession> sessions = new ConcurrentHashMap<SocketAddress, NioUdpSession>();
/**
+ * Create an UDP server with new selector pool of default size and a {@link IoHandlerExecutor} of default type (
+ * {@link OrderedHandlerExecutor})
+ */
+ public NioUdpServer() {
+ this(new NioSelectorLoop("accept", 0), null);
+ }
+
+ /**
* Create a new instance of NioUdpServer
*/
public NioUdpServer(NioSelectorLoop selectorLoop, IoHandlerExecutor ioHandlerExecutor) {
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java b/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java
index 076eed7..803d2ac 100644
--- a/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java
+++ b/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java
@@ -34,45 +34,6 @@ public class DefaultTcpSessionConfig extends AbstractIoSessionConfig implements
private SSLContext sslContext;
//=====================
- // buffers
- //=====================
- private Integer receiveBufferSize = null;
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Integer getReceiveBufferSize() {
- return receiveBufferSize;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void setReceiveBufferSize(int receiveBufferSize) {
- this.receiveBufferSize = receiveBufferSize;
- }
-
- private Integer sendBufferSize = null;
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Integer getSendBufferSize() {
- return sendBufferSize;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void setSendBufferSize(int sendBufferSize) {
- this.sendBufferSize = sendBufferSize;
- }
-
- //=====================
// socket options
//=====================
@@ -112,24 +73,6 @@ public class DefaultTcpSessionConfig extends AbstractIoSessionConfig implements
this.reuseAddress = reuseAddress;
}
- private Integer trafficClass;
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Integer getTrafficClass() {
- return trafficClass;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void setTrafficClass(int trafficClass) {
- this.trafficClass = trafficClass;
- }
-
private Boolean keepAlive = null;
/**
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/tcp/ProxyTcpSessionConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/tcp/ProxyTcpSessionConfig.java b/core/src/main/java/org/apache/mina/transport/tcp/ProxyTcpSessionConfig.java
index 125668d..5c6deb1 100644
--- a/core/src/main/java/org/apache/mina/transport/tcp/ProxyTcpSessionConfig.java
+++ b/core/src/main/java/org/apache/mina/transport/tcp/ProxyTcpSessionConfig.java
@@ -25,6 +25,7 @@ import javax.net.ssl.SSLContext;
import org.apache.mina.api.ConfigurationException;
import org.apache.mina.api.IdleStatus;
+import org.apache.mina.session.TrafficClassEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -133,7 +134,7 @@ public class ProxyTcpSessionConfig implements TcpSessionConfig {
* {@inheritDoc}
*/
@Override
- public Integer getReceiveBufferSize() {
+ public Integer getReadBufferSize() {
try {
return socket.getReceiveBufferSize();
} catch (SocketException e) {
@@ -145,7 +146,7 @@ public class ProxyTcpSessionConfig implements TcpSessionConfig {
* {@inheritDoc}
*/
@Override
- public void setReceiveBufferSize(int receiveBufferSize) {
+ public void setReadBufferSize(int receiveBufferSize) {
LOG.debug("set receive buffer size '{}' for session '{}'", receiveBufferSize, this);
try {
socket.setReceiveBufferSize(receiveBufferSize);
@@ -183,7 +184,7 @@ public class ProxyTcpSessionConfig implements TcpSessionConfig {
* {@inheritDoc}
*/
@Override
- public Integer getTrafficClass() {
+ public int getTrafficClass() {
try {
return socket.getTrafficClass();
} catch (SocketException e) {
@@ -208,6 +209,19 @@ public class ProxyTcpSessionConfig implements TcpSessionConfig {
* {@inheritDoc}
*/
@Override
+ public void setTrafficClass(TrafficClassEnum trafficClass) {
+ LOG.debug("set traffic class '{}' for session '{}'", trafficClass, this);
+ try {
+ socket.setTrafficClass(trafficClass.getValue());
+ } catch (SocketException e) {
+ throw new ConfigurationException(e);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public Boolean isKeepAlive() {
try {
return socket.getKeepAlive();
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/tcp/TcpSessionConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/tcp/TcpSessionConfig.java b/core/src/main/java/org/apache/mina/transport/tcp/TcpSessionConfig.java
index 78cd6c6..4264eb3 100644
--- a/core/src/main/java/org/apache/mina/transport/tcp/TcpSessionConfig.java
+++ b/core/src/main/java/org/apache/mina/transport/tcp/TcpSessionConfig.java
@@ -41,50 +41,6 @@ public interface TcpSessionConfig extends IoSessionConfig {
void setTcpNoDelay(boolean tcpNoDelay);
/**
- * @see Socket#getReuseAddress()
- * return <code>null</code> if the default system value is used
- */
- Boolean isReuseAddress();
-
- /**
- * @see Socket#setReuseAddress(boolean)
- */
- void setReuseAddress(boolean reuseAddress);
-
- /**
- * @see Socket#getReceiveBufferSize()
- * return <code>null</code> if the default system value is used
- */
- Integer getReceiveBufferSize();
-
- /**
- * @see Socket#setReceiveBufferSize(int)
- */
- void setReceiveBufferSize(int receiveBufferSize);
-
- /**
- * @see Socket#getSendBufferSize()
- * return <code>null</code> if the default system value is used
- */
- Integer getSendBufferSize();
-
- /**
- * @see Socket#setSendBufferSize(int)
- */
- void setSendBufferSize(int sendBufferSize);
-
- /**
- * @see Socket#getTrafficClass()
- * return <code>null</code> if the default system value is used
- */
- Integer getTrafficClass();
-
- /**
- * @see Socket#setTrafficClass(int)
- */
- void setTrafficClass(int trafficClass);
-
- /**
* @see Socket#getKeepAlive()
* return <code>null</code> if the default system value is used
*/
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java b/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java
index 197c796..3baee02 100644
--- a/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java
+++ b/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java
@@ -31,11 +31,15 @@ import org.apache.mina.service.server.AbstractIoServer;
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public abstract class AbstractUdpServer extends AbstractIoServer {
+ /** the default session configuration */
+ private UdpSessionConfig config;
+
/**
* Create an new AbsractUdpServer instance
*/
protected AbstractUdpServer(IoHandlerExecutor ioHandlerExecutor) {
super(ioHandlerExecutor);
+ this.config = new DefaultUdpSessionConfig();
}
/**
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/udp/DefaultUdpSessionConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/udp/DefaultUdpSessionConfig.java b/core/src/main/java/org/apache/mina/transport/udp/DefaultUdpSessionConfig.java
new file mode 100644
index 0000000..2489eda
--- /dev/null
+++ b/core/src/main/java/org/apache/mina/transport/udp/DefaultUdpSessionConfig.java
@@ -0,0 +1,65 @@
+/**
+ * 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.transport.udp;
+
+import java.net.DatagramSocket;
+
+import org.apache.mina.session.AbstractIoSessionConfig;
+
+/**
+ * Implementation for the UDP session configuration.
+ *
+ * Will hold the values for the service in change of configuring this session (before the session opening).
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class DefaultUdpSessionConfig extends AbstractIoSessionConfig implements UdpSessionConfig {
+ // The Broadcast flag and field
+ private static boolean DEFAULT_BROADCAST = false;
+
+ private boolean broadcast = DEFAULT_BROADCAST;
+
+ /**
+ * @see DatagramSocket#getBroadcast()
+ */
+ public boolean isBroadcast() {
+ return broadcast;
+ }
+
+ //=====================
+ // socket options
+ //=====================
+ @Override
+ public void setBroadcast(boolean broadcast) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public boolean isCloseOnPortUnreachable() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public void setCloseOnPortUnreachable(boolean closeOnPortUnreachable) {
+ // TODO Auto-generated method stub
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/main/java/org/apache/mina/transport/udp/UdpSessionConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/udp/UdpSessionConfig.java b/core/src/main/java/org/apache/mina/transport/udp/UdpSessionConfig.java
index ef12e87..ea5628d 100644
--- a/core/src/main/java/org/apache/mina/transport/udp/UdpSessionConfig.java
+++ b/core/src/main/java/org/apache/mina/transport/udp/UdpSessionConfig.java
@@ -19,6 +19,9 @@
*/
package org.apache.mina.transport.udp;
+import java.net.DatagramSocket;
+import java.net.PortUnreachableException;
+
import org.apache.mina.api.IoSessionConfig;
/**
@@ -26,6 +29,26 @@ import org.apache.mina.api.IoSessionConfig;
*
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
-public interface UdpSessionConfig extends IoSessionConfig{
+public interface UdpSessionConfig extends IoSessionConfig {
+ /**
+ * @see DatagramSocket#getBroadcast()
+ */
+ boolean isBroadcast();
+
+ /**
+ * @see DatagramSocket#setBroadcast(boolean)
+ */
+ void setBroadcast(boolean broadcast);
+
+ /**
+ * If method returns true, it means session should be closed when a
+ * {@link PortUnreachableException} occurs.
+ */
+ boolean isCloseOnPortUnreachable();
+ /**
+ * Sets if the session should be closed if an {@link PortUnreachableException}
+ * occurs.
+ */
+ void setCloseOnPortUnreachable(boolean closeOnPortUnreachable);
}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/core/src/test/java/org/apache/mina/transport/tcp/ProxySocketSessionConfigTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mina/transport/tcp/ProxySocketSessionConfigTest.java b/core/src/test/java/org/apache/mina/transport/tcp/ProxySocketSessionConfigTest.java
index 58548be..f278f4b 100644
--- a/core/src/test/java/org/apache/mina/transport/tcp/ProxySocketSessionConfigTest.java
+++ b/core/src/test/java/org/apache/mina/transport/tcp/ProxySocketSessionConfigTest.java
@@ -135,18 +135,18 @@ public class ProxySocketSessionConfigTest {
@Test
public void receiveBufferSize() throws SocketException {
when(socket.getReceiveBufferSize()).thenReturn(1234);
- assertEquals(1234, config.getReceiveBufferSize().intValue());
+ assertEquals(1234, config.getReadBufferSize().intValue());
verify(socket).getReceiveBufferSize();
verifyNoMoreInteractions(socket);
- config.setReceiveBufferSize(1234);
+ config.setReadBufferSize(1234);
verify(socket).setReceiveBufferSize(eq(1234));
verifyNoMoreInteractions(socket);
// handle error
when(socket.getReceiveBufferSize()).thenThrow(new SocketException("test"));
try {
- config.getReceiveBufferSize();
+ config.getReadBufferSize();
fail();
} catch (ConfigurationException e) {
assertEquals("test", e.getCause().getMessage());
@@ -155,7 +155,7 @@ public class ProxySocketSessionConfigTest {
// handle error
doThrow(new SocketException("test")).when(socket).setReceiveBufferSize(eq(1234));
try {
- config.setReceiveBufferSize(1234);
+ config.setReadBufferSize(1234);
fail();
} catch (ConfigurationException e) {
assertEquals("test", e.getCause().getMessage());
@@ -196,7 +196,7 @@ public class ProxySocketSessionConfigTest {
@Test
public void trafficClass() throws SocketException {
when(socket.getTrafficClass()).thenReturn(1234);
- assertEquals(1234, config.getTrafficClass().intValue());
+ assertEquals(1234, config.getTrafficClass());
verify(socket).getTrafficClass();
verifyNoMoreInteractions(socket);
[2/2] git commit: o Added a .gitgnore in the benchmarks module o
Bumped up to Netty 3.6.3 o Added netty UDP benchmarks o Created sub-package
(nio and tcp) for the various benchmarks o Started to refactor the
IoSessionConfig implementing classes to take c
Posted by el...@apache.org.
o Added a .gitgnore in the benchmarks module
o Bumped up to Netty 3.6.3
o Added netty UDP benchmarks
o Created sub-package (nio and tcp) for the various benchmarks
o Started to refactor the IoSessionConfig implementing classes to take care of UDP
o Started to mplement the UDP client and server
Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/626868af
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/626868af
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/626868af
Branch: refs/heads/trunk
Commit: 626868afa2d79cca9573574e130e5297d49a7cdd
Parents: 9b47d0d
Author: Emmanuel Lécharny <el...@apache.org>
Authored: Wed Mar 20 16:36:39 2013 +0100
Committer: Emmanuel Lécharny <el...@apache.org>
Committed: Wed Mar 20 16:36:39 2013 +0100
----------------------------------------------------------------------
benchmarks/.gitignore | 13 +
benchmarks/pom.xml | 4 +-
.../apache/mina/core/BenchmarkClientFactory.java | 14 +-
.../org/apache/mina/core/BenchmarkFactory.java | 2 +-
.../apache/mina/core/BenchmarkServerFactory.java | 14 +-
.../org/apache/mina/core/Mina3BenchmarkClient.java | 111 ---------
.../org/apache/mina/core/Mina3BenchmarkServer.java | 157 ------------
...ina3ClientVsMina3ServerBenchmarkBinaryTest.java | 74 ------
...3ClientVsMina3ServerTcpBenchmarkBinaryTest.java | 65 +++++
...3ClientVsMina3ServerUdpBenchmarkBinaryTest.java | 65 +++++
...ClientVsNetty3ServerTcpBenchmarkBinaryTest.java | 65 +++++
...MinaClientVsNettyServerBenchmarkBinaryTest.java | 74 ------
...3ClientVsMina3ServerTcpBenchmarkBinaryTest.java | 64 +++++
...ClientVsNetty3ServerTcpBenchmarkBinaryTest.java | 64 +++++
.../org/apache/mina/core/NettyBenchmarkClient.java | 102 --------
.../org/apache/mina/core/NettyBenchmarkServer.java | 187 ---------------
...NettyClientVsMinaServerBenchmarkBinaryTest.java | 74 ------
...ettyClientVsNettyServerBenchmarkBinaryTest.java | 74 ------
.../mina/core/nio/tcp/Mina3TcpBenchmarkClient.java | 112 +++++++++
.../mina/core/nio/tcp/Mina3TcpBenchmarkServer.java | 158 ++++++++++++
.../core/nio/tcp/Netty3TcpBenchmarkClient.java | 103 ++++++++
.../core/nio/tcp/Netty3TcpBenchmarkServer.java | 187 +++++++++++++++
.../mina/core/nio/udp/Mina3UdpBenchmarkClient.java | 121 ++++++++++
.../mina/core/nio/udp/Mina3UdpBenchmarkServer.java | 160 ++++++++++++
...ClientVsNetty3ServerUdpBenchmarkBinaryTest.java | 66 +++++
.../core/nio/udp/Netty3UdpBenchmarkClient.java | 101 ++++++++
.../core/nio/udp/Netty3UdpBenchmarkServer.java | 185 ++++++++++++++
.../java/org/apache/mina/api/IoSessionConfig.java | 68 ++++++
.../mina/session/AbstractIoSessionConfig.java | 83 ++++++-
.../org/apache/mina/session/TrafficClassEnum.java | 70 ++++++
.../apache/mina/transport/nio/NioTcpClient.java | 6 +-
.../apache/mina/transport/nio/NioTcpServer.java | 6 +-
.../apache/mina/transport/nio/NioUdpClient.java | 21 ++-
.../apache/mina/transport/nio/NioUdpServer.java | 9 +
.../transport/tcp/DefaultTcpSessionConfig.java | 57 -----
.../mina/transport/tcp/ProxyTcpSessionConfig.java | 20 ++-
.../mina/transport/tcp/TcpSessionConfig.java | 44 ----
.../mina/transport/udp/AbstractUdpServer.java | 4 +
.../transport/udp/DefaultUdpSessionConfig.java | 65 +++++
.../mina/transport/udp/UdpSessionConfig.java | 25 ++-
.../tcp/ProxySocketSessionConfigTest.java | 10 +-
41 files changed, 1920 insertions(+), 984 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/.gitignore
----------------------------------------------------------------------
diff --git a/benchmarks/.gitignore b/benchmarks/.gitignore
new file mode 100644
index 0000000..ca7d13f
--- /dev/null
+++ b/benchmarks/.gitignore
@@ -0,0 +1,13 @@
+.classpath
+.project
+.settings
+.wtpmodules
+*.ipr
+*.iws
+*.iml
+target/
+bin/
+*.log
+.deployables
+.clover
+
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/pom.xml
----------------------------------------------------------------------
diff --git a/benchmarks/pom.xml b/benchmarks/pom.xml
index 41d99dd..615756e 100755
--- a/benchmarks/pom.xml
+++ b/benchmarks/pom.xml
@@ -34,9 +34,9 @@
<properties>
<!-- defined in order to run against a different MINA version -->
<mina.version>${project.version}</mina.version>
- <netty.version>3.5.9.Final</netty.version>
+ <netty.version>3.6.3.Final</netty.version>
</properties>
-
+
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClientFactory.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClientFactory.java b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClientFactory.java
index 023f8c6..059651a 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClientFactory.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkClientFactory.java
@@ -19,6 +19,10 @@
*/
package org.apache.mina.core;
+import org.apache.mina.core.nio.tcp.Mina3TcpBenchmarkClient;
+import org.apache.mina.core.nio.tcp.Netty3TcpBenchmarkClient;
+import org.apache.mina.core.nio.udp.Mina3UdpBenchmarkClient;
+
/**
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
@@ -28,10 +32,12 @@ public class BenchmarkClientFactory implements BenchmarkFactory<BenchmarkClient>
public BenchmarkClient get(Type type) {
switch (type) {
- case Mina:
- return new Mina3BenchmarkClient();
- case Netty:
- return new NettyBenchmarkClient();
+ case Mina3_tcp:
+ return new Mina3TcpBenchmarkClient();
+ case Mina3_udp:
+ return new Mina3UdpBenchmarkClient();
+ case Netty3_tcp:
+ return new Netty3TcpBenchmarkClient();
default:
throw new IllegalArgumentException("Invalid type " + type);
}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/BenchmarkFactory.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkFactory.java b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkFactory.java
index d852cb8..d99fd54 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkFactory.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkFactory.java
@@ -29,7 +29,7 @@ public interface BenchmarkFactory<T> {
* The different types of providers
*/
public enum Type {
- Mina, Netty
+ Mina3_tcp, Mina3_udp, Netty3_tcp, Netty3_udp, Netty4_tcp, Netty4_udp
}
/**
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/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 623ba52..6377063 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/BenchmarkServerFactory.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/BenchmarkServerFactory.java
@@ -19,6 +19,10 @@
*/
package org.apache.mina.core;
+import org.apache.mina.core.nio.tcp.Mina3TcpBenchmarkServer;
+import org.apache.mina.core.nio.tcp.Netty3TcpBenchmarkServer;
+import org.apache.mina.core.nio.udp.Mina3UdpBenchmarkServer;
+
/**
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
@@ -31,10 +35,12 @@ public class BenchmarkServerFactory implements BenchmarkFactory<BenchmarkServer>
*/
public BenchmarkServer get(org.apache.mina.core.BenchmarkFactory.Type type) {
switch (type) {
- case Mina:
- return new Mina3BenchmarkServer();
- case Netty:
- return new NettyBenchmarkServer();
+ case Mina3_tcp:
+ return new Mina3TcpBenchmarkServer();
+ case Mina3_udp:
+ return new Mina3UdpBenchmarkServer();
+ case Netty3_tcp:
+ return new Netty3TcpBenchmarkServer();
default:
throw new IllegalArgumentException("Invalid type " + type);
}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/Mina3BenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Mina3BenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/Mina3BenchmarkClient.java
deleted file mode 100755
index 23681ad..0000000
--- a/benchmarks/src/test/java/org/apache/mina/core/Mina3BenchmarkClient.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * 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.nio.ByteBuffer;
-import java.util.concurrent.CountDownLatch;
-
-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.transport.nio.NioTcpClient;
-
-/**
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- */
-public class Mina3BenchmarkClient implements BenchmarkClient {
- // The TCP client
- private NioTcpClient client;
-
- /**
- * {@inheritDoc}
- */
- public void start(int port, final CountDownLatch counter, final byte[] data) throws IOException {
- client = new NioTcpClient();
- client.getSessionConfig().setSendBufferSize(64 * 1024);
- client.getSessionConfig().setTcpNoDelay(true);
- client.setIoHandler(new IoHandler() {
- private void sendMessage(IoSession session, byte[] data) {
- ByteBuffer iobuf = ByteBuffer.wrap(data);
- session.write(iobuf);
- }
-
- public void sessionOpened(IoSession session) {
- //System.out.println("Client session opened");
- sendMessage(session, data);
- }
-
- public void messageReceived(IoSession session, Object message) {
- //System.out.println("Client message received : " + message);
- if (message instanceof ByteBuffer) {
- ByteBuffer buffer = (ByteBuffer) message;
- //System.out.println("length=" + buffer.remaining());
- for (int i = 0; i < buffer.remaining(); ++i) {
- counter.countDown();
- long count = counter.getCount();
- if (count > 0) {
- sendMessage(session, data);
- if (count % 100000 == 0) {
- System.out.println("Received " + count);
- }
- }
- }
- }
- }
-
- public void exceptionCaught(IoSession session, Throwable 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("Client message sent : " + message);
- }
-
- @Override
- public void serviceActivated(IoService service) {
- }
-
- @Override
- public void serviceInactivated(IoService service) {
- }
- });
-
- client.connect(new InetSocketAddress(port));
- }
-
- /**
- * {@inheritedDoc}
- */
- public void stop() throws IOException {
- client.disconnect();
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/Mina3BenchmarkServer.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Mina3BenchmarkServer.java b/benchmarks/src/test/java/org/apache/mina/core/Mina3BenchmarkServer.java
deleted file mode 100755
index 80c1b11..0000000
--- a/benchmarks/src/test/java/org/apache/mina/core/Mina3BenchmarkServer.java
+++ /dev/null
@@ -1,157 +0,0 @@
-/*
- * 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.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.session.AttributeKey;
-import org.apache.mina.transport.nio.NioTcpServer;
-
-/**
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- */
-public class Mina3BenchmarkServer 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,
- Mina3BenchmarkServer.class.getName() + ".state");
-
- private static final AttributeKey<Integer> LENGTH_ATTRIBUTE = new AttributeKey<Integer>(Integer.class,
- Mina3BenchmarkServer.class.getName() + ".length");
-
- private NioTcpServer tcpServer;
-
- /**
- * {@inheritDoc}
- */
- public void start(int port) throws IOException {
- tcpServer = new NioTcpServer();
- tcpServer.getSessionConfig().setReceiveBufferSize(128 * 1024);
- tcpServer.getSessionConfig().setTcpNoDelay(true);
- tcpServer.setIoHandler(new IoHandler() {
- public void sessionOpened(IoSession session) {
- //System.out.println("Server session opened");
- session.setAttribute(STATE_ATTRIBUTE, State.WAIT_FOR_FIRST_BYTE_LENGTH);
- }
-
- 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);
- }
- }
-
- public void exceptionCaught(IoSession session, Throwable 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) {
- }
- });
-
- tcpServer.bind(new InetSocketAddress(port));
- }
-
- /**
- * {@inheritedDoc}
- */
- public void stop() throws IOException {
- tcpServer.unbind();
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerBenchmarkBinaryTest.java
deleted file mode 100755
index 19eba35..0000000
--- a/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerBenchmarkBinaryTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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 Mina3ClientVsMina3ServerBenchmarkBinaryTest extends BenchmarkBinaryTest {
-
- /**
- * @param numberOfMessages
- * @param messageSize
- */
- public Mina3ClientVsMina3ServerBenchmarkBinaryTest(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.Mina;
- }
-
- @Parameters(name="{0} messages of size {1}")
- public static Collection<Object[]> getParameters() {
- Object[][] parameters = new Object[][] {
- { 1000000, 10, 2 * 60 },
- { 1000000, 1 * 1024, 2 * 60 },
- { 1000000, 10 * 1024, 2 * 60 },
- { 1000000, 20 * 1024, 2 * 60 },
- { 500000, 50 * 1024, 2 * 60 },
- { 200000, 100 * 1024, 2 * 60 },
- { 100000, 200 * 1024, 2 * 60 },
- { 50000, 500 * 1024, 2 * 60 },
- { 20000, 1024 * 1024, 2 * 60 },
- { 2000, 10 * 1024* 1024, 2 * 60 },
- { 500, 64 * 1024 * 1024, 2 * 60 }
- };
- return Arrays.asList(parameters);
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerTcpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerTcpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerTcpBenchmarkBinaryTest.java
new file mode 100755
index 0000000..d258ec8
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerTcpBenchmarkBinaryTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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 Mina3ClientVsMina3ServerTcpBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+ /**
+ * @param numberOfMessages
+ * @param messageSize
+ */
+ public Mina3ClientVsMina3ServerTcpBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+ super(numberOfMessages, messageSize, timeout);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Type getClientType() {
+ return Type.Mina3_tcp;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Type getServerType() {
+ return Type.Mina3_tcp;
+ }
+
+ @Parameters(name = "{0} messages of size {1}")
+ public static Collection<Object[]> getParameters() {
+ Object[][] parameters = new Object[][] { { 1000000, 10, 2 * 60 }, { 1000000, 1 * 1024, 2 * 60 },
+ { 1000000, 10 * 1024, 2 * 60 }, { 1000000, 20 * 1024, 2 * 60 }, { 500000, 50 * 1024, 2 * 60 },
+ { 200000, 100 * 1024, 2 * 60 }, { 100000, 200 * 1024, 2 * 60 }, { 50000, 500 * 1024, 2 * 60 },
+ { 20000, 1024 * 1024, 2 * 60 }, { 2000, 10 * 1024 * 1024, 2 * 60 }, { 500, 64 * 1024 * 1024, 2 * 60 } };
+ return Arrays.asList(parameters);
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
new file mode 100644
index 0000000..6d85a55
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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 Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+ /**
+ * @param numberOfMessages
+ * @param messageSize
+ */
+ public Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+ super(numberOfMessages, messageSize, timeout);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Type getClientType() {
+ return Type.Mina3_udp;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Type getServerType() {
+ return Type.Mina3_udp;
+ }
+
+ @Parameters(name = "{0} messages of size {1}")
+ public static Collection<Object[]> getParameters() {
+ Object[][] parameters = new Object[][] { { 1000000, 10, 2 * 60 }, { 1000000, 1 * 1024, 2 * 60 },
+ { 1000000, 10 * 1024, 2 * 60 }, { 1000000, 20 * 1024, 2 * 60 }, { 500000, 50 * 1024, 2 * 60 },
+ { 200000, 100 * 1024, 2 * 60 }, { 100000, 200 * 1024, 2 * 60 }, { 50000, 500 * 1024, 2 * 60 },
+ { 20000, 1024 * 1024, 2 * 60 }, { 2000, 10 * 1024 * 1024, 2 * 60 }, { 500, 64 * 1024 * 1024, 2 * 60 } };
+ return Arrays.asList(parameters);
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsNetty3ServerTcpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsNetty3ServerTcpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsNetty3ServerTcpBenchmarkBinaryTest.java
new file mode 100644
index 0000000..7ce1769
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsNetty3ServerTcpBenchmarkBinaryTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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 Mina3ClientVsNetty3ServerTcpBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+ /**
+ * @param numberOfMessages
+ * @param messageSize
+ */
+ public Mina3ClientVsNetty3ServerTcpBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+ super(numberOfMessages, messageSize, timeout);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Type getClientType() {
+ return Type.Mina3_tcp;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Type getServerType() {
+ return Type.Netty3_tcp;
+ }
+
+ @Parameters()
+ public static Collection<Object[]> getParameters() {
+ Object[][] parameters = new Object[][] { { 1000000, 10, 2 * 60 }, { 1000000, 1 * 1024, 2 * 60 },
+ { 1000000, 10 * 1024, 2 * 60 }, { 1000000, 20 * 1024, 2 * 60 }, { 500000, 50 * 1024, 2 * 60 },
+ { 200000, 100 * 1024, 2 * 60 }, { 100000, 200 * 1024, 2 * 60 }, { 50000, 500 * 1024, 2 * 60 },
+ { 20000, 1024 * 1024, 2 * 60 }, { 2000, 10 * 1024 * 1024, 2 * 60 }, { 500, 64 * 1024 * 1024, 2 * 60 } };
+ return Arrays.asList(parameters);
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/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
deleted file mode 100644
index 2df932e..0000000
--- a/benchmarks/src/test/java/org/apache/mina/core/MinaClientVsNettyServerBenchmarkBinaryTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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 },
- { 1000000, 20 * 1024, 2 * 60 },
- { 500000, 50 * 1024, 2 * 60 },
- { 200000, 100 * 1024, 2 * 60 },
- { 100000, 200 * 1024, 2 * 60 },
- { 50000, 500 * 1024, 2 * 60 },
- { 20000, 1024 * 1024, 2 * 60 },
- { 2000, 10 * 1024* 1024, 2 * 60 },
- { 500, 64 * 1024 * 1024, 2 * 60 }
- };
- return Arrays.asList(parameters);
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsMina3ServerTcpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsMina3ServerTcpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsMina3ServerTcpBenchmarkBinaryTest.java
new file mode 100644
index 0000000..b93b799
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsMina3ServerTcpBenchmarkBinaryTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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 Netty3ClientVsMina3ServerTcpBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+ /**
+ * @param numberOfMessages
+ * @param messageSize
+ */
+ public Netty3ClientVsMina3ServerTcpBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+ super(numberOfMessages, messageSize, timeout);
+ }
+
+ /** {@inheritDoc}
+ */
+ @Override
+ public Type getClientType() {
+ return Type.Netty3_tcp;
+ }
+
+ /** {@inheritDoc}
+ */
+ @Override
+ public Type getServerType() {
+ return Type.Mina3_tcp;
+ }
+
+ //TODO: analyze with Netty is so slow on large message: last test lower to 100 messages
+ @Parameters(name = "{0} messages of size {1}")
+ public static Collection<Object[]> getParameters() {
+ Object[][] parameters = new Object[][] { { 1000000, 10, 2 * 60 }, { 1000000, 1 * 1024, 2 * 60 },
+ { 1000000, 10 * 1024, 2 * 60 }, { 1000000, 20 * 1024, 2 * 60 }, { 500000, 50 * 1024, 2 * 60 },
+ { 200000, 100 * 1024, 2 * 60 }, { 100000, 200 * 1024, 2 * 60 }, { 50000, 500 * 1024, 2 * 60 },
+ { 20000, 1024 * 1024, 2 * 60 }, { 2000, 10 * 1024 * 1024, 2 * 60 }, { 500, 64 * 1024 * 1024, 2 * 60 } };
+ return Arrays.asList(parameters);
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsNetty3ServerTcpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsNetty3ServerTcpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsNetty3ServerTcpBenchmarkBinaryTest.java
new file mode 100644
index 0000000..0ba293c
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsNetty3ServerTcpBenchmarkBinaryTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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 Netty3ClientVsNetty3ServerTcpBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+ /**
+ * @param numberOfMessages
+ * @param messageSize
+ */
+ public Netty3ClientVsNetty3ServerTcpBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+ super(numberOfMessages, messageSize, timeout);
+ }
+
+ /** {@inheritDoc}
+ */
+ @Override
+ public Type getClientType() {
+ return Type.Netty3_tcp;
+ }
+
+ /** {@inheritDoc}
+ */
+ @Override
+ public Type getServerType() {
+ return Type.Netty3_tcp;
+ }
+
+ //TODO: analyze with Netty is so slow on large message: last test lower to 100 messages
+ @Parameters(name = "{0} messages of size {1}")
+ public static Collection<Object[]> getParameters() {
+ Object[][] parameters = new Object[][] { { 1000000, 10, 2 * 60 }, { 1000000, 1 * 1024, 2 * 60 },
+ { 1000000, 10 * 1024, 2 * 60 }, { 1000000, 20 * 1024, 2 * 60 }, { 500000, 50 * 1024, 2 * 60 },
+ { 200000, 100 * 1024, 2 * 60 }, { 100000, 200 * 1024, 2 * 60 }, { 50000, 500 * 1024, 2 * 60 },
+ { 20000, 1024 * 1024, 2 * 60 }, { 2000, 10 * 1024 * 1024, 2 * 60 }, { 500, 64 * 1024 * 1024, 2 * 60 } };
+ return Arrays.asList(parameters);
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkClient.java
deleted file mode 100644
index 71145dc..0000000
--- a/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkClient.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * 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.concurrent.CountDownLatch;
-
-import org.jboss.netty.bootstrap.ClientBootstrap;
-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.MessageEvent;
-import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
-import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
-
-/**
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- */
-public class NettyBenchmarkClient implements BenchmarkClient {
-
- private ChannelFactory factory;
-
- /**
- *
- */
- public NettyBenchmarkClient() {
- }
-
- /**
- * {@inheritedDoc}
- */
- public void start(final int port, final CountDownLatch counter, final byte[] data) throws IOException {
- factory = new NioClientSocketChannelFactory();
- ClientBootstrap bootstrap = new ClientBootstrap(factory);
- bootstrap.setOption("sendBufferSize", 64 * 1024);
- bootstrap.setOption("tcpNoDelay", true);
- bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
- public ChannelPipeline getPipeline() throws Exception {
- return Channels.pipeline(new SimpleChannelUpstreamHandler() {
- private void sendMessage(ChannelHandlerContext ctx, byte[] data) {
- ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(data);
- ctx.getChannel().write(buffer);
- }
-
- @Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
- if (e.getMessage() instanceof ChannelBuffer) {
- ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
- for (int i = 0; i < buffer.readableBytes(); ++i) {
- counter.countDown();
- if (counter.getCount() > 0) {
- sendMessage(ctx, data);
- } else {
- ctx.getChannel().close();
- }
- }
- } else {
- throw new IllegalArgumentException(e.getMessage().getClass().getName());
- }
- }
-
- @Override
- public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
- sendMessage(ctx, data);
- }
-
- });
- }
- });
- bootstrap.connect(new InetSocketAddress(port));
- }
-
- /**
- * {@inheritedDoc}
- */
- public void stop() throws IOException {
- factory.releaseExternalResources();
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/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
deleted file mode 100644
index 44860a3..0000000
--- a/benchmarks/src/test/java/org/apache/mina/core/NettyBenchmarkServer.java
+++ /dev/null
@@ -1,187 +0,0 @@
-/*
- * 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.Channel;
-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.group.ChannelGroup;
-import org.jboss.netty.channel.group.DefaultChannelGroup;
-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;
-
- private ChannelGroup allChannels = new DefaultChannelGroup();
-
- /**
- * 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);
- allChannels.add(ctx.getChannel());
- }
-
- @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 channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
- allChannels.remove(ctx.getChannel());
- }
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
- e.getCause().printStackTrace();
- }
- });
- }
- });
- allChannels.add(bootstrap.bind(new InetSocketAddress(port)));
- }
-
- /**
- * {@inheritedDoc}
- */
- public void stop() throws IOException {
- allChannels.disconnect().awaitUninterruptibly();
- factory.releaseExternalResources();
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsMinaServerBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsMinaServerBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsMinaServerBenchmarkBinaryTest.java
deleted file mode 100644
index eaa5c88..0000000
--- a/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsMinaServerBenchmarkBinaryTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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 NettyClientVsMinaServerBenchmarkBinaryTest
- extends BenchmarkBinaryTest {
-
- /**
- * @param numberOfMessages
- * @param messageSize
- */
- public NettyClientVsMinaServerBenchmarkBinaryTest( 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.Mina;
- }
-
- //TODO: analyze with Netty is so slow on large message: last test lower to 100 messages
- @Parameters(name="{0} messages of size {1}")
- public static Collection<Object[]> getParameters() {
- Object[][] parameters = new Object[][] {
- { 1000000, 10, 2 * 60 },
- { 1000000, 1 * 1024, 2 * 60 },
- { 1000000, 10 * 1024, 2 * 60 },
- { 1000000, 20 * 1024, 2 * 60 },
- { 500000, 50 * 1024, 2 * 60 },
- { 200000, 100 * 1024, 2 * 60 },
- { 100000, 200 * 1024, 2 * 60 },
- { 50000, 500 * 1024, 2 * 60 },
- { 20000, 1024 * 1024, 2 * 60 },
- { 2000, 10 * 1024* 1024, 2 * 60 },
- { 500, 64 * 1024 * 1024, 2 * 60 }
- };
- return Arrays.asList(parameters);
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/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
deleted file mode 100644
index ba57b40..0000000
--- a/benchmarks/src/test/java/org/apache/mina/core/NettyClientVsNettyServerBenchmarkBinaryTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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(name="{0} messages of size {1}")
- public static Collection<Object[]> getParameters() {
- Object[][] parameters = new Object[][] {
- { 1000000, 10, 2 * 60 },
- { 1000000, 1 * 1024, 2 * 60 },
- { 1000000, 10 * 1024, 2 * 60 },
- { 1000000, 20 * 1024, 2 * 60 },
- { 500000, 50 * 1024, 2 * 60 },
- { 200000, 100 * 1024, 2 * 60 },
- { 100000, 200 * 1024, 2 * 60 },
- { 50000, 500 * 1024, 2 * 60 },
- { 20000, 1024 * 1024, 2 * 60 },
- { 2000, 10 * 1024* 1024, 2 * 60 },
- { 500, 64 * 1024 * 1024, 2 * 60 }
- };
- return Arrays.asList(parameters);
- }
-}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java
new file mode 100755
index 0000000..98fa5bb
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java
@@ -0,0 +1,112 @@
+/*
+ * 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.nio.tcp;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.util.concurrent.CountDownLatch;
+
+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.BenchmarkClient;
+import org.apache.mina.transport.nio.NioTcpClient;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Mina3TcpBenchmarkClient implements BenchmarkClient {
+ // The TCP client
+ private NioTcpClient client;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void start(int port, final CountDownLatch counter, final byte[] data) throws IOException {
+ client = new NioTcpClient();
+ client.getSessionConfig().setSendBufferSize(64 * 1024);
+ client.getSessionConfig().setTcpNoDelay(true);
+ client.setIoHandler(new IoHandler() {
+ private void sendMessage(IoSession session, byte[] data) {
+ ByteBuffer iobuf = ByteBuffer.wrap(data);
+ session.write(iobuf);
+ }
+
+ public void sessionOpened(IoSession session) {
+ //System.out.println("Client session opened");
+ sendMessage(session, data);
+ }
+
+ public void messageReceived(IoSession session, Object message) {
+ //System.out.println("Client message received : " + message);
+ if (message instanceof ByteBuffer) {
+ ByteBuffer buffer = (ByteBuffer) message;
+ //System.out.println("length=" + buffer.remaining());
+ for (int i = 0; i < buffer.remaining(); ++i) {
+ counter.countDown();
+ long count = counter.getCount();
+ if (count > 0) {
+ sendMessage(session, data);
+ if (count % 100000 == 0) {
+ System.out.println("Received " + count);
+ }
+ }
+ }
+ }
+ }
+
+ public void exceptionCaught(IoSession session, Throwable 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("Client message sent : " + message);
+ }
+
+ @Override
+ public void serviceActivated(IoService service) {
+ }
+
+ @Override
+ public void serviceInactivated(IoService service) {
+ }
+ });
+
+ client.connect(new InetSocketAddress(port));
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void stop() throws IOException {
+ client.disconnect();
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java
new file mode 100755
index 0000000..4443a00
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java
@@ -0,0 +1,158 @@
+/*
+ * 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.nio.tcp;
+
+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.session.AttributeKey;
+import org.apache.mina.transport.nio.NioTcpServer;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Mina3TcpBenchmarkServer 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,
+ Mina3TcpBenchmarkServer.class.getName() + ".state");
+
+ private static final AttributeKey<Integer> LENGTH_ATTRIBUTE = new AttributeKey<Integer>(Integer.class,
+ Mina3TcpBenchmarkServer.class.getName() + ".length");
+
+ private NioTcpServer tcpServer;
+
+ /**
+ * {@inheritDoc}
+ */
+ public void start(int port) throws IOException {
+ tcpServer = new NioTcpServer();
+ tcpServer.getSessionConfig().setReadBufferSize(128 * 1024);
+ tcpServer.getSessionConfig().setTcpNoDelay(true);
+ tcpServer.setIoHandler(new IoHandler() {
+ public void sessionOpened(IoSession session) {
+ //System.out.println("Server session opened");
+ session.setAttribute(STATE_ATTRIBUTE, State.WAIT_FOR_FIRST_BYTE_LENGTH);
+ }
+
+ 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);
+ }
+ }
+
+ public void exceptionCaught(IoSession session, Throwable 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) {
+ }
+ });
+
+ tcpServer.bind(new InetSocketAddress(port));
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void stop() throws IOException {
+ tcpServer.unbind();
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Netty3TcpBenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Netty3TcpBenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Netty3TcpBenchmarkClient.java
new file mode 100644
index 0000000..cd712be
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Netty3TcpBenchmarkClient.java
@@ -0,0 +1,103 @@
+/*
+ * 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.nio.tcp;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.concurrent.CountDownLatch;
+
+import org.apache.mina.core.BenchmarkClient;
+import org.jboss.netty.bootstrap.ClientBootstrap;
+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.MessageEvent;
+import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
+import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Netty3TcpBenchmarkClient implements BenchmarkClient {
+
+ private ChannelFactory factory;
+
+ /**
+ *
+ */
+ public Netty3TcpBenchmarkClient() {
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void start(final int port, final CountDownLatch counter, final byte[] data) throws IOException {
+ factory = new NioClientSocketChannelFactory();
+ ClientBootstrap bootstrap = new ClientBootstrap(factory);
+ bootstrap.setOption("sendBufferSize", 64 * 1024);
+ bootstrap.setOption("tcpNoDelay", true);
+ bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
+ public ChannelPipeline getPipeline() throws Exception {
+ return Channels.pipeline(new SimpleChannelUpstreamHandler() {
+ private void sendMessage(ChannelHandlerContext ctx, byte[] data) {
+ ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(data);
+ ctx.getChannel().write(buffer);
+ }
+
+ @Override
+ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
+ if (e.getMessage() instanceof ChannelBuffer) {
+ ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
+ for (int i = 0; i < buffer.readableBytes(); ++i) {
+ counter.countDown();
+ if (counter.getCount() > 0) {
+ sendMessage(ctx, data);
+ } else {
+ ctx.getChannel().close();
+ }
+ }
+ } else {
+ throw new IllegalArgumentException(e.getMessage().getClass().getName());
+ }
+ }
+
+ @Override
+ public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
+ sendMessage(ctx, data);
+ }
+
+ });
+ }
+ });
+ bootstrap.connect(new InetSocketAddress(port));
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void stop() throws IOException {
+ factory.releaseExternalResources();
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/626868af/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Netty3TcpBenchmarkServer.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Netty3TcpBenchmarkServer.java b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Netty3TcpBenchmarkServer.java
new file mode 100644
index 0000000..f529ff4
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Netty3TcpBenchmarkServer.java
@@ -0,0 +1,187 @@
+/*
+ * 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.nio.tcp;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.mina.core.BenchmarkServer;
+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.group.ChannelGroup;
+import org.jboss.netty.channel.group.DefaultChannelGroup;
+import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Netty3TcpBenchmarkServer 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 = Netty3TcpBenchmarkServer.class.getName() + ".state";
+
+ private static final String LENGTH_ATTRIBUTE = Netty3TcpBenchmarkServer.class.getName() + ".length";
+
+ private ChannelFactory factory;
+
+ private ChannelGroup allChannels = new DefaultChannelGroup();
+
+ /**
+ * 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);
+ allChannels.add(ctx.getChannel());
+ }
+
+ @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 channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
+ allChannels.remove(ctx.getChannel());
+ }
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
+ e.getCause().printStackTrace();
+ }
+ });
+ }
+ });
+ allChannels.add(bootstrap.bind(new InetSocketAddress(port)));
+ }
+
+ /**
+ * {@inheritedDoc}
+ */
+ public void stop() throws IOException {
+ allChannels.disconnect().awaitUninterruptibly();
+ factory.releaseExternalResources();
+ }
+}