001: /*
002: * $Id: RaceTest.java,v 1.3 2002/02/24 02:10:19 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash;
052:
053: import java.io.*;
054: import java.util.*;
055:
056: import org.openlaszlo.iv.flash.api.*;
057: import org.openlaszlo.iv.flash.parser.*;
058: import org.openlaszlo.iv.flash.util.*;
059: import org.openlaszlo.iv.flash.context.*;
060:
061: /**
062: * Command-line (offline) generator
063: */
064: public final class RaceTest {
065:
066: public static FlashFile parse(String name) throws IVException,
067: FileNotFoundException {
068: return FlashFile.parse(name);
069: }
070:
071: public static void process(FlashFile file, Context context)
072: throws IVException {
073: file.processFile(context);
074: }
075:
076: public static FlashOutput generate(FlashFile file)
077: throws IVException {
078: return file.generate();
079: }
080:
081: public static void help() {
082: System.err.println("JGenerator Race Condition Test Version "
083: + Util.getVersion());
084: System.err
085: .println("Copyright (c) Dmitry Skavish, 2000. All rights reserved.");
086: System.err.println("");
087: System.err.println("Usage: racetest [options] <filename.swt>");
088: System.err.println("");
089: System.err.println("Options:");
090: System.err
091: .println(" -help displays usage text");
092: System.err
093: .println(" -param <name> <value> specifies a named parameter");
094: System.err
095: .println(" -threads <num> number of created threads (50 default)");
096: System.err
097: .println(" -iters <num> number of iterations in each thread (1 default)");
098: System.err.println(" -verbose verbose output");
099: System.err.println(" -save save output");
100: System.err.println("");
101: System.exit(1);
102: }
103:
104: public static void err(String msg) {
105: System.err.println(msg);
106: help();
107: }
108:
109: public static void main(String[] args) {
110:
111: Util.init();
112: Log.setLogToConsole();
113:
114: String outFileName = null;
115: String inFileName = null;
116: int threads = 50;
117: int iters = 1;
118: boolean verbose = false;
119: boolean save = false;
120: StandardContext context = new StandardContext();
121: // parse options
122: int l = args.length - 1;
123: for (int i = 0; i <= l; i++) {
124: if (args[i].equals("-help")) {
125: help();
126: } else if (args[i].equals("-param")) {
127: if (i + 2 > l)
128: err("Error declaring parameter");
129: String name = args[++i];
130: String value = args[++i];
131: if (value.charAt(0) == '"'
132: && value.charAt(value.length() - 1) == '"') {
133: value = value.substring(1, value.length() - 1);
134: }
135: context.setValue(name, value);
136: } else if (args[i].equals("-threads")) {
137: if (i + 1 > l)
138: err("Number of threads is not specified");
139: threads = Util.toInt(args[++i], 50);
140: } else if (args[i].equals("-iters")) {
141: if (i + 1 > l)
142: err("Number of iterations is not specified");
143: iters = Util.toInt(args[++i], 1);
144: } else if (args[i].equals("-verbose")) {
145: verbose = true;
146: } else if (args[i].equals("-save")) {
147: save = true;
148: } else {
149: inFileName = args[i];
150: if (i != l)
151: err("Too many parameters");
152: }
153: }
154:
155: if (inFileName == null)
156: err("Input file is not specified");
157: if (outFileName == null) {
158: if (inFileName.endsWith(".swt")) {
159: outFileName = inFileName.substring(0, inFileName
160: .length() - 3)
161: + "swf";
162: } else {
163: outFileName = inFileName + ".swf";
164: }
165: }
166:
167: final String myFileName = inFileName;
168: final Context myContext = context;
169: final boolean myVerbose = verbose;
170: final int myIters = iters;
171: final FlashOutput[] fobs = new FlashOutput[threads];
172: FlashFile file2 = null;
173: try {
174: file2 = parse(myFileName);
175: } catch (Exception e) {
176: Log.log(e);
177: }
178: final FlashFile file1 = file2;
179: for (int i = 0; i < threads; i++) {
180: final int ii = i;
181: Thread st = new Thread() {
182: public void run() {
183: for (int j = 0; j < myIters; j++) {
184: fobs[ii] = process(file1, myContext, ii,
185: myVerbose);
186: // fobs[ii] = process( myFileName, myContext, ii, myVerbose );
187: }
188: }
189: };
190: st.start();
191: }
192:
193: // compare all fobs
194: if (verbose)
195: System.out.println("Comparing fobs ... ");
196: boolean f = true;
197: FlashOutput fob = getFob(fobs, 0);
198: for (int i = 1; i < threads; i++) {
199: FlashOutput fb = getFob(fobs, i);
200: if (!compare(fob, fb)) {
201: if (verbose)
202: System.out.println("#0 and #" + i
203: + " are not equal");
204: f = false;
205: } else {
206: if (verbose)
207: System.out.println("#0 and #" + i + " are equal");
208: }
209: }
210: if (f) {
211: System.out.println("Comparing ok");
212: } else {
213: System.out.println("Comparing failed");
214: }
215: if (save) {
216: System.out.println("Saving ...");
217: for (int i = 0; i < threads; i++) {
218: FlashOutput fb = getFob(fobs, i);
219: try {
220: BufferedOutputStream bos = new BufferedOutputStream(
221: new FileOutputStream(i + "_" + outFileName));
222: bos.write(fb.getBuf(), 0, fb.getSize());
223: bos.close();
224: } catch (Exception e) {
225: Log.log(e);
226: }
227: }
228: }
229: // System.exit(0);
230: }
231:
232: private static FlashOutput process(FlashFile file1,
233: Context myContext, int ii, boolean v) {
234: try {
235: long startTime = System.currentTimeMillis();
236: if (v)
237: System.out.println("Copying thread #" + ii);
238: FlashFile file = file1.copyFile();
239: if (v)
240: System.out.println("Processing thread #" + ii);
241: process(file, myContext);
242: if (v)
243: System.out.println("Generating thread #" + ii);
244: FlashOutput fob = generate(file);
245: if (v)
246: System.out.println("Done thread #" + ii
247: + " processing time is: "
248: + (System.currentTimeMillis() - startTime)
249: + "ms");
250: return fob;
251: } catch (IVException e) {
252: Log.log(e);
253: } catch (RuntimeException e) {
254: Log.logRB(Resource.UNKNOWNERROR, e);
255: }
256: return null;
257: }
258:
259: private static FlashOutput process(String myFileName,
260: Context myContext, int ii, boolean v) {
261: try {
262: long startTime = System.currentTimeMillis();
263: if (v)
264: System.out.println("Parsing thread #" + ii);
265: FlashFile file = parse(myFileName);
266: if (v)
267: System.out.println("Processing thread #" + ii);
268: process(file, myContext);
269: if (v)
270: System.out.println("Generating thread #" + ii);
271: FlashOutput fob = generate(file);
272: if (v)
273: System.out.println("Done thread #" + ii
274: + " processing time is: "
275: + (System.currentTimeMillis() - startTime)
276: + "ms");
277: return fob;
278: } catch (FileNotFoundException e) {
279: Log.logRB(Resource.FILENOTFOUND,
280: new Object[] { myFileName });
281: } catch (IVException e) {
282: Log.log(e);
283: } catch (RuntimeException e) {
284: Log.logRB(Resource.UNKNOWNERROR, e);
285: }
286: return null;
287: }
288:
289: private static boolean compare(FlashOutput fob1, FlashOutput fob2) {
290: int size = fob1.getSize();
291: if (size != fob2.getSize())
292: return false;
293: byte[] buf1 = fob1.getBuf();
294: byte[] buf2 = fob2.getBuf();
295: for (int i = 0; i < size; i++) {
296: if (buf1[i] != buf2[i])
297: return false;
298: }
299: return true;
300: }
301:
302: private static FlashOutput getFob(FlashOutput[] fobs, int i) {
303: while (fobs[i] == null) {
304: try {
305: Thread.sleep(10);
306: } catch (InterruptedException e) {
307: }
308: }
309: return fobs[i];
310: }
311:
312: }
|