/* Copyright (c) 2003-2006 Ricebridge. All Rights Reserved.
 *
 * This file is available under the terms and conditions of the
 * Ricebridge "Open Source API" policy; Ricebridge grants use of this
 * copyrighted work under the terms of a BSD-style license only. See
 * http://www.opensource.org/licenses/bsd-license.php for more
 * information.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following
 *  disclaimer in the documentation and/or other materials provided
 *  with the distribution.
 *
 *  - Neither the name of the Ricebridge nor the names of its
 *  contributors may be used to endorse or promote products derived
 *  from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.  
 */

package com.ricebridge.csvman.test;


import com.ricebridge.csvman.*;

import org.jostraca.util.*;

import junit.framework.*;
import junit.textui.*;

import javax.swing.table.*;
import java.sql.ResultSet;
import java.util.*;
import java.io.*;


/** Create performance test data.
 *    <p>The <b><a href="PerformanceTest.java.html">Source Code</a></b> of this Java class 
 *    is available under a <a href="http://www.opensource.org/licenses/bsd-license.php">BSD-style license</a>.</p>
 */
public class PerformanceTest extends TestCase {

  // standard test methods
  
  public PerformanceTest( String pName ) {
    super( pName );
  }

  public static TestSuite suite() {
    return new TestSuite( PerformanceTest.class );
  }

  public static void main( String[] pArgs ) {
    if( 0 < pArgs.length ) {
      sFromMemory = "mem".equals(pArgs[0]);
    }
    TestRunner.run( suite() );
  }


  private static boolean sFromMemory   = false;
  private static String  sFileContents = null;


  public void testFiles() {
    CsvManager cm = new CsvManager();

    try {
      String     fn = null;
      List       d  = null;
      TableModel tm = null;
      ResultSet  rs = null;

      // primer
      fn  = "p10";
      d   = cm.load( getCsvFile(fn) );
      d   = cm.loadAsLists( getCsvFile(fn) );
      tm  = cm.loadTableModel( getCsvFile(fn), false );
      rs  = cm.loadResultSet( getCsvFile(fn), false );

      String[]  files = new String[] { "p10", "p100", "p1000", "p10000", "p100000", /*"p1000000"*/ };
      ArrayList stats = new ArrayList();

      ArrayList row = new ArrayList();
      row.add("FileName");
      row.add("NumLines");
      row.add("Load");
      row.add("LoadAsLists");
      row.add("TableModel");
      row.add("ResultSet");
      stats.add( row );

      cm.getCsvSpec().setFlushEachLine( true );

      for( int fI = 0; fI < files.length; fI++ ) {
        measureFile( cm, files[fI], (long) Math.pow(10, fI+1),  stats );
      }

      cm.saveAsLists( getCsvFile("stats"), stats );
    }
    catch( Exception e ) {
      System.out.println( e.getMessage() );
      e.printStackTrace();
    }
    catch( Error e ) {
      System.out.println( cm.getLineCount() );
      System.out.println( e.getMessage() );
      e.printStackTrace();
    }
  }


  public void measureFile( CsvManager pCsvManager, String pFileName, long pLineCount, ArrayList pStats ) throws Exception {

    if( sFromMemory ) {
      loadCsvFile( pFileName );
    }

    for( int rI = 0; rI < 10; rI++ ) {
      System.out.println( pFileName+":"+rI );

      ArrayList row = new ArrayList();
      row.add( pFileName );
      row.add( new Long(pLineCount) );

      pStats.add( row );

      List d = load( pCsvManager, pFileName );
      display(pCsvManager);
      check( pCsvManager, pLineCount, 0 );
      row.add( new Long( pCsvManager.getTimeTaken() ) );
      System.out.println( "load:"+rI+":"+pCsvManager.getTimeTaken() );
      d = null;
      System.gc();      

      List lald = loadAsLists( pCsvManager, pFileName );
      //display(pCsvManager);
      check( pCsvManager, pLineCount, 0 );
      row.add( new Long( pCsvManager.getTimeTaken() ) );
      System.out.println( "lald:"+rI+":"+pCsvManager.getTimeTaken() );
      lald = null;
      System.gc();

      TableModel tm  = loadTableModel( pCsvManager, pFileName );
      //display(pCsvManager);
      check( pCsvManager, pLineCount, 0 );
      row.add( new Long( pCsvManager.getTimeTaken() ) );
      System.out.println( "tm:"+rI+":"+pCsvManager.getTimeTaken() );
      tm = null;
      System.gc();

      ResultSet rs  = loadResultSet( pCsvManager, pFileName );
      //display(pCsvManager);
      check( pCsvManager, pLineCount, 0 );
      row.add( new Long( pCsvManager.getTimeTaken() ) );
      System.out.println( "rs:"+rI+":"+pCsvManager.getTimeTaken() );
      rs = null;

      System.gc();
    }

    if( sFromMemory ) {
      sFileContents = null;
    }
  }



  public List load( CsvManager pCsvManager, String pFileName ) throws Exception {
    List out = null;
    if( sFromMemory ) {
      out = pCsvManager.loadFromString( sFileContents );
    }
    else {
      out = pCsvManager.load( getCsvFile( pFileName ) );
    }
    return out;
  }


  public List loadAsLists( CsvManager pCsvManager, String pFileName ) throws Exception {
    List out = null;
    if( sFromMemory ) {
      out = pCsvManager.loadAsListsFromString( sFileContents );
    }
    else {
      out = pCsvManager.loadAsLists( getCsvFile( pFileName ) );
    }
    return out;
  }


  public TableModel loadTableModel( CsvManager pCsvManager, String pFileName ) throws Exception {
    TableModel out = null;
    if( sFromMemory ) {
      out = pCsvManager.loadTableModelFromString( sFileContents, false );
    }
    else {
      out = pCsvManager.loadTableModel( getCsvFile( pFileName ), false );
    }
    return out;
  }


  public ResultSet loadResultSet( CsvManager pCsvManager, String pFileName ) throws Exception {
    ResultSet out = null;
    if( sFromMemory ) {
      out = pCsvManager.loadResultSetFromString( sFileContents, false );
    }
    else {
      out = pCsvManager.loadResultSet( getCsvFile( pFileName ), false );
    }
    return out;
  }



  public void loadCsvFile( String pFileName ) throws Exception {
    File csvf = getCsvFile( pFileName );
    BufferedReader br = new BufferedReader( new FileReader( csvf ) );
    StringBuffer sb = new StringBuffer();
    String line = null;
    while( null != ( line = br.readLine() ) ) {
      sb.append( line );
      sb.append( "\n" );
    }
    br.close();
    sFileContents = sb.toString();
    System.out.println( "loaded: "+pFileName  );
  }


  public void check( CsvManager pCsvManager, long pLineCount, long pBadLineCount ) {
    assertEquals( pLineCount, pCsvManager.getLineCount() );
    assertEquals( pBadLineCount, pCsvManager.getBadLineCount() );
  }


  public void display( CsvManager pCsvManager ) {
    System.out.println( "lc:"+pCsvManager.getLineCount()+" blc:"+pCsvManager.getBadLineCount()
                        +" tt:"+pCsvManager.getTimeTaken() +" atpl:"+pCsvManager.getAverageTimePerLineInSeconds());
  }



  public static File getCsvFile( String pName ) throws Exception {
    File csvf = TestUtil.getTextCsvFile( pName );
    if( !csvf.exists() ) {
      csvf = new File( pName+".csv" );
    }
    return csvf;
  }

}





Syntax Highlighting created using the com.Ostermiller.Syntax package.
Sunday, May 27 2007 at 22:08