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