0N/A/*
2362N/A * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A */
0N/A
0N/Apackage bench;
0N/A
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.IOException;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Date;
0N/Aimport java.util.Properties;
0N/A
0N/A/**
0N/A * Benchmark text report generator.
0N/A */
0N/Apublic class TextReporter implements Reporter {
0N/A
0N/A static final int PRECISION = 3;
0N/A static final int INDEX_WIDTH = 3;
0N/A static final int NAME_WIDTH = 30;
0N/A static final int TIME_WIDTH = 10;
0N/A static final int SCORE_WIDTH = 10;
0N/A static final int PROPNAME_WIDTH = 25;
0N/A static final String[] PROPNAMES = { "os.name", "os.arch", "os.version",
0N/A "java.home", "java.vm.version", "java.vm.vendor", "java.vm.name",
0N/A "java.compiler", "java.class.path", "sun.boot.class.path" };
0N/A
0N/A OutputStream out;
0N/A String title;
0N/A
0N/A /**
0N/A * Create TextReporter which writes to the given stream.
0N/A */
0N/A public TextReporter(OutputStream out, String title) {
0N/A this.out = out;
0N/A this.title = title;
0N/A }
0N/A
0N/A /**
0N/A * Generate text report.
0N/A */
0N/A public void writeReport(BenchInfo[] binfo, Properties props)
0N/A throws IOException
0N/A {
0N/A PrintStream p = new PrintStream(out);
0N/A float total = 0.0f;
0N/A
0N/A p.println("\n" + title);
0N/A p.println(pad('-', title.length()));
0N/A p.println("");
0N/A for (int i = 0; i < PROPNAMES.length; i++) {
0N/A p.println(fit(PROPNAMES[i] + ":", PROPNAME_WIDTH) +
0N/A props.getProperty(PROPNAMES[i]));
0N/A }
0N/A p.println("");
0N/A
0N/A p.println(fit("#", INDEX_WIDTH) + " " +
0N/A fit("Benchmark Name", NAME_WIDTH) + " " +
0N/A fit("Time (ms)", TIME_WIDTH) + " " +
0N/A fit("Score", SCORE_WIDTH));
0N/A p.println(pad('-', INDEX_WIDTH + NAME_WIDTH + TIME_WIDTH +
0N/A SCORE_WIDTH + 6));
0N/A
0N/A for (int i = 0; i < binfo.length; i++) {
0N/A BenchInfo b = binfo[i];
0N/A p.print(fit(Integer.toString(i), INDEX_WIDTH) + " ");
0N/A p.print(fit(b.getName(), NAME_WIDTH) + " ");
0N/A if (b.getTime() != -1) {
0N/A float score = b.getTime() * b.getWeight();
0N/A total += score;
0N/A p.print(fit(Long.toString(b.getTime()), TIME_WIDTH) + " ");
0N/A p.println(fit(Util.floatToString(score, PRECISION),
0N/A SCORE_WIDTH));
0N/A }
0N/A else {
0N/A p.print(fit("--", TIME_WIDTH) + " ");
0N/A p.println(fit("--", SCORE_WIDTH));
0N/A }
0N/A }
0N/A p.println(pad('-', INDEX_WIDTH + NAME_WIDTH + TIME_WIDTH +
0N/A SCORE_WIDTH + 6));
0N/A p.println(fit("Total score", INDEX_WIDTH + NAME_WIDTH + TIME_WIDTH +
0N/A 4) + " " + Util.floatToString(total, PRECISION));
0N/A p.println("");
0N/A p.println("-----");
0N/A p.println("Report generated on " + new Date() + "\n");
0N/A p.println("");
0N/A }
0N/A
0N/A /**
0N/A * Extend or truncate string so it fits in the given space.
0N/A */
0N/A private static String fit(String s, int len) {
0N/A int slen = s.length();
0N/A StringBuffer buf = new StringBuffer(s);
0N/A buf.setLength(len);
0N/A for (int i = slen; i < len; i++)
0N/A buf.setCharAt(i, ' ');
0N/A return buf.toString();
0N/A }
0N/A
0N/A /**
0N/A * Return string with given number of chars.
0N/A */
0N/A private static String pad(char c, int len) {
0N/A char[] buf = new char[len];
0N/A Arrays.fill(buf, c);
0N/A return new String(buf);
0N/A }
0N/A}