Package de.netseeker.ejoe.test

Source Code of de.netseeker.ejoe.test.SpeedCompare$Runner

/*********************************************************************
* AdapterSpeedLog.java
* created on 29.07.2006 by netseeker
* $Id: SpeedCompare.java,v 1.2 2007/05/28 14:06:26 netseeker Exp $
* $Log: SpeedCompare.java,v $
* Revision 1.2  2007/05/28 14:06:26  netseeker
* *** empty log message ***
*
* Revision 1.1  2007/05/27 22:13:09  netseeker
* *** empty log message ***
*
* Revision 1.8  2007/03/22 21:01:34  netseeker
* *** empty log message ***
*
* Revision 1.7  2007/02/11 15:42:29  netseeker
* *** empty log message ***
*
* Revision 1.6  2006/11/05 16:59:50  netseeker
* *** empty log message ***
*
* Revision 1.5  2006/10/11 22:40:32  netseeker
* *** empty log message ***
*
* Revision 1.4  2006/08/15 16:48:42  netseeker
* *** empty log message ***
*
* Revision 1.3  2006/08/09 20:13:54  netseeker
* *** empty log message ***
*
* Revision 1.2  2006/08/02 23:03:34  netseeker
* *** empty log message ***
*
* Revision 1.1  2006/07/31 22:29:50  netseeker
* *** empty log message ***
*
*
* ====================================================================
*
*  Copyright 2005-2006 netseeker aka Michael Manske
*
*  Licensed 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.
* ====================================================================
*
* This file is part of the EJOE framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.netseeker.ejoe.EJClient;
import de.netseeker.ejoe.EJServer;

/**
* @author netseeker
* @since 0.3.9.1
*/
public class SpeedCompare
{
    private static String dFormat = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * @param args
     */
    public static void main( String[] args )
    {
        int loops = Integer.parseInt( args[0] );
        boolean inJvm = Boolean.parseBoolean( args[1] );
        int port = Integer.parseInt( args[2] );

        SpeedCompare sc = new SpeedCompare();
        sc.runSpeedCompare( loops, inJvm, port, args[3] );

        System.exit( 0 );
    }

    public void runSpeedCompare( int loops, boolean inJvm, int port, String outFile )
    {
        String header = "loops;start time;end time;duration in ms;average single duration in ms";

        EJServer server = new EJServer( new TestHandler(), port );
        server.enablePersistentConnections( true );
       
        try
        {
            System.out.println( "Starting EJServer..." );
            server.start();
            System.out.println( "Waiting 3 seconds..." );
            Thread.sleep( 3000 );
        }
        catch ( Exception e1 )
        {
            e1.printStackTrace();
            System.exit( -1 );
        }

        EJClient client1 = new EJClient( "localhost", port );
        client1.enablePersistentConnection( true );
        client1.setInJVM( inJvm );
        EJClient client2 = new EJClient( "localhost", port );
        client1.enablePersistentConnection( true );
        client2.setInJVM( inJvm );

        Runnable r1 = new Runner( client1 );
        Runnable r2 = new Runner( client2 );

        Date start = new Date();

        try
        {
            for ( int go = 0; go < loops; go++ )
            {
                try
                {
                    Thread a = new Thread( r1 );
                    Thread b = new Thread( r2 );
                    a.start();
                    b.start();
                    a.join();
                    b.join();
                }
                catch ( Throwable e )
                {
                    e.printStackTrace();
                    break;
                }
            }
        }
        finally
        {
            client1.close();
            client2.close();
        }

        Date end = new Date();

        System.out.println( "Stopping server..." );
        server.stop();

        long duration = end.getTime() - start.getTime();
        DateFormat df = new SimpleDateFormat( dFormat );
        BigDecimal resultF = BigDecimal.valueOf( (duration * 1.00) / (loops * 1.00) )
                .setScale( 2, BigDecimal.ROUND_HALF_UP );
        DecimalFormat dcf = new DecimalFormat( "###.##" );

        try
        {
            System.out.println( "Writing report to file " + outFile );
            boolean append = new File( outFile ).exists();
            BufferedWriter out = new BufferedWriter( new FileWriter( outFile, append ) );
            if ( !append ) out.write( header );
            out.write( "\r\n" );
            out.write( loops + ";" + df.format( start ) + ';' + df.format( end ) + ';' + duration + ";"
                    + dcf.format( resultF ) );
            out.close();
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
    }

    class Runner implements Runnable
    {
        private EJClient client;

        public Runner(EJClient client)
        {
            this.client = client;
        }

        public void run()
        {
            try
            {
                client.execute( new ObjectBean() );
            }
            catch ( Throwable e )
            {
                e.printStackTrace();
            }
        }

    }
}
TOP

Related Classes of de.netseeker.ejoe.test.SpeedCompare$Runner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.