/* Copyright (c) 2004-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 streaming performance test data.
 *    <p>The <b><a href="StreamingTest.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 StreamingTest extends TestCase {

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

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

  public static void main( String[] pArgs ) {
    TestRunner.run( suite() );
  }




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

    try {
        String fn = null;
        LineListener ln = new MeasureStreamLineListener( "primer", 10 );

        // primer
        fn  = "p10";
        cm.load( TestUtil.getTextCsvFile(fn), ln );

        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("MeasureStream");
        stats.add( row );

        cm.getCsvSpec().setFlushEachLine( true );

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

        cm.saveAsLists( TestUtil.getTextCsvFile("stream-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 {
    LineListener ln = new MeasureStreamLineListener( pFileName, pLineCount );

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

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

      pStats.add( row );

      pCsvManager.load( TestUtil.getTextCsvFile( pFileName ), ln );
      display(pCsvManager);
      check( pCsvManager, pLineCount, 0 );
      row.add( new Long( pCsvManager.getTimeTaken() ) );
      System.out.println( "measure-stream:"+rI+":"+pCsvManager.getTimeTaken() );
      System.gc();      
    }
  }





  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() );
  }


  
  public static class MeasureStreamLineListener extends CustomLineListener {

    private String      iMeasureFileName;
    private PrintWriter iMeasureWriter;
    private long        iTimeMillis;
    private Runtime     iRuntime;
    private long        iExpectedLines;


    public MeasureStreamLineListener( String pFileName, long pExpectedLines ) {
      iMeasureFileName = "measure-"+pFileName;
      iRuntime = Runtime.getRuntime();
      iExpectedLines = pExpectedLines;
    }


    public void startProcessImpl() {
      try {
        File mf = TestUtil.getTextCsvFile( iMeasureFileName );
        iMeasureWriter = new PrintWriter( new FileWriter( mf ) );
        iMeasureWriter.println( "Line,Mem,TimeMillis");
        iTimeMillis = new Date().getTime();
      }
      catch( Exception e ) {
        throw new CsvManagerException( e );
      }
    }


    public void endProcessImpl() {
      try {
        iMeasureWriter.close();
      }
      catch( Exception e ) {
        throw new CsvManagerException( e );
      }
    }


    protected void handleBadLineImpl( BadLine pBadLine ) {
      System.out.println( "BADLINE:"+pBadLine );
    }


    protected BadLine handleLineImpl( String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine ) {
      long usedmem = iRuntime.totalMemory() - iRuntime.freeMemory();
      Date now = new Date();
      long tm  = (now.getTime()-iTimeMillis);

      if( iExpectedLines < 10001 || tm > 10 || 0 == (pLineNumber % (iExpectedLines/10000)) ) {
        iMeasureWriter.println( pLineNumber+","+usedmem+","+tm );
      }

      iTimeMillis = now.getTime();
      return null;
    }

  }


}





Syntax Highlighting created using the com.Ostermiller.Syntax package.
Friday, October 13 2006 at 22:45