KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Reader JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * This class represents a collector of benchmark results. The purpose
29  * of the class is to collect results from different benchmarks in
30  * one report. This class may be run from the command line.
31  *
32  * @author Søren Bak
33  * @version 1.0 2003/5/1
34  * @since 1.0
35  */

36 public class ResultCollector {
37
38     /** The report in which results are collected. */
39     private Report report;
40
41     /**
42      * Creates a new result collector on a specified report.
43      * All collected results will be added to the report.
44      *
45      * @param report
46      * the report in which to collect the results.
47      *
48      * @throws NullPointerException
49      * if <tt>report</tt> is <tt>null</tt>.
50      */

51     public ResultCollector(Report report) {
52         if (report == null)
53             throw new NullPointerException JavaDoc();
54         this.report = report;
55     }
56
57     /**
58      * Creates a new result collector on a new report.
59      */

60     public ResultCollector() {
61         this(new Report());
62     }
63
64     /**
65      * Collects the results of a report from a specified reader.
66      * The reader should deliver data on the form output by
67      * reports.
68      *
69      * @param in
70      * the reader from which to collect the results.
71      *
72      * @see Report#writeResults(Writer out)
73      * @see #collect(String)
74      */

75     public void collect(Reader JavaDoc in) throws IOException JavaDoc {
76         report.readResults(in);
77     }
78
79     /**
80      * Collects the results of a report from a specified file.
81      * The file should contain data on the form output by
82      * reports.
83      *
84      * @param filename
85      * the name of the file from which to collect
86      * the results.
87      *
88      * @see Report#writeResults(Writer out)
89      * @see #collect(Reader)
90      */

91     public void collect(String JavaDoc filename) throws IOException JavaDoc {
92         collect(new FileReader JavaDoc(filename));
93     }
94
95     /**
96      * Returns the report in which the results are collected.
97      *
98      * @return the report in which the results are collected.
99      */

100     public Report getReport() {
101         return report;
102     }
103
104     private static void printUsageAndExit(Throwable JavaDoc e) {
105         System.err.println("Usage: bak.pcj.benchmark.ResultCollector <output file> <report title> [<results files>] ");
106         if (e != null) {
107             System.err.println("An exception was raised:");
108             e.printStackTrace();
109         }
110         System.exit(1);
111     }
112
113     /**
114      * Runs a result collector on a set of files and formats a report as HTML.
115      * The first argument is the name of a file on which to write the report.
116      * The second argument is a title for the report.
117      * The following arguments are names of result files as output by reports.
118      *
119      * @param args
120      * as specified above.
121      */

122     public static void main(String JavaDoc[] args) {
123         if (args.length < 2)
124             printUsageAndExit(null);
125         try {
126             ResultCollector c = new ResultCollector();
127             Report report = c.getReport();
128             report.putProperty("report.title", args[1]);
129             report.putProperty("benchmark.time", (new java.util.Date JavaDoc()).toString());
130             report.putProperty("java.version", System.getProperty("java.version"));
131             report.putProperty("java.vendor", System.getProperty("java.vendor"));
132             report.putProperty("java.vm.specification.version", System.getProperty("java.vm.specification.version"));
133             report.putProperty("java.vm.specification.vendor", System.getProperty("java.vm.specification.vendor"));
134             report.putProperty("java.vm.specification.name", System.getProperty("java.vm.specification.name"));
135             report.putProperty("java.vm.version", System.getProperty("java.vm.version"));
136             report.putProperty("java.vm.vendor", System.getProperty("java.vm.vendor"));
137             report.putProperty("java.vm.name", System.getProperty("java.vm.name"));
138             report.putProperty("java.specification.version", System.getProperty("java.specification.version"));
139             report.putProperty("java.specification.vendor", System.getProperty("java.specification.vendor"));
140             report.putProperty("java.specification.name", System.getProperty("java.specification.name"));
141             report.putProperty("java.compiler", System.getProperty("java.compiler"));
142             report.putProperty("os.name", System.getProperty("os.name"));
143             report.putProperty("os.arch", System.getProperty("os.arch"));
144             report.putProperty("os.version", System.getProperty("os.version"));
145
146             for (int i = 2; i < args.length; i++)
147                 c.collect(args[i]);
148             Writer JavaDoc out = new FileWriter JavaDoc(args[0]);
149             c.report.writeHTML(out);
150             out.flush();
151             out.close();
152         } catch (IOException JavaDoc e) {
153             printUsageAndExit(e);
154         }
155     }
156
157 }
158
Popular Tags