/* Copyright (c) 2004 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.xmlman.test;


import com.ricebridge.xmlman.*;

import org.jostraca.util.*;

import java.util.*;
import java.text.*;
import java.io.*;


/** Create performance test files with random data.
 *    <p>The <b><a href="PerformanceTestFileMaker.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 PerformanceTestFileMaker {

  public static final void main( String[] pArgs ) throws Exception {
    String type = 0<pArgs.length?pArgs[0]:"deep";
    if( "random".equals( type ) ) {
      makeRandomFiles();
    }
    else {
      makeDeepFile();
    }
  }


  public static void makeRandomFiles() throws Exception {
    XmlManager xm = new XmlManager();
    RecordSpec rs = new RecordSpec("/data/row", new String[]{ "int", "@char", "val/@long", "val/double", "val/date", "." } );
    xm.setRecordSpec( rs );

    RandomValueRecordProvider rvp = null;

    for( long i = 1; i <= 6; i++ ) {
      long numrecs = (long) Math.pow(10L, i);

      rvp = new RandomValueRecordProvider( numrecs );
      xm.save( PerformanceTest.findFile("p"+numrecs), rvp );
      System.out.println( ""+numrecs );
    }

  }


  public static void makeDeepFile() throws Exception {
    FileOutputStream fos = new FileOutputStream( PerformanceTest.findFile("deep") );
    PrintWriter      pw  = new PrintWriter(fos);

    pw.println("<?xml version='1.0'?>");
    int levels = 10000;
    for( int i = 0; i < levels; i++ ) {
      pw.print("<e n=\""+i+"\">t"+i);
    }
    for( int i = 0; i < levels; i++ ) {
      pw.print("</e>");
    }
    pw.println();

    pw.close();
  }



  /** Provides random values as data for saving. */
  public static class RandomValueRecordProvider extends RecordProviderSupport {

    private Random iRandom = new Random();
    private long   iNumRecords   = 0;
    private long   iRecordIndex  = 0;
    private int    iNumFields    = 6;

    SimpleDateFormat sdf = new SimpleDateFormat();


    public RandomValueRecordProvider( long pNumRecords ) {
      iNumRecords = pNumRecords;
    }

    
    protected void startProcessImpl() {
      iRecordIndex  = 0;
    }


    protected void endProcessImpl() {
      // do nothing
    }



    protected boolean hasNextRecordImpl() {
      return iRecordIndex < iNumRecords;
    }


    public String[]  nextRecordImpl() {
      String[] rec = new String[iNumFields];
      rec[0] = String.valueOf( iRecordIndex+1 );
      rec[1] = String.valueOf( makeRandomChar() );
      rec[2] = String.valueOf( iRandom.nextLong() );
      rec[3] = String.valueOf( iRandom.nextDouble() );

      Date d = new Date( iRandom.nextLong() );
      rec[4] = sdf.format(d);

      rec[5] = makeRandomString();
      iRecordIndex++;
      return rec;
    }


    public static final char[] sChars = new char[] {
      '\'',' ','!','"','&','$','%','(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';',    
      '<','>','@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W',
      'X','Y','Z','[','\\',']','^','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
      't','u','v','w','x','y','z','{','|','}','`'
    }; 


    public char makeRandomChar() {
      int c = iRandom.nextInt( sChars.length );
      return sChars[c];
    }

    public String makeRandomString() {
      int nc = iRandom.nextInt( sChars.length );
      StringBuffer sb = new StringBuffer();
      for( int cI = 0; cI < nc; cI++ ) {
        sb.append( makeRandomChar() );
      }
      String s = sb.toString();

      // avoid fillbuf crimson bug
      /*
      while( s.endsWith("]") 
             || s.endsWith("[")
             || s.endsWith("<")
             || s.endsWith(">")
             || s.endsWith("&")
             || s.endsWith("\"")
             ) {
        s = s.substring(0,s.length()-1);
      }
      */

      return s;
    }
  }

}





Syntax Highlighting created using the com.Ostermiller.Syntax package.
Thursday, February 23 2006 at 16:47