1 19 package bak.pcj.benchmark; 20 21 import java.util.List ; 22 import java.util.ArrayList ; 23 import java.util.TreeMap ; 24 import java.util.Map ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.Collections ; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import java.util.TreeSet ; 31 32 import java.io.Writer ; 33 import java.io.Reader ; 34 import java.io.IOException ; 35 36 45 public class Report { 46 47 48 private List results; 49 50 51 private Map properties; 52 53 56 public Report() { 57 results = new ArrayList (); 58 properties = new TreeMap (); 59 } 60 61 70 public void addResult(Result result) { 71 if (result == null) 72 throw new NullPointerException (); 73 results.add(result); 74 } 75 76 83 public Collection getResults() { 84 return Collections.unmodifiableCollection(results); 85 } 86 87 90 public void clearResults() { 91 results.clear(); 92 } 93 94 95 109 public void putProperty(String key, String value) { 110 if (key == null) 111 throw new NullPointerException (); 112 if (value == null) 113 properties.remove(key); 114 else 115 properties.put(key, value); 116 } 117 118 131 public String getProperty(String key) { 132 if (key == null) 133 throw new NullPointerException (); 134 return (String )properties.get(key); 135 } 136 137 141 private String readLine(Reader in) throws IOException { 142 StringBuffer s = new StringBuffer (); 143 int c; 144 while ((c = in.read()) != -1 && c != '\n') 145 s.append((char)c); 146 if (s.length() == 0 && c == -1) 147 return null; 148 return s.toString(); 149 } 150 151 private static String [] split(String s, char c) { 152 ArrayList a = new ArrayList (); 153 int p = 0; 154 int len = s.length(); 155 while (p < len) { 156 if (a.size() == 0) 157 a.add(""); 158 char pc = s.charAt(p); 159 if (pc == c) 160 a.add(""); 161 else 162 a.set(a.size()-1, ((String )a.get(a.size()-1))+pc); 163 p++; 164 } 165 String [] sa = new String [a.size()]; 166 a.toArray(sa); 167 return sa; 168 } 169 170 179 public void readResults(Reader in) throws IOException { 180 String s; 181 int ptr, nptr, len; 182 while ((s = readLine(in)) != null) { 183 String [] fields = split(s, ';'); 184 Result result = new Result( 185 fields[0], fields[1], fields[2], fields[3], fields[4], Long.parseLong(fields[5]) ); 192 addResult(result); 193 } 194 } 195 196 205 public void writeResults(Writer out) throws IOException { 206 Iterator i = results.iterator(); 207 while (i.hasNext()) { 208 Result result = (Result)i.next(); 209 out.write(result.getBenchmarkId()); 210 out.write(';'); 211 out.write(result.getDataSetId()); 212 out.write(';'); 213 out.write(result.getClassId()); 214 out.write(';'); 215 out.write(result.getTaskId()); 216 out.write(';'); 217 out.write(result.getTaskDescription()); 218 out.write(';'); 219 out.write(String.valueOf(result.getTime())); 220 out.write('\n'); 221 } 222 } 223 224 228 private static String stylesheet = 229 "body {\n" + 230 " margin-left: 2em;\n" + 231 " margin-right: 2em;\n" + 232 "}\n" + 233 "\n" + 234 "h1, h2, h3, caption, th {\n" + 235 " font-family: helvetica, arial, verdana;\n" + 236 "}\n" + 237 "\n" + 238 "h1 {\n" + 239 " font-size: 24pt;\n" + 240 " font-weight: bold;\n" + 241 "}\n" + 242 "\n" + 243 "h2 {\n" + 244 " font-size: 16pt;\n" + 245 "}\n" + 246 "\n" + 247 "thead {\n" + 248 " background-color: #CCCCFF;\n" + 249 "}\n" + 250 ""; 251 252 264 public void writeHTML(Writer out) throws IOException { 265 266 Set sortedResults = new TreeSet (new ResultComparator()); 267 sortedResults.addAll(results); 268 String title = "PCJ Benchmark Results"; 269 if (getProperty("report.title") != null) 270 title += " - " + getProperty("report.title"); 271 272 out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"); 273 out.write("\"http://www.w3.org/TR/html4/strict.dtd\">\n"); 274 out.write("<html lang=\"en\">\n"); 275 276 out.write("<head>\n"); 277 out.write(" <title>" + title + "</title>\n"); 278 out.write(" <style>\n"); 279 out.write(stylesheet); 280 out.write(" </style>\n"); 281 out.write("</head>\n"); 282 283 out.write("<body>\n"); 284 out.write("<h1>" + title + "</h1>\n"); 285 286 out.write("<table frame=\"border\" rules=\"groups\" cellspacing=\"0\" cellpadding=\"4\">\n"); 287 Iterator i = properties.entrySet().iterator(); 288 out.write(" <thead>\n"); 289 out.write(" <tr>\n"); 290 out.write(" "); 291 out.write("<th>Property</th>"); 292 out.write("<th>Value</th>\n"); 293 out.write(" </tr>\n"); 294 out.write(" </thead>\n"); 295 out.write(" <tbody>\n"); 296 while (i.hasNext()) { 297 Map.Entry e = (Map.Entry )i.next(); 298 String s = String.valueOf(e.getKey()); 299 out.write(" <tr>\n"); 300 out.write(" "); 301 out.write("<td align=\"left\">"+String.valueOf(e.getKey())+"</td>"); 302 out.write("<td align=\"left\">"+String.valueOf(e.getValue())+"</td>"); 303 out.write("\n"); 304 out.write(" </tr>\n"); 305 } 306 out.write(" </tbody>\n"); 307 out.write("</table>\n"); 308 309 String lastBenchmarkId = null; 310 String lastClassId = null; 311 String lastTaskId = null; 312 String lastTaskDescription = null; 313 boolean firstClass = true; 314 boolean firstTask = true; 315 316 Iterator ri = sortedResults.iterator(); 317 while (ri.hasNext()) { 318 Result r = (Result)ri.next(); 319 320 if (!r.getClassId().equals(lastClassId)) { 321 if (!firstClass) { 322 out.write(" </tbody>\n"); 323 out.write("</table>\n"); 324 } 325 if (!r.getBenchmarkId().equals(lastBenchmarkId)) { 326 out.write("<h1>Benchmark: " + r.getBenchmarkId() + "</h1>\n"); 327 } 328 String cid = r.getClassId(); 329 String link; 330 if (cid.startsWith("bak.pcj.")) { 331 String url = "../api/" + cid.replace('.', '/') + ".html"; 332 link = "<a target=\"_blank\" HREF=\""+url+"\" title=\"API: "+cid+"\">" + cid + "</a>"; 333 } else if (cid.startsWith("java.")) { 334 String url = "http://java.sun.com/j2se/1.4/docs/api/" + cid.replace('.', '/') + ".html"; 335 link = "<a target=\"_blank\" HREF=\""+url+"\" title=\"API: "+cid+"\">" + cid + "</a>"; 336 } else 337 link = cid; 338 out.write("<h2>Class: " + link + "</h2>\n"); 339 out.write("<table frame=\"border\" rules=\"groups\" cellspacing=\"0\" cellpadding=\"4\">\n"); 340 341 out.write(" <thead>\n"); 342 out.write(" <tr>\n"); 343 out.write(" "); 344 out.write("<th>Task</th>"); 345 out.write("<th>Description</th>"); 346 out.write("<th>Data set</th>"); 347 out.write("<th>Time (ms)</th>"); 348 out.write("\n"); 349 out.write(" </tr>\n"); 350 out.write(" </thead>\n"); 351 firstTask = true; 352 } else { 353 if (!r.getBenchmarkId().equals(lastBenchmarkId)) { 354 out.write("<h1>Benchmark: " + r.getBenchmarkId() + "</h1>\n"); 355 } 356 } 357 358 String taskIdHeading; 359 if (!r.getTaskId().equals(lastTaskId)) { 360 taskIdHeading = r.getTaskId(); 361 if (!firstTask) 362 out.write(" </tbody>\n"); 363 out.write(" <tbody>\n"); 364 } else { 365 taskIdHeading = ""; 366 } 367 368 String taskDescription; 369 if (!r.getTaskDescription().equals(lastTaskDescription)) 370 taskDescription = r.getTaskDescription(); 371 else 372 taskDescription = ""; 373 374 String dataSetIdHeading = r.getDataSetId(); 375 376 out.write(" <tr>\n"); 377 out.write(" "); 378 out.write("<td align=\"left\" valign=\"top\">"+taskIdHeading+"</td>"); 379 out.write("<td align=\"left\" valign=\"top\">"+taskDescription+"</td>"); 380 out.write("<td align=\"left\" valign=\"top\">"+dataSetIdHeading+"</td>"); 381 out.write("<td align=\"right\" valign=\"top\">"+String.valueOf(r.getTime())+"</td>"); 382 out.write("\n"); 383 out.write(" </tr>\n"); 384 385 lastBenchmarkId = r.getBenchmarkId(); 386 lastClassId = r.getClassId(); 387 lastTaskId = r.getTaskId(); 388 lastTaskDescription = r.getTaskDescription(); 389 firstClass = false; 390 firstTask = false; 391 } 392 out.write(" </tbody>\n"); 393 out.write("</table>\n"); 394 out.write("</body>\n"); 395 out.write("</html>\n"); 396 } 397 398 } | Popular Tags |