Hello, I Write a Server Demo use Mina-1.1.2,use the
ObjectSerializationCodecFactory. But When Run This Server,Use a 100 threads
Client to test it’s performance,it’s performance is poor. Server machine
is  2CPU *2.8G,1G memory ,Suse Linux  9. It Tran/sec only 60~80. I past the
code under, who can help me?

 

Then,I Run the HttpServer exsample  from Mina1.1.2 in same machine, I use
the apche ab test it.When I use apace ab in 50 concurrency,it performance
Trans/Sec is 4800,but when I use 100 or more concurrency,it performance is
very poor, only 100~200 trans/sec. I cannot understand it ,who can tell me
why?

 

-------------------------------------------Server:--------------------------
-----------------------

 

Main.java

 

/*

 *  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 test.mina.server;

 

import java.net.InetSocketAddress;

import java.util.concurrent.Executors;

 

import org.apache.mina.common.DefaultIoFilterChainBuilder;

import org.apache.mina.common.IoAcceptor;

import org.apache.mina.filter.LoggingFilter;

import org.apache.mina.filter.codec.ProtocolCodecFilter;

import
org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;

import org.apache.mina.filter.executor.ExecutorFilter;

import org.apache.mina.transport.socket.nio.SocketAcceptor;

import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;

 

/**

 * (<b>Entry point</b>) Echo server

 * 

 * @author The Apache Directory Project ([EMAIL PROTECTED])

 * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (旮?, 13  7��?
2007) $

 */

public class Main {

    /** Choose your favorite port number. */

    private static final int PORT = 8282;

 

    /** Set this to true if you want to make the server SSL */

    private static final boolean USE_SSL = false;

 

    public static void main(String[] args) throws Exception {

//        IoAcceptor acceptor = new SocketAcceptor();

        //IoAcceptorConfig config = new SocketAcceptorConfig();

        IoAcceptor acceptor = new SocketAcceptor(4,
Executors.newCachedThreadPool());

        SocketAcceptorConfig config = new SocketAcceptorConfig();

        

//        config.setReuseAddress(true);

        config.setBacklog(30);

        

//        Storager.create();

//        config.getFilterChain().addLast(

//                "protocolFilter",

//                new ProtocolCodecFilter(

//                             new TestProtocolCodecFactory(false)));

 

      config.getFilterChain().addLast(

      "protocolFilter",

      new ProtocolCodecFilter(

              new ObjectSerializationCodecFactory()));

        

        

        

        DefaultIoFilterChainBuilder chain = config.getFilterChain();

 

        chain.addLast("threadPool", new
ExecutorFilter(Executors.newCachedThreadPool()) );

 

//        addLogger(chain);

 

        // Bind

        acceptor.bind(new InetSocketAddress(PORT), new
EchoProtocolHandler(),

                config);

 

        System.out.println("Listening on port " + PORT);

    }

 

 

 

    private static void addLogger(DefaultIoFilterChainBuilder chain)

            throws Exception {

        chain.addLast("logger", new LoggingFilter());

        System.out.println("Logging ON");

    }

}

 

Handler:

 

/*

 *  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 test.mina.server;

 

import java.util.Properties;

 

import org.apache.mina.common.ByteBuffer;

import org.apache.mina.common.IdleStatus;

import org.apache.mina.common.IoHandler;

import org.apache.mina.common.IoHandlerAdapter;

import org.apache.mina.common.IoSession;

import org.apache.mina.common.TransportType;

 

import org.apache.mina.transport.socket.nio.SocketSessionConfig;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

import test.mina.TestRequest;

import test.mina.TestResponse;

 

public class EchoProtocolHandler extends IoHandlerAdapter {

    private static final Logger log = LoggerFactory

            .getLogger(EchoProtocolHandler.class);

 

    public void sessionCreated(IoSession session) {

        if (session.getTransportType() == TransportType.SOCKET) {

           SocketSessionConfig config = (SocketSessionConfig)
session.getConfig(); 

           

             config.setReceiveBufferSize(2048);

             config.setSoLinger(0);

        }

 

        session.setIdleTime(IdleStatus.BOTH_IDLE, 10);

 

    }

 

    public void sessionIdle(IoSession session, IdleStatus status) {

        log.info("*** IDLE #" + session.getIdleCount(IdleStatus.BOTH_IDLE)

                + " ***");

    }

 

    public void exceptionCaught(IoSession session, Throwable cause) {

//        cause.printStackTrace();

        session.close();

    }

 

    public void messageReceived(IoSession session, Object message)

            throws Exception {

    

 

    TestRequest req = (TestRequest)message;

    Processor p = new TESTProcessor();

       

        TestResponse res = new TestResponse(0,"OK");// p.process( req );

        session.write(res);

        

        session.close();

        

    }

}

 

TestRequest:

package test.mina;

 

 

import java.io.Serializable;

import java.util.Enumeration;

import java.util.Properties;

 

public class TestRequest implements Serializable {

         

         private String type;

         private Properties data;

         

         /**

          * @param type

          */

         public TestRequest(String type) {

                   this.type = type;

         }

 

         /**

          * @param type

          * @param data

          */

         public TestRequest(String type, Properties data) {

                   this.type = type;

                   this.data = data;

         }

 

         public Properties getData() {

                   return data;

         }

 

         public void setData(Properties data) {

                   this.data = data;

         }

 

         public String getType() {

                   return type;

         }

         

         public void setKeyValue(String key,String value){

                   if(data == null)

                            data = new Properties();

                   data.setProperty( key,value  );

         }

         

         public String getKeyValue(String key){

                   if(data==null)return null;

                   return data.getProperty(key);

                   

         }

 

         @Override

         public String toString() {

                   StringBuffer buff= new StringBuffer();

                   buff.append( "{[TestRequest]" );

                   buff.append("[request type:" + type + "]");

                   buff.append("[request data:");

                   if( data!=null ){

                            buff.append( data );

                   }

                   buff.append("]");

 

                   buff.append("}");

                   return buff.toString();

                   

                   

                   

         }

         

         

         

 

}

 

TestResponse:

 

package test.mina;

 

import java.io.Serializable;

import java.util.Properties;

 

public class TestResponse implements Serializable {

         

         private int err=0;

         private String msg = "OK";

         

         private Properties data;

 

         public TestResponse(int err, String msg, Properties data) {

                   this.err = err;

                   this.msg = msg;

                   this.data = data;

         }

         

         

         public TestResponse(int err, String msg){

                   this(err,msg,null);

         }

 

 

         public Properties getData() {

                   return data;

         }

 

 

         public void setData(Properties data) {

                   this.data = data;

         }

 

 

         public int getErr() {

                   return err;

         }

 

 

         public String getMsg() {

                   return msg;

         }

         

 

         

         public String toString() {

                   StringBuffer buff= new StringBuffer();

                   buff.append( "{[TestResponse]" );

                   buff.append("[response err:" + err + "]");

                   buff.append("[response msg:" + msg + "]");

                   buff.append("[response data:");

                   if( data!=null ){

                            buff.append( data );

                   }

                   buff.append("]");

                   

 

                   buff.append("}");

                   return buff.toString();

                   

                   

                   

         }        

 

}

 

--------------------------------Client:-----------------------------------

TestSign:

 

package test.thread;

 

import java.io.ByteArrayInputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.PrintWriter;

import java.security.KeyStore;

import java.security.PrivateKey;

import java.security.Security;

import java.util.Enumeration;

 

//import cn.com.infosec.jce.provider.InfosecProvider;

 

/*

 * Created on 2005-10-18

 *

 * TODO To change the template for this generated file go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

 

/**

 * @author lixiangfeng

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class TestSign {

 

         

         private static int finiCount = 0;

         private static int failCount = 0;

         public static Object synObj = new Object();

         public static Object synObj2 = new Object();

         public static Object synObj3 = new Object();

         public static int threadCount = 0;

         public static void main(String[] args) {

                   

                   try {

                            int uCount = Integer.parseInt(args[0]);

                            int uTime = Integer.parseInt(args[1]);

 

 

                            FileOutputStream fos = new FileOutputStream(
"sign.log" );

                            PrintWriter pr = new PrintWriter( fos );

                            

                            TestSignThread.init();

                            

                            

                            for (int i = 0; i < uCount; i++) {

                                     Thread th = new Thread(new
TestSignThread( uTime, pr ));

                                     th.start();

                            }

                            

                            threadCount = uCount;

                            System.out.println("testcount:" + threadCount);

                            System.out.println( "run time       :" + uTime +
"s" );

                            System.out.println("test running,please
wait...");

                            while( threadCount>0){

                                     Thread.sleep( 1000 );

                            }

                            

                            System.out.println( "test finish......" );

                            System.out.println( "finish count   :" +
finiCount );

                            System.out.println( "failed count   :" +
failCount );

                            System.out.println( "speed:" + (finiCount/uTime)
);

                            

                            

                            

                   } catch (Exception e) {

                            e.printStackTrace();

                   }

         }

         

         

         public static void incSucc(){

                   synchronized(synObj){

                            finiCount++;

                   }

         }

         

         public static void incFail(){

                   synchronized(synObj2){

                            failCount++;

                   }

         }        

         

         public static void stopThread(){

                   synchronized(synObj3){

                            threadCount--;

                   }

         }

         

         

}

 

 

TestThread:

 

package test.thread;

 

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.ObjectOutputStream;

import java.io.PrintWriter;

import java.net.InetSocketAddress;

import java.net.Socket;

import java.security.PrivateKey;

import java.security.Security;

import java.security.Signature;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Properties;

import java.util.TimeZone;

 

import org.apache.mina.common.ConnectFuture;

import org.apache.mina.common.IoSession;

import org.apache.mina.common.RuntimeIOException;

import org.apache.mina.filter.codec.ProtocolCodecFilter;

import
org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;

import org.apache.mina.transport.socket.nio.SocketConnector;

import org.apache.mina.transport.socket.nio.SocketConnectorConfig;

 

import test.mina.TestRequest;

import test.mina.client.ClientSessionHandler;

 

//import Test.TestResult;

//

//import cn.com.infosec.jce.provider.InfosecProvider;

//import cn.com.infosec.netcert.crypto.CryptoHandler;

//import cn.com.infosec.netcert.rads.CertManager;

//import cn.com.infosec.netcert.rads.SysProperty;

//import cn.com.infosec.netcert.rads.exception.CAException;

//import cn.com.infosec.netcert.rads.exception.RAException;

//import cn.com.infosec.netcert.resource.PropertiesKeysRes;

 

/*

 * Created on 2005-10-18

 *

 * TODO To change the template for this generated file go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

 

/**

 * @author lixiangfeng

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class TestSignThread implements Runnable {

 

 

         public long endTime = System.currentTimeMillis();

         //public PrivateKey prik = null;

         public PrintWriter pr = null;

         public TestSignThread( int time, PrintWriter pr ){

                   endTime += time * 1000;

                   //this.prik = prik;

                   this.pr = pr;

         }

         

         private static String basedn = "o=infosec";

         private static String template = "EE_SIGNING";

         

         

         public static void init() throws Exception{

                   

 

         }

         

         public void run() {

         

        SocketConnectorConfig cfg = new SocketConnectorConfig();

        cfg.setConnectTimeout(30);

        cfg.getSessionConfig().setSoLinger(0);

 

            cfg.getFilterChain().addLast(

                    "codec",

                    new ProtocolCodecFilter(

                            new ObjectSerializationCodecFactory()));


                   

                   while( true ){

                            if(endTime < System.currentTimeMillis() ){

                                     TestSign.stopThread();

                                     break;

                            }

                            

                            

 

                                    SocketConnector connector = new
SocketConnector();

                                    connector.setWorkerTimeout(1);

 

                                    IoSession session=null;

                                        try {

                                            ConnectFuture future =
connector.connect(new InetSocketAddress(

                                                    "192.168.0.111", 8282),
new ClientSessionHandler(), cfg);

 

                                            future.join();

                                            session = future.getSession();

                                            session.getCloseFuture().join();

                                            TestSign.incSucc();

                                            //break;

                                        } catch (Exception e) {

                                            e.printStackTrace( pr );

                                            TestSign.incFail();

                                        }

 

 

                            

         

                   }

 

         }

 

}

 

ClientHandler:

 

/*

 *  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 test.mina.client;

 

import java.util.Properties;

 

import org.apache.mina.common.IoHandler;

import org.apache.mina.common.IoHandlerAdapter;

import org.apache.mina.common.IoSession;

import org.apache.mina.transport.socket.nio.SocketSessionConfig;

 

import test.mina.TestRequest;

import test.mina.TestResponse;

 

/**

 * [EMAIL PROTECTED] IoHandler} for SumUp client.

 * 

 * @author The Apache Directory Project ([EMAIL PROTECTED])

 * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (旮?, 13  7��?
2007) $

 */

public class ClientSessionHandler extends IoHandlerAdapter {

    //private final int[] values;

 

    private boolean finished;

 

 

    public boolean isFinished() {

        return finished;

    }

 

    public void sessionOpened(IoSession session) {

      

//             System.out.println( "Thread" + Thread.currentThread() +  "
start at:" + System.currentTimeMillis()   );

//             SocketSessionConfig config = session.getConfig();

//             config.

    //System.out.println( session.getConfig()   );

    SocketSessionConfig config = (SocketSessionConfig)session.getConfig();

    config.setSoLinger(0);

    TestRequest req = new TestRequest("TESTMINA");

    Properties p1 = new Properties();

    p1.setProperty("CARDSN","1111000000000068993");

    req.setData(p1);

    session.write(req);

    

    

    }

 

    public void messageReceived(IoSession session, Object message) {

        // server only sends ResultMessage. otherwise, we will have to
identify

        // its type using instanceof operator.

        TestResponse res = (TestResponse)message;

//        System.out.println( res );

    

        session.close();

    finished = true;

//             System.out.println( "Thread" + Thread.currentThread() +  "
finish at:" + System.currentTimeMillis()   );

 

    }

 

    public void exceptionCaught(IoSession session, Throwable cause) {

        session.close();

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Reply via email to