package com.ricebridge.xmlman.test;
import com.ricebridge.xmlman.*;
import org.jostraca.util.*;
import java.util.*;
import java.text.*;
import java.io.*;
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();
}
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() {
}
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();
return s;
}
}
}