Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/TextReporter.java
41161 views
/*1* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25*/2627package bench;2829import java.io.OutputStream;30import java.io.PrintStream;31import java.io.IOException;32import java.util.Arrays;33import java.util.Date;34import java.util.Properties;3536/**37* Benchmark text report generator.38*/39public class TextReporter implements Reporter {4041static final int PRECISION = 3;42static final int INDEX_WIDTH = 3;43static final int NAME_WIDTH = 30;44static final int TIME_WIDTH = 10;45static final int SCORE_WIDTH = 10;46static final int PROPNAME_WIDTH = 25;47static final String[] PROPNAMES = { "os.name", "os.arch", "os.version",48"java.home", "java.vm.version", "java.vm.vendor", "java.vm.name",49"java.compiler", "java.class.path" };5051OutputStream out;52String title;5354/**55* Create TextReporter which writes to the given stream.56*/57public TextReporter(OutputStream out, String title) {58this.out = out;59this.title = title;60}6162/**63* Generate text report.64*/65public void writeReport(BenchInfo[] binfo, Properties props)66throws IOException67{68PrintStream p = new PrintStream(out);69float total = 0.0f;7071p.println("\n" + title);72p.println(pad('-', title.length()));73p.println("");74for (int i = 0; i < PROPNAMES.length; i++) {75p.println(fit(PROPNAMES[i] + ":", PROPNAME_WIDTH) +76props.getProperty(PROPNAMES[i]));77}78p.println("");7980p.println(fit("#", INDEX_WIDTH) + " " +81fit("Benchmark Name", NAME_WIDTH) + " " +82fit("Time (ms)", TIME_WIDTH) + " " +83fit("Score", SCORE_WIDTH));84p.println(pad('-', INDEX_WIDTH + NAME_WIDTH + TIME_WIDTH +85SCORE_WIDTH + 6));8687for (int i = 0; i < binfo.length; i++) {88BenchInfo b = binfo[i];89p.print(fit(Integer.toString(i), INDEX_WIDTH) + " ");90p.print(fit(b.getName(), NAME_WIDTH) + " ");91if (b.getTime() != -1) {92float score = b.getTime() * b.getWeight();93total += score;94p.print(fit(Long.toString(b.getTime()), TIME_WIDTH) + " ");95p.println(fit(Util.floatToString(score, PRECISION),96SCORE_WIDTH));97}98else {99p.print(fit("--", TIME_WIDTH) + " ");100p.println(fit("--", SCORE_WIDTH));101}102}103p.println(pad('-', INDEX_WIDTH + NAME_WIDTH + TIME_WIDTH +104SCORE_WIDTH + 6));105p.println(fit("Total score", INDEX_WIDTH + NAME_WIDTH + TIME_WIDTH +1064) + " " + Util.floatToString(total, PRECISION));107p.println("");108p.println("-----");109p.println("Report generated on " + new Date() + "\n");110p.println("");111}112113/**114* Extend or truncate string so it fits in the given space.115*/116private static String fit(String s, int len) {117int slen = s.length();118StringBuffer buf = new StringBuffer(s);119buf.setLength(len);120for (int i = slen; i < len; i++)121buf.setCharAt(i, ' ');122return buf.toString();123}124125/**126* Return string with given number of chars.127*/128private static String pad(char c, int len) {129char[] buf = new char[len];130Arrays.fill(buf, c);131return new String(buf);132}133}134135136