KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > RaceTest


1 /*
2  * $Id: RaceTest.java,v 1.3 2002/02/24 02:10:19 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

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 /**
62  * Command-line (offline) generator
63  */

64 public final class RaceTest {
65
66     public static FlashFile parse( String JavaDoc 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 JavaDoc msg ) {
96         System.err.println( msg );
97         help();
98     }
99
100     public static void main( String JavaDoc[] args ) {
101
102         Util.init();
103         Log.setLogToConsole();
104
105         String JavaDoc outFileName = null;
106         String JavaDoc inFileName = null;
107         int threads = 50;
108         int iters = 1;
109         boolean verbose = false;
110         boolean save = false;
111         StandardContext context = new StandardContext();
112         // parse options
113
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 JavaDoc name = args[++i];
120                 String JavaDoc 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 JavaDoc 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 JavaDoc e ) { Log.log(e); }
157         final FlashFile file1 = file2;
158         for( int i=0; i<threads; i++ ) {
159             final int ii = i;
160             Thread JavaDoc st = new Thread JavaDoc() {
161                 public void run() {
162                     for( int j=0; j<myIters; j++ ) {
163                         fobs[ii] = process( file1, myContext, ii, myVerbose );
164 // fobs[ii] = process( myFileName, myContext, ii, myVerbose );
165
}
166                 }
167             };
168             st.start();
169         }
170
171         // compare all fobs
172
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 JavaDoc e ) {
198                     Log.log(e);
199                 }
200             }
201         }
202 // System.exit(0);
203
}
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 JavaDoc e ) {
219              Log.logRB(Resource.UNKNOWNERROR, e);
220          }
221          return null;
222     }
223
224     private static FlashOutput process( String JavaDoc 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 JavaDoc[] {myFileName} );
237          } catch( IVException e ) {
238              Log.log(e);
239          } catch( RuntimeException JavaDoc 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 JavaDoc e ) {}
261         }
262         return fobs[i];
263     }
264
265 }
266
Popular Tags