import java.io.*; import java.util.*; /** * (machine_configuration, test_name, parser_name, sample_name, time) * (tp600_jdk13, count, xerces_sax2, list100, 0.3) */ public class Driver { private final static String PROPS_FILE = "exxp.properties"; private final static String RES_PREFIX = "/"; private final static String SAMPLES_DIR = "samples"; private java.text.DecimalFormat df = new java.text.DecimalFormat("##0.000000000"); private Properties props; private Map sampleData = new HashMap(); private Map sampleRepeat = new HashMap(); private boolean verbose; public Driver() { } public void setProperties(Properties p) { this.props = p; String sVerbose = p.getProperty("verbose"); verbose = "yes".equals(sVerbose) || "true".equals(sVerbose) || "1".equals(sVerbose); } public void test() throws Exception { // get parameters String[] testers = parseStrings(props.getProperty("testers", "")); String[] tests = parseStrings(props.getProperty("tests", "")); String machineName = props.getProperty("name"); String fileName = props.getProperty("out"); if(testers == null || testers.length == 0) throw new IllegalArgumentException("no testers property specified in tests property"); if(tests == null || tests.length == 0) throw new IllegalArgumentException("no tests specified in tests property"); if(tests.length > 1 || ! "count".equals(tests[0]) ) throw new IllegalArgumentException("only count test is supported now not "+tests[0]); // redirect output PrintWriter out = null; if(fileName != null) { out = new PrintWriter(new FileWriter(fileName, true), true); if(machineName == null) throw new IllegalArgumentException("name property with system configuration name must be set"); System.err.println("appending output to "+fileName); } else { out = new PrintWriter(System.out, true); if(machineName == null) machineName = "UNKNOWN_SYSTEM"; if(verbose) System.err.println("test output: STDOUT"); } // execute all tests for all parsers for (int p=0; p < testers.length; p++) { try { test(machineName, tests[0], testers[p], out); } catch (UnsupportedOperationException e) { System.err.println(e.getMessage()); continue; } catch (Exception e) { e.printStackTrace(); continue; } } out.flush(); if(fileName != null) { out.close(); } } public void test(String machineName, String testName, String parserName, PrintWriter out) throws Exception { Tester tester = (Tester) getClassForTester(parserName).newInstance(); System.err.println(parserName+": starting test "+testName); tester.setUp(props); String[] samples = parseStrings(props.getProperty("samples", props.getProperty("test."+testName+".samples"))); for (int i=0; i < samples.length; i++) { String data = (String) sampleData.get(samples[i]); // fetching samples content cache if(data == null) { //System.err.println("loading sample: "+samples[i]); if(verbose) System.err.print("loading sample "+samples[i]); try { data = loadSample(getClass(), samples[i]); // try loading directly } catch(IOException ex) { // check for alias String s = props.getProperty("sample."+samples[i]+".filename"); if(s != null) data = loadSample(getClass(), s); } if(data == null) throw new IllegalArgumentException("can't load sample data for "+samples[i]); if(verbose) System.err.println(" loaded "); sampleData.put(samples[i], data); int repeat = 1; try { repeat = Integer.parseInt(props.getProperty("sample."+samples[i]+".repeat","1")); } catch(NumberFormatException ex) { } sampleRepeat.put(samples[i], new Integer(repeat)); } int repeat = ((Integer)sampleRepeat.get(samples[i])).intValue(); String element = props.getProperty("test."+testName+"."+samples[i]+".element", props.getProperty("element")); if(element == null) throw new IllegalArgumentException("in "+testName+" test no element name defined for sample "+samples[i]); String sCount = props.getProperty("test."+testName+"."+samples[i]+".count"); if(sCount == null) throw new IllegalArgumentException("in "+testName+" test no count defined for sample "+samples[i]); int count = -1; try { count = Integer.parseInt(sCount); } catch(NumberFormatException ex) { throw new IllegalArgumentException("in "+testName+" test count must be a number not "+sCount); } if(verbose) System.err.println("testing sample="+samples[i]+" element="+element+ " expected count="+count); TestResult result = tester.testCount(data, repeat, element); if(count != -1 && result.elementCount != count) throw new RuntimeException("test failed expected count "+count+" of elements " +element+" not "+result.elementCount); out.print( machineName + '\t' + testName + '\t' + parserName + '\t' + samples[i] + '\t' + df.format(result.timeSecs) + "\r\n"); out.flush(); } } public Class getClassForTester(String testerName) throws UnsupportedOperationException, ClassNotFoundException { String klassName = props.getProperty("tester."+testerName+".classname"); if (klassName == null) { throw new UnsupportedOperationException(testerName + ": not yet implemented."); } Class klass = Class.forName(klassName); return klass; } public static String[] parseStrings(String s) { return parseStrings(s, " \t\r\n,;"); } public static String[] parseStrings(String s, String delim) { StringTokenizer st = null; if (delim == null) { st = new StringTokenizer(s); } else { st = new StringTokenizer(s, delim+",;:"); } String[] strings = new String[st.countTokens()]; for (int i=0; i< strings.length; i++) { strings[i] = st.nextToken(); } return strings; } public static Properties loadProperties(Class klass, String propertiesName) throws IOException { Properties p = new Properties(); p.load(new StringBufferInputStream(loadData(klass, propertiesName))); return p; } public static String loadSample(Class klass, String sampleName) throws Exception { String data = null; try { data = loadData(klass, SAMPLES_DIR + "/" + sampleName); } catch(IOException ex) { data = loadData(klass, sampleName); } return data; } static char[] readBuf = new char[8*1024]; public static String loadData(Class klass, String name) throws IOException { StringWriter sink = new StringWriter(); InputStream in = null; try { in = new FileInputStream(name); } catch(IOException ex) { //try { // in = new FileInputStream(SAMPLES_PREFIX + "/" + name); //} catch(IOException ex2) { in = klass.getResourceAsStream( name ); if(in == null) { in = klass.getResourceAsStream(RES_PREFIX + name ); } //} } if(in == null) { throw new IOException("can't load "+name); } int i; //while((i = in.read()) != -1) { // sink.write((char)i); //} Reader reader = new InputStreamReader(in); while((i = reader.read(readBuf)) > 0) { sink.write(readBuf, 0 ,i); } sink.close(); return sink.toString(); } public static void loadCmdLine(Properties props, String[] args) { StringBuffer buf = new StringBuffer(); for(int i=0;i 0) { props.put("samples", buf.toString().substring(0, buf.length()-1)); } } public static void usage() { System.out.println( "Usage: java "+Driver.class.getName()+" [-options]\n"+ "where options include:\n"+ " -h help\n"+ " -v verbose\n"+ " -parsers list comma separated list of test names\n"+ " -samples list \n"+ " -out name name of output file with test results\n"+ " -description name \n"+ "The command line arguments override the "+PROPS_FILE+" file." ); System.exit(0); } static public void main(String[] args) throws Exception { System.err.println("Java XML parsers speed test by aslom"); System.err.println("loading "+PROPS_FILE); Properties p = loadProperties(Driver.class, PROPS_FILE); loadCmdLine(p, args); Driver d = new Driver(); d.setProperties(p); System.err.println("executing tests"); d.test(); } }