KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > benchmark > Report


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.benchmark;
20
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.TreeMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.TreeSet JavaDoc;
31
32 import java.io.Writer JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37  * This class represents reports of results from benchmarks.
38  * Results are collected in a report that can finally be
39  * transformed to some external representation.
40  *
41  * @author Søren Bak
42  * @version 1.1 2003/15/2
43  * @since 1.0
44  */

45 public class Report {
46
47     /** The results collected in the report. */
48     private List JavaDoc results;
49
50     /** Properties like time, machine, OS, VM, etc. */
51     private Map JavaDoc properties;
52
53     /**
54      * Creates a new report for benchmark results.
55      */

56     public Report() {
57         results = new ArrayList JavaDoc();
58         properties = new TreeMap JavaDoc();
59     }
60
61     /**
62      * Adds a result to this report.
63      *
64      * @param result
65      * the result to add.
66      *
67      * @throws NullPointerException
68      * if <tt>result</tt> is <tt>null</tt>.
69      */

70     public void addResult(Result result) {
71         if (result == null)
72             throw new NullPointerException JavaDoc();
73         results.add(result);
74     }
75
76     /**
77      * Returns the results of this report in no particular
78      * order.
79      *
80      * @return an unmodifiable collection of the results
81      * of this report in no particular order.
82      */

83     public Collection JavaDoc getResults() {
84         return Collections.unmodifiableCollection(results);
85     }
86
87     /**
88      * Clears the results of this report.
89      */

90     public void clearResults() {
91         results.clear();
92     }
93
94
95     /**
96      * Adds a property to this report.
97      *
98      * @param key
99      * the key of the property.
100      *
101      * @param value
102      * the value of the property. If the value is
103      * <tt>null</tt>, the property is removed from
104      * the report.
105      *
106      * @throws NullPointerException
107      * if <tt>key</tt> is <tt>null</tt>.
108      */

109     public void putProperty(String JavaDoc key, String JavaDoc value) {
110         if (key == null)
111             throw new NullPointerException JavaDoc();
112         if (value == null)
113             properties.remove(key);
114         else
115             properties.put(key, value);
116     }
117
118     /**
119      * Returns a property of this report.
120      *
121      * @param key
122      * the key of the property to return.
123      *
124      * @return the value of the property with the specified
125      * key; returns <tt>null</tt> if no such property
126      * is in this report.
127      *
128      * @throws NullPointerException
129      * if <tt>key</tt> is <tt>null</tt>.
130      */

131     public String JavaDoc getProperty(String JavaDoc key) {
132         if (key == null)
133             throw new NullPointerException JavaDoc();
134         return (String JavaDoc)properties.get(key);
135     }
136
137     // ---------------------------------------------------------------
138
// Report input/output
139
// ---------------------------------------------------------------
140

141     private String JavaDoc readLine(Reader JavaDoc in) throws IOException JavaDoc {
142         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
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 JavaDoc[] split(String JavaDoc s, char c) {
152         ArrayList JavaDoc a = new ArrayList JavaDoc();
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 JavaDoc)a.get(a.size()-1))+pc);
163             p++;
164         }
165         String JavaDoc[] sa = new String JavaDoc[a.size()];
166         a.toArray(sa);
167         return sa;
168     }
169
170     /**
171      * Reads results into this report from a specified reader.
172      *
173      * @param in
174      * the reader from which to read results.
175      *
176      * @throws IOException
177      * if an error occurs reading from <tt>out</tt>.
178      */

179     public void readResults(Reader JavaDoc in) throws IOException JavaDoc {
180         String JavaDoc s;
181         int ptr, nptr, len;
182         while ((s = readLine(in)) != null) {
183             String JavaDoc[] fields = split(s, ';');
184             Result result = new Result(
185                 fields[0], // benchmark id
186
fields[1], // data set id
187
fields[2], // class id
188
fields[3], // task id
189
fields[4], // task description
190
Long.parseLong(fields[5]) // time
191
);
192             addResult(result);
193         }
194     }
195
196     /**
197      * Writes the results of this report to a specified writer.
198      *
199      * @param out
200      * the writer on which to write the results.
201      *
202      * @throws IOException
203      * if an error occurs writing to <tt>out</tt>.
204      */

205     public void writeResults(Writer JavaDoc out) throws IOException JavaDoc {
206         Iterator JavaDoc 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     // ---------------------------------------------------------------
225
// Formatting
226
// ---------------------------------------------------------------
227

228     private static String JavaDoc 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     /**
253      * Formats this report as HTML on a specified writer.
254      *
255      * @param out
256      * the writer on which to format this report.
257      *
258      * @throws NullPointerException
259      * if <tt>out</tt> is <tt>null</tt>.
260      *
261      * @throws IOException
262      * if an error occurs writing to <tt>out</tt>.
263      */

264     public void writeHTML(Writer JavaDoc out) throws IOException JavaDoc {
265
266         Set JavaDoc sortedResults = new TreeSet JavaDoc(new ResultComparator());
267         sortedResults.addAll(results);
268         String JavaDoc 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 JavaDoc 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 JavaDoc e = (Map.Entry JavaDoc)i.next();
298             String JavaDoc 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 JavaDoc lastBenchmarkId = null;
310         String JavaDoc lastClassId = null;
311         String JavaDoc lastTaskId = null;
312         String JavaDoc lastTaskDescription = null;
313         boolean firstClass = true;
314         boolean firstTask = true;
315
316         Iterator JavaDoc 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 JavaDoc cid = r.getClassId();
329                 String JavaDoc link;
330                 if (cid.startsWith("bak.pcj.")) {
331                     String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc taskDescription;
369             if (!r.getTaskDescription().equals(lastTaskDescription))
370                 taskDescription = r.getTaskDescription();
371             else
372                 taskDescription = "";
373
374             String JavaDoc 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