1 19 package bak.pcj.benchmark; 20 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.Modifier ; 24 import java.lang.reflect.InvocationTargetException ; 25 26 import java.io.Writer ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 30 40 public class BenchmarkRunner { 41 42 43 private boolean verbose = true; 44 45 46 private Report report; 47 48 49 private long relaxTime = 4000L; 50 51 54 public BenchmarkRunner() { 55 report = new Report(); 56 } 57 58 66 public void setVerbose(boolean verbose) 67 { this.verbose = verbose; } 68 69 73 private void relax() { 74 if (verbose) 75 System.out.println("Garbage collecting..."); 76 System.gc(); 77 try { 78 Thread.sleep(relaxTime); 79 } catch (InterruptedException e) { 80 } 81 } 82 83 private String nameOf(Class cls) { 84 String name = cls.getName(); 85 int ldot = name.lastIndexOf('.'); 86 if (ldot != -1) 87 name = name.substring(ldot+1); 88 return name; 89 } 90 91 98 public Report getReport() 99 { return report; } 100 101 113 public void runBenchmark(Benchmark bm, DataSet dataSet) { 114 relax(); 115 116 String classId = bm.getClassId(); 118 String dataSetId = dataSet.getId(); 119 String benchmarkId = nameOf(bm.getClass()); 120 String taskDescription = ""; 121 122 if (verbose) 123 System.out.println("Running " + benchmarkId + "/" + classId + " with data " + dataSetId); 124 125 Method [] methods = bm.getClass().getMethods(); 127 for (int i = 0; i < methods.length; i++) { 128 Method m = methods[i]; 129 if ((m.getModifiers() & (Modifier.PUBLIC|Modifier.ABSTRACT)) == Modifier.PUBLIC && m.getName().startsWith("benchmark") && m.getParameterTypes().length == 1 && (DataSet.class).isAssignableFrom(m.getParameterTypes()[0]) ) { 131 if (verbose) 132 System.out.println(" task: " + m.getName().substring("benchmark".length())); 133 try { 134 taskDescription = (String )m.invoke(bm, new Object []{dataSet}); 135 } catch (IllegalAccessException iae) { 136 throw new RuntimeException (iae.getMessage()); 137 } catch (InvocationTargetException ite) { 138 throw new RuntimeException (ite.getMessage()); 139 } 140 long time = bm.readTimer(); 141 String taskId = m.getName().substring("benchmark".length()); 142 Result result = new Result(benchmarkId, dataSetId, classId, taskId, taskDescription, time); 143 report.addResult(result); 144 } 145 } 146 } 147 148 private static void printUsageAndExit(Throwable e) { 149 System.err.println("Usage: bak.pcj.benchmark.BenchmarkRunner <dataset class> <dataset size> <benchmark class> <output file>"); 150 if (e != null) { 151 System.err.println("An exception was raised:"); 152 e.printStackTrace(); 153 } 154 System.exit(1); 155 } 156 157 168 public static void main(String [] args) { 169 int size = 0; 170 DataSet dataSet = null; 171 Benchmark benchmark = null; 172 Writer out = null; 173 174 if (args.length != 4) 176 printUsageAndExit(null); 177 178 try { 180 size = Integer.parseInt(args[1]); 181 if (size <= 0) 182 printUsageAndExit(null); 183 } catch (NumberFormatException e) { 184 printUsageAndExit(e); 185 } 186 187 try { 189 Class dataSetClass = Class.forName(args[0]); 190 Constructor c = dataSetClass.getConstructor(new Class []{Integer.TYPE}); 191 dataSet = (DataSet)c.newInstance(new Object []{new Integer (size)}); 192 } catch (InstantiationException ie) { 193 printUsageAndExit(ie); 194 } catch (IllegalAccessException iae) { 195 printUsageAndExit(iae); 196 } catch (ClassNotFoundException cnfe) { 197 printUsageAndExit(cnfe); 198 } catch (NoSuchMethodException nsme) { 199 printUsageAndExit(nsme); 200 } catch (InvocationTargetException ite) { 201 printUsageAndExit(ite); 202 } 203 204 try { 206 Class benchmarkClass = Class.forName(args[2]); 207 benchmark = (Benchmark)benchmarkClass.newInstance(); 208 } catch (InstantiationException ie) { 209 printUsageAndExit(ie); 210 } catch (IllegalAccessException iae) { 211 printUsageAndExit(iae); 212 } catch (ClassNotFoundException cnfe) { 213 printUsageAndExit(cnfe); 214 } 215 216 try { 218 out = new FileWriter (args[3]); 219 } catch (IOException e) { 220 printUsageAndExit(e); 221 } 222 223 BenchmarkRunner run = new BenchmarkRunner(); 224 225 run.runBenchmark(benchmark, dataSet); 227 run.report.clearResults(); 228 229 run.runBenchmark(benchmark, dataSet); 231 232 try { 233 run.report.writeResults(out); 234 out.flush(); 235 out.close(); 236 } catch (IOException e) { 237 printUsageAndExit(e); 238 } 239 } 240 241 } | Popular Tags |