001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2003 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.benchmark;
020:
021: import java.io.Reader;
022: import java.io.FileReader;
023: import java.io.Writer;
024: import java.io.FileWriter;
025: import java.io.IOException;
026:
027: /**
028: * This class represents a collector of benchmark results. The purpose
029: * of the class is to collect results from different benchmarks in
030: * one report. This class may be run from the command line.
031: *
032: * @author Søren Bak
033: * @version 1.0 2003/5/1
034: * @since 1.0
035: */
036: public class ResultCollector {
037:
038: /** The report in which results are collected. */
039: private Report report;
040:
041: /**
042: * Creates a new result collector on a specified report.
043: * All collected results will be added to the report.
044: *
045: * @param report
046: * the report in which to collect the results.
047: *
048: * @throws NullPointerException
049: * if <tt>report</tt> is <tt>null</tt>.
050: */
051: public ResultCollector(Report report) {
052: if (report == null)
053: throw new NullPointerException();
054: this .report = report;
055: }
056:
057: /**
058: * Creates a new result collector on a new report.
059: */
060: public ResultCollector() {
061: this (new Report());
062: }
063:
064: /**
065: * Collects the results of a report from a specified reader.
066: * The reader should deliver data on the form output by
067: * reports.
068: *
069: * @param in
070: * the reader from which to collect the results.
071: *
072: * @see Report#writeResults(Writer out)
073: * @see #collect(String)
074: */
075: public void collect(Reader in) throws IOException {
076: report.readResults(in);
077: }
078:
079: /**
080: * Collects the results of a report from a specified file.
081: * The file should contain data on the form output by
082: * reports.
083: *
084: * @param filename
085: * the name of the file from which to collect
086: * the results.
087: *
088: * @see Report#writeResults(Writer out)
089: * @see #collect(Reader)
090: */
091: public void collect(String filename) throws IOException {
092: collect(new FileReader(filename));
093: }
094:
095: /**
096: * Returns the report in which the results are collected.
097: *
098: * @return the report in which the results are collected.
099: */
100: public Report getReport() {
101: return report;
102: }
103:
104: private static void printUsageAndExit(Throwable e) {
105: System.err
106: .println("Usage: bak.pcj.benchmark.ResultCollector <output file> <report title> [<results files>] ");
107: if (e != null) {
108: System.err.println("An exception was raised:");
109: e.printStackTrace();
110: }
111: System.exit(1);
112: }
113:
114: /**
115: * Runs a result collector on a set of files and formats a report as HTML.
116: * The first argument is the name of a file on which to write the report.
117: * The second argument is a title for the report.
118: * The following arguments are names of result files as output by reports.
119: *
120: * @param args
121: * as specified above.
122: */
123: public static void main(String[] args) {
124: if (args.length < 2)
125: printUsageAndExit(null);
126: try {
127: ResultCollector c = new ResultCollector();
128: Report report = c.getReport();
129: report.putProperty("report.title", args[1]);
130: report.putProperty("benchmark.time", (new java.util.Date())
131: .toString());
132: report.putProperty("java.version", System
133: .getProperty("java.version"));
134: report.putProperty("java.vendor", System
135: .getProperty("java.vendor"));
136: report.putProperty("java.vm.specification.version", System
137: .getProperty("java.vm.specification.version"));
138: report.putProperty("java.vm.specification.vendor", System
139: .getProperty("java.vm.specification.vendor"));
140: report.putProperty("java.vm.specification.name", System
141: .getProperty("java.vm.specification.name"));
142: report.putProperty("java.vm.version", System
143: .getProperty("java.vm.version"));
144: report.putProperty("java.vm.vendor", System
145: .getProperty("java.vm.vendor"));
146: report.putProperty("java.vm.name", System
147: .getProperty("java.vm.name"));
148: report.putProperty("java.specification.version", System
149: .getProperty("java.specification.version"));
150: report.putProperty("java.specification.vendor", System
151: .getProperty("java.specification.vendor"));
152: report.putProperty("java.specification.name", System
153: .getProperty("java.specification.name"));
154: report.putProperty("java.compiler", System
155: .getProperty("java.compiler"));
156: report
157: .putProperty("os.name", System
158: .getProperty("os.name"));
159: report
160: .putProperty("os.arch", System
161: .getProperty("os.arch"));
162: report.putProperty("os.version", System
163: .getProperty("os.version"));
164:
165: for (int i = 2; i < args.length; i++)
166: c.collect(args[i]);
167: Writer out = new FileWriter(args[0]);
168: c.report.writeHTML(out);
169: out.flush();
170: out.close();
171: } catch (IOException e) {
172: printUsageAndExit(e);
173: }
174: }
175:
176: }
|