J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | package bench.serial; |
| 29 | |
| 30 | import bench.ConfigFormatException; |
| 31 | import bench.Harness; |
| 32 | import bench.HtmlReporter; |
| 33 | import bench.Reporter; |
| 34 | import bench.TextReporter; |
| 35 | import bench.XmlReporter; |
| 36 | import java.io.FileInputStream; |
| 37 | import java.io.FileOutputStream; |
| 38 | import java.io.InputStream; |
| 39 | import java.io.IOException; |
| 40 | import java.io.OutputStream; |
| 41 | import java.io.PrintStream; |
| 42 | import java.util.Timer; |
| 43 | import java.util.TimerTask; |
| 44 | |
| 45 | /** |
| 46 | * Object serialization benchmark mainline. |
| 47 | */ |
| 48 | public class Main { |
| 49 | |
| 50 | static final String CONFFILE = "/bench/serial/config"; |
| 51 | static final String VERSION = "1.3"; |
| 52 | |
| 53 | static final int TEXT = 0; |
| 54 | static final int HTML = 1; |
| 55 | static final int XML = 2; |
| 56 | |
| 57 | static boolean verbose; |
| 58 | static boolean list; |
| 59 | static boolean exitOnTimer; |
| 60 | static int testDurationSeconds; |
| 61 | static volatile boolean exitRequested; |
| 62 | static Timer timer; |
| 63 | static int format = TEXT; |
| 64 | static InputStream confstr; |
| 65 | static OutputStream repstr; |
| 66 | static Harness harness; |
| 67 | static Reporter reporter; |
| 68 | |
| 69 | /** |
| 70 | * Print help message. |
| 71 | */ |
| 72 | static void usage() { |
| 73 | PrintStream p = System.err; |
| 74 | p.println("\nUsage: java -jar serialbench.jar [-options]"); |
| 75 | p.println("\nwhere options are:"); |
| 76 | p.println(" -h print this message"); |
| 77 | p.println(" -v verbose mode"); |
| 78 | p.println(" -l list configuration file"); |
| 79 | p.println(" -t <num hours> repeat benchmarks for specified number of hours"); |
| 80 | p.println(" -o <file> specify output file"); |
| 81 | p.println(" -c <file> specify (non-default) configuration file"); |
| 82 | p.println(" -html format output as html (default is text)"); |
| 83 | p.println(" -xml format output as xml"); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Print error message and exit. |
| 88 | */ |
| 89 | static void die(String mesg) { |
| 90 | System.err.println(mesg); |
| 91 | System.exit(1); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Mainline parses command line, then hands off to benchmark harness. |
| 96 | */ |
| 97 | public static void main(String[] args) { |
| 98 | parseArgs(args); |
| 99 | setupStreams(); |
| 100 | if (list) { |
| 101 | listConfig(); |
| 102 | } else { |
| 103 | setupHarness(); |
| 104 | setupReporter(); |
| 105 | if (exitOnTimer) { |
| 106 | setupTimer(testDurationSeconds); |
| 107 | while (true) { |
| 108 | runBenchmarks(); |
| 109 | if (exitRequested) { |
| 110 | System.exit(0); |
| 111 | } |
| 112 | } |
| 113 | } else { |
| 114 | runBenchmarks(); |
| 115 | System.exit(0); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Parse command-line arguments. |
| 122 | */ |
| 123 | static void parseArgs(String[] args) { |
| 124 | for (int i = 0; i < args.length; i++) { |
| 125 | if (args[i].equals("-h")) { |
| 126 | usage(); |
| 127 | System.exit(0); |
| 128 | } else if (args[i].equals("-v")) { |
| 129 | verbose = true; |
| 130 | } else if (args[i].equals("-l")) { |
| 131 | list = true; |
| 132 | } else if (args[i].equals("-t")) { |
| 133 | if (++i >= args.length) |
| 134 | die("Error: no timeout value specified"); |
| 135 | try { |
| 136 | exitOnTimer = true; |
| 137 | testDurationSeconds = Integer.parseInt(args[i]) * 3600; |
| 138 | } catch (Exception e) { |
| 139 | die("Error: unable to determine timeout value"); |
| 140 | } |
| 141 | } else if (args[i].equals("-o")) { |
| 142 | if (++i >= args.length) |
| 143 | die("Error: no output file specified"); |
| 144 | try { |
| 145 | repstr = new FileOutputStream(args[i]); |
| 146 | } catch (IOException e) { |
| 147 | die("Error: unable to open \"" + args[i] + "\""); |
| 148 | } |
| 149 | } else if (args[i].equals("-c")) { |
| 150 | if (++i >= args.length) |
| 151 | die("Error: no config file specified"); |
| 152 | try { |
| 153 | confstr = new FileInputStream(args[i]); |
| 154 | } catch (IOException e) { |
| 155 | die("Error: unable to open \"" + args[i] + "\""); |
| 156 | } |
| 157 | } else if (args[i].equals("-html")) { |
| 158 | if (format != TEXT) |
| 159 | die("Error: conflicting formats"); |
| 160 | format = HTML; |
| 161 | } else if (args[i].equals("-xml")) { |
| 162 | if (format != TEXT) |
| 163 | die("Error: conflicting formats"); |
| 164 | format = XML; |
| 165 | } else { |
| 166 | System.err.println("Illegal option: \"" + args[i] + "\""); |
| 167 | usage(); |
| 168 | System.exit(1); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Set up configuration file and report streams, if not set already. |
| 175 | */ |
| 176 | static void setupStreams() { |
| 177 | if (repstr == null) |
| 178 | repstr = System.out; |
| 179 | if (confstr == null) |
| 180 | confstr = (new Main()).getClass().getResourceAsStream(CONFFILE); |
| 181 | if (confstr == null) |
| 182 | die("Error: unable to find default config file"); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Print contents of configuration file to selected output stream. |
| 187 | */ |
| 188 | static void listConfig() { |
| 189 | try { |
| 190 | byte[] buf = new byte[256]; |
| 191 | int len; |
| 192 | while ((len = confstr.read(buf)) != -1) |
| 193 | repstr.write(buf, 0, len); |
| 194 | } catch (IOException e) { |
| 195 | die("Error: failed to list config file"); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Set up the timer to end the test. |
| 201 | * |
| 202 | * @param delay the amount of delay, in seconds, before requesting |
| 203 | * the process exit |
| 204 | */ |
| 205 | static void setupTimer(int delay) { |
| 206 | timer = new Timer(true); |
| 207 | timer.schedule( |
| 208 | new TimerTask() { |
| 209 | public void run() { |
| 210 | exitRequested = true; |
| 211 | } |
| 212 | }, |
| 213 | delay * 1000); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Set up benchmark harness. |
| 218 | */ |
| 219 | static void setupHarness() { |
| 220 | try { |
| 221 | harness = new Harness(confstr); |
| 222 | } catch (ConfigFormatException e) { |
| 223 | String errmsg = e.getMessage(); |
| 224 | if (errmsg != null) { |
| 225 | die("Error parsing config file: " + errmsg); |
| 226 | } else { |
| 227 | die("Error: illegal config file syntax"); |
| 228 | } |
| 229 | } catch (IOException e) { |
| 230 | die("Error: failed to read config file"); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Setup benchmark reporter. |
| 236 | */ |
| 237 | static void setupReporter() { |
| 238 | String title = "Object Serialization Benchmark, v" + VERSION; |
| 239 | switch (format) { |
| 240 | case TEXT: |
| 241 | reporter = new TextReporter(repstr, title); |
| 242 | break; |
| 243 | |
| 244 | case HTML: |
| 245 | reporter = new HtmlReporter(repstr, title); |
| 246 | break; |
| 247 | |
| 248 | case XML: |
| 249 | reporter = new XmlReporter(repstr, title); |
| 250 | break; |
| 251 | |
| 252 | default: |
| 253 | die("Error: unrecognized format type"); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Run benchmarks. |
| 259 | */ |
| 260 | static void runBenchmarks() { |
| 261 | harness.runBenchmarks(reporter, verbose); |
| 262 | } |
| 263 | } |
| 264 | |