1 50 51 package org.openlaszlo.iv.flash; 52 53 import java.io.*; 54 import java.util.*; 55 56 import org.openlaszlo.iv.flash.api.*; 57 import org.openlaszlo.iv.flash.parser.*; 58 import org.openlaszlo.iv.flash.util.*; 59 import org.openlaszlo.iv.flash.context.*; 60 61 64 public final class RaceTest { 65 66 public static FlashFile parse( String name ) throws IVException, FileNotFoundException { 67 return FlashFile.parse( name ); 68 } 69 70 public static void process( FlashFile file, Context context ) throws IVException { 71 file.processFile(context); 72 } 73 74 public static FlashOutput generate( FlashFile file ) throws IVException { 75 return file.generate(); 76 } 77 78 public static void help() { 79 System.err.println( "JGenerator Race Condition Test Version "+Util.getVersion() ); 80 System.err.println( "Copyright (c) Dmitry Skavish, 2000. All rights reserved." ); 81 System.err.println( "" ); 82 System.err.println( "Usage: racetest [options] <filename.swt>" ); 83 System.err.println( "" ); 84 System.err.println( "Options:" ); 85 System.err.println( " -help displays usage text" ); 86 System.err.println( " -param <name> <value> specifies a named parameter" ); 87 System.err.println( " -threads <num> number of created threads (50 default)" ); 88 System.err.println( " -iters <num> number of iterations in each thread (1 default)" ); 89 System.err.println( " -verbose verbose output" ); 90 System.err.println( " -save save output" ); 91 System.err.println( "" ); 92 System.exit(1); 93 } 94 95 public static void err( String msg ) { 96 System.err.println( msg ); 97 help(); 98 } 99 100 public static void main( String [] args ) { 101 102 Util.init(); 103 Log.setLogToConsole(); 104 105 String outFileName = null; 106 String inFileName = null; 107 int threads = 50; 108 int iters = 1; 109 boolean verbose = false; 110 boolean save = false; 111 StandardContext context = new StandardContext(); 112 int l = args.length-1; 114 for( int i=0; i<=l; i++ ) { 115 if( args[i].equals("-help") ) { 116 help(); 117 } else if( args[i].equals("-param") ) { 118 if( i+2 > l ) err( "Error declaring parameter" ); 119 String name = args[++i]; 120 String value = args[++i]; 121 if( value.charAt(0) == '"' && value.charAt( value.length()-1 ) == '"' ) { 122 value = value.substring(1, value.length()-1); 123 } 124 context.setValue(name, value); 125 } else if( args[i].equals("-threads") ) { 126 if( i+1 > l ) err( "Number of threads is not specified" ); 127 threads = Util.toInt( args[++i], 50 ); 128 } else if( args[i].equals("-iters") ) { 129 if( i+1 > l ) err( "Number of iterations is not specified" ); 130 iters = Util.toInt( args[++i], 1 ); 131 } else if( args[i].equals("-verbose") ) { 132 verbose = true; 133 } else if( args[i].equals("-save") ) { 134 save = true; 135 } else { 136 inFileName = args[i]; 137 if( i != l ) err( "Too many parameters" ); 138 } 139 } 140 141 if( inFileName == null ) err( "Input file is not specified" ); 142 if( outFileName == null ) { 143 if( inFileName.endsWith(".swt") ) { 144 outFileName = inFileName.substring(0, inFileName.length()-3)+"swf"; 145 } else { 146 outFileName = inFileName+".swf"; 147 } 148 } 149 150 final String myFileName = inFileName; 151 final Context myContext = context; 152 final boolean myVerbose = verbose; 153 final int myIters = iters; 154 final FlashOutput[] fobs = new FlashOutput[threads]; 155 FlashFile file2 = null; 156 try { file2 = parse( myFileName ); } catch( Exception e ) { Log.log(e); } 157 final FlashFile file1 = file2; 158 for( int i=0; i<threads; i++ ) { 159 final int ii = i; 160 Thread st = new Thread () { 161 public void run() { 162 for( int j=0; j<myIters; j++ ) { 163 fobs[ii] = process( file1, myContext, ii, myVerbose ); 164 } 166 } 167 }; 168 st.start(); 169 } 170 171 if( verbose ) System.out.println( "Comparing fobs ... " ); 173 boolean f = true; 174 FlashOutput fob = getFob( fobs, 0 ); 175 for( int i=1; i<threads; i++ ) { 176 FlashOutput fb = getFob( fobs, i ); 177 if( !compare( fob, fb ) ) { 178 if( verbose ) System.out.println( "#0 and #"+i+" are not equal" ); 179 f = false; 180 } else { 181 if( verbose ) System.out.println( "#0 and #"+i+" are equal" ); 182 } 183 } 184 if( f ) { 185 System.out.println( "Comparing ok" ); 186 } else { 187 System.out.println( "Comparing failed" ); 188 } 189 if( save ) { 190 System.out.println( "Saving ..." ); 191 for( int i=0; i<threads; i++ ) { 192 FlashOutput fb = getFob( fobs, i ); 193 try { 194 BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( i+"_"+outFileName ) ); 195 bos.write( fb.getBuf(), 0, fb.getSize() ); 196 bos.close(); 197 } catch( Exception e ) { 198 Log.log(e); 199 } 200 } 201 } 202 } 204 205 private static FlashOutput process( FlashFile file1, Context myContext, int ii, boolean v ) { 206 try { 207 long startTime = System.currentTimeMillis(); 208 if( v ) System.out.println( "Copying thread #"+ii ); 209 FlashFile file = file1.copyFile(); 210 if( v ) System.out.println( "Processing thread #"+ii ); 211 process(file,myContext); 212 if( v ) System.out.println( "Generating thread #"+ii ); 213 FlashOutput fob = generate(file); 214 if( v ) System.out.println( "Done thread #"+ii+" processing time is: "+(System.currentTimeMillis()-startTime)+"ms" ); 215 return fob; 216 } catch( IVException e ) { 217 Log.log(e); 218 } catch( RuntimeException e ) { 219 Log.logRB(Resource.UNKNOWNERROR, e); 220 } 221 return null; 222 } 223 224 private static FlashOutput process( String myFileName, Context myContext, int ii, boolean v ) { 225 try { 226 long startTime = System.currentTimeMillis(); 227 if( v ) System.out.println( "Parsing thread #"+ii ); 228 FlashFile file = parse( myFileName ); 229 if( v ) System.out.println( "Processing thread #"+ii ); 230 process(file,myContext); 231 if( v ) System.out.println( "Generating thread #"+ii ); 232 FlashOutput fob = generate(file); 233 if( v ) System.out.println( "Done thread #"+ii+" processing time is: "+(System.currentTimeMillis()-startTime)+"ms" ); 234 return fob; 235 } catch( FileNotFoundException e ) { 236 Log.logRB( Resource.FILENOTFOUND, new Object [] {myFileName} ); 237 } catch( IVException e ) { 238 Log.log(e); 239 } catch( RuntimeException e ) { 240 Log.logRB(Resource.UNKNOWNERROR, e); 241 } 242 return null; 243 } 244 245 private static boolean compare( FlashOutput fob1, FlashOutput fob2 ) { 246 int size = fob1.getSize(); 247 if( size != fob2.getSize() ) return false; 248 byte[] buf1 = fob1.getBuf(); 249 byte[] buf2 = fob2.getBuf(); 250 for( int i=0; i<size; i++ ) { 251 if( buf1[i] != buf2[i] ) return false; 252 } 253 return true; 254 } 255 256 private static FlashOutput getFob( FlashOutput[] fobs, int i ) { 257 while( fobs[i] == null ) { 258 try { 259 Thread.sleep(10); 260 } catch( InterruptedException e ) {} 261 } 262 return fobs[i]; 263 } 264 265 } 266 | Popular Tags |