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 2012/09/28 09:13:55 UTC

svn commit: r1391336 - in /mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp: ./ perf/ perf/TcpClient.java perf/TcpServer.java

Author: elecharny
Date: Fri Sep 28 07:13:55 2012
New Revision: 1391336

URL: http://svn.apache.org/viewvc?rev=1391336&view=rev
Log:
added some performance tests for TCP

Added:
    mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/
    mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/
    mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpClient.java
    mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpServer.java

Added: mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpClient.java
URL: http://svn.apache.org/viewvc/mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpClient.java?rev=1391336&view=auto
==============================================================================
--- mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpClient.java (added)
+++ mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpClient.java Fri Sep 28 07:13:55 2012
@@ -0,0 +1,148 @@
+/*
+ *  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.example.tcp.perf;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.core.buffer.IoBuffer;
+import org.apache.mina.core.future.ConnectFuture;
+import org.apache.mina.core.service.IoConnector;
+import org.apache.mina.core.service.IoHandlerAdapter;
+import org.apache.mina.core.session.IdleStatus;
+import org.apache.mina.core.session.IoSession;
+import org.apache.mina.transport.socket.SocketSessionConfig;
+import org.apache.mina.transport.socket.nio.NioSocketConnector;
+
+/**
+ * An UDP client taht just send thousands of small messages to a UdpServer. 
+ * 
+ * This class is used for performance test purposes. It does nothing at all, but send a message
+ * repetitly to a server.
+ * 
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class TcpClient extends IoHandlerAdapter {
+    /** The connector */
+    private IoConnector connector;
+
+    /** The session */
+    private static IoSession session;
+
+    /**
+     * Create the UdpClient's instance
+     */
+    public TcpClient() {
+        connector = new NioSocketConnector();
+
+        connector.setHandler(this);
+        SocketSessionConfig dcfg = (SocketSessionConfig) connector.getSessionConfig();
+
+        ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", TcpServer.PORT));
+
+        connFuture.awaitUninterruptibly();
+
+        session = connFuture.getSession();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
+        cause.printStackTrace();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void messageReceived(IoSession session, Object message) throws Exception {
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void messageSent(IoSession session, Object message) throws Exception {
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void sessionClosed(IoSession session) throws Exception {
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void sessionCreated(IoSession session) throws Exception {
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void sessionOpened(IoSession session) throws Exception {
+    }
+
+    /**
+     * The main method : instanciates a client, and send N messages. We sleep 
+     * between each K messages sent, to avoid the server saturation.
+     * @param args
+     * @throws Exception
+     */
+    public static void main(String[] args) throws Exception {
+        TcpClient client = new TcpClient();
+
+        long t0 = System.currentTimeMillis();
+
+        for (int i = 0; i <= TcpServer.MAX_RECEIVED; i++) {
+            //if (i % 2 == 0) {
+            Thread.sleep(1);
+            //}
+
+            IoBuffer buffer = IoBuffer.allocate(4);
+            buffer.putInt(i);
+            buffer.flip();
+            session.write(buffer);
+
+            if (i % 10000 == 0) {
+                System.out.println("Sent " + i + " messages");
+            }
+        }
+
+        long t1 = System.currentTimeMillis();
+
+        System.out.println("Sent messages delay : " + (t1 - t0));
+
+        Thread.sleep(100000);
+
+        client.connector.dispose(true);
+    }
+}

Added: mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpServer.java
URL: http://svn.apache.org/viewvc/mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpServer.java?rev=1391336&view=auto
==============================================================================
--- mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpServer.java (added)
+++ mina/branches/2.0/mina-example/src/main/java/org/apache/mina/example/tcp/perf/TcpServer.java Fri Sep 28 07:13:55 2012
@@ -0,0 +1,151 @@
+/*
+ *  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.example.tcp.perf;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.mina.core.service.IoHandlerAdapter;
+import org.apache.mina.core.session.IdleStatus;
+import org.apache.mina.core.session.IoSession;
+import org.apache.mina.transport.socket.SocketSessionConfig;
+import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
+
+/**
+ * An TCP server used for performance tests.
+ * 
+ * It does nothing fancy, except receiving the messages, and counting the number of
+ * received messages.
+ * 
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class TcpServer extends IoHandlerAdapter {
+    /** The listening port (check that it's not already in use) */
+    public static final int PORT = 18567;
+
+    /** The number of message to receive */
+    public static final int MAX_RECEIVED = 100000;
+
+    /** The starting point, set when we receive the first message */
+    private static long t0;
+
+    /** A counter incremented for every recieved message */
+    private AtomicInteger nbReceived = new AtomicInteger(0);
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
+        cause.printStackTrace();
+        session.close(true);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void messageReceived(IoSession session, Object message) throws Exception {
+
+        int nb = nbReceived.incrementAndGet();
+
+        if (nb == 1) {
+            t0 = System.currentTimeMillis();
+        }
+
+        if (nb == MAX_RECEIVED) {
+            long t1 = System.currentTimeMillis();
+            System.out.println("-------------> end " + (t1 - t0));
+        }
+
+        if (nb % 10000 == 0) {
+            System.out.println("Received " + nb + " messages");
+        }
+
+        //System.out.println("Message : " + ((IoBuffer) message).getInt());
+
+        //((IoBuffer) message).flip();
+
+        // If we want to test the write operation, uncomment this line
+        session.write(message);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void sessionClosed(IoSession session) throws Exception {
+        System.out.println("Session closed...");
+
+        // Reinitialize the counter and expose the number of received messages
+        System.out.println("Nb message received : " + nbReceived.get());
+        nbReceived.set(0);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void sessionCreated(IoSession session) throws Exception {
+        System.out.println("Session created...");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
+        System.out.println("Session idle...");
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void sessionOpened(IoSession session) throws Exception {
+        System.out.println("Session Opened...");
+    }
+
+    /**
+     * Create the TCP server
+     */
+    public TcpServer() throws IOException {
+        NioSocketAcceptor acceptor = new NioSocketAcceptor();
+        acceptor.setHandler(this);
+
+        // The logger, if needed. Commented atm
+        //DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
+        //chain.addLast("logger", new LoggingFilter());
+
+        SocketSessionConfig scfg = acceptor.getSessionConfig();
+
+        acceptor.bind(new InetSocketAddress(PORT));
+
+        System.out.println("Server started...");
+    }
+
+    /**
+     * The entry point.
+     */
+    public static void main(String[] args) throws IOException {
+        new TcpServer();
+    }
+}