1 /*
   2  * Copyright (c) 1999, 2013, 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  * @test
  26  * @summary The RMI Serialization benchmark test. This java class is used to run
  27  *          the test under JTREG.
  28  * @library ../../
  29  * @build bench.BenchInfo bench.HtmlReporter bench.Util bench.Benchmark 
  30  * @build bench.Reporter bench.XmlReporter bench.ConfigFormatException 
  31  * @build bench.Harness bench.TextReporter
  32  * @build bench.serial.BooleanArrays bench.serial.Booleans
  33  * @build bench.serial.ByteArrays bench.serial.Bytes bench.serial.CharArrays
  34  * @build bench.serial.Chars bench.serial.ClassDesc bench.serial.Cons
  35  * @build bench.serial.CustomDefaultObjTrees bench.serial.CustomObjTrees
  36  * @build bench.serial.DoubleArrays bench.serial.Doubles
  37  * @build bench.serial.ExternObjTrees bench.serial.FloatArrays
  38  * @build bench.serial.Floats bench.serial.GetPutFieldTrees
  39  * @build bench.serial.IntArrays bench.serial.Ints bench.serial.LongArrays
  40  * @build bench.serial.Longs bench.serial.Main bench.serial.ObjArrays
  41  * @build bench.serial.ObjTrees bench.serial.ProxyArrays
  42  * @build bench.serial.ProxyClassDesc bench.serial.RepeatObjs
  43  * @build bench.serial.ReplaceTrees bench.serial.ShortArrays
  44  * @build bench.serial.Shorts bench.serial.SmallObjTrees
  45  * @build bench.serial.StreamBuffer bench.serial.Strings
  46  * @run main/othervm/timeout=1800 bench.serial.Main -c jtreg-config
  47  * @author Mike Warres, Nigel Daley
  48  */
  49 
  50 package bench.serial;
  51 
  52 import bench.ConfigFormatException;
  53 import bench.Harness;
  54 import bench.HtmlReporter;
  55 import bench.Reporter;
  56 import bench.TextReporter;
  57 import bench.XmlReporter;
  58 import java.io.File;
  59 import java.io.FileInputStream;
  60 import java.io.FileNotFoundException;
  61 import java.io.FileOutputStream;
  62 import java.io.InputStream;
  63 import java.io.IOException;
  64 import java.io.OutputStream;
  65 import java.io.PrintStream;
  66 import java.util.Timer;
  67 import java.util.TimerTask;
  68 
  69 /**
  70  * Object serialization benchmark mainline.
  71  */
  72 public class Main {
  73 
  74     static final String CONFFILE = "config";
  75     static final String VERSION = "1.3";
  76     static final String TEST_SRC_PATH = System.getProperty("test.src") + File.separator;
  77 
  78     static final int TEXT = 0;
  79     static final int HTML = 1;
  80     static final int XML = 2;
  81 
  82     static boolean verbose;
  83     static boolean list;
  84     static boolean exitOnTimer;
  85     static int testDurationSeconds;
  86     static volatile boolean exitRequested;
  87     static Timer timer;
  88     static int format = TEXT;
  89     static InputStream confstr;
  90     static OutputStream repstr;
  91     static Harness harness;
  92     static Reporter reporter;
  93 
  94     /**
  95      * Print help message.
  96      */
  97     static void usage() {
  98         PrintStream p = System.err;
  99         p.println("\nUsage: java -jar serialbench.jar [-options]");
 100         p.println("\nwhere options are:");
 101         p.println("  -h              print this message");
 102         p.println("  -v              verbose mode");
 103         p.println("  -l              list configuration file");
 104         p.println("  -t <num hours>  repeat benchmarks for specified number of hours");
 105         p.println("  -o <file>       specify output file");
 106         p.println("  -c <file>       specify (non-default) configuration file");
 107         p.println("  -html           format output as html (default is text)");
 108         p.println("  -xml            format output as xml");
 109     }
 110 
 111     /**
 112      * Throw RuntimeException that wrap message.
 113      *
 114      * @param mesg a message will be wrapped in the RuntimeException.
 115      */
 116     static void die(String mesg) {
 117         throw new RuntimeException(mesg);
 118     }
 119 
 120     /**
 121      * Mainline parses command line, then hands off to benchmark harness.
 122      * 
 123      * @param args
 124      */
 125     public static void main(String[] args) {
 126         parseArgs(args);
 127         setupStreams();
 128         if (list) {
 129             listConfig();
 130         } else {
 131             setupHarness();
 132             setupReporter();
 133             if (exitOnTimer) {
 134                 setupTimer(testDurationSeconds);
 135                 do {
 136                     runBenchmarks();
 137                 } while (!exitRequested);
 138             } else {
 139                 runBenchmarks();
 140             }
 141         }
 142     }
 143 
 144     /**
 145      * Parse command-line arguments.
 146      */
 147     static void parseArgs(String[] args) {
 148         for (int i = 0; i < args.length; i++) {
 149             switch (args[i]) {
 150                 case "-h":
 151                     usage();
 152                     System.exit(0);
 153                     break;
 154                 case "-v":
 155                     verbose = true;
 156                     break;
 157                 case "-l":
 158                     list = true;
 159                     break;
 160                 case "-t":
 161                     if (++i >= args.length)
 162                         die("Error: no timeout value specified");
 163                     try {
 164                         exitOnTimer = true;
 165                         testDurationSeconds = Integer.parseInt(args[i]) * 3600;
 166                     } catch (NumberFormatException e) {
 167                         die("Error: unable to determine timeout value");
 168                     }   break;
 169                 case "-o":
 170                     if (++i >= args.length)
 171                         die("Error: no output file specified");
 172                     try {
 173                         repstr = new FileOutputStream(args[i]);
 174                     } catch (FileNotFoundException e) {
 175                         die("Error: unable to open \"" + args[i] + "\"");
 176                     }   break;
 177                 case "-c":
 178                     if (++i >= args.length)
 179                         die("Error: no config file specified");
 180                     String confFileName = TEST_SRC_PATH + args[i];
 181                     try {
 182                         confstr = new FileInputStream(confFileName);
 183                     } catch (FileNotFoundException e) {
 184                         die("Error: unable to open \"" + confFileName + "\"");
 185                     }   break;
 186                 case "-html":
 187                     if (format != TEXT)
 188                         die("Error: conflicting formats");
 189                     format = HTML;
 190                     break;
 191                 case "-xml":
 192                     if (format != TEXT)
 193                         die("Error: conflicting formats");
 194                     format = XML;
 195                     break;
 196                 default:
 197                     usage();
 198                     die("Illegal option: \"" + args[i] + "\"");
 199             }
 200         }
 201     }
 202 
 203     /**
 204      * Set up configuration file and report streams, if not set already.
 205      */
 206     static void setupStreams() {
 207         if (repstr == null)
 208             repstr = System.out;
 209         if (confstr == null)
 210             confstr = Main.class.getResourceAsStream(TEST_SRC_PATH + CONFFILE);
 211         if (confstr == null)
 212             die("Error: unable to find default config file");
 213     }
 214 
 215     /**
 216      * Print contents of configuration file to selected output stream.
 217      */
 218     static void listConfig() {
 219         try {
 220             byte[] buf = new byte[256];
 221             int len;
 222             while ((len = confstr.read(buf)) != -1)
 223                 repstr.write(buf, 0, len);
 224         } catch (IOException e) {
 225             die("Error: failed to list config file");
 226         }
 227     }
 228 
 229     /**
 230      * Set up the timer to end the test.
 231      *
 232      * @param delay the amount of delay, in seconds, before requesting
 233      * the process exit
 234      */
 235     static void setupTimer(int delay) {
 236         timer = new Timer(true);
 237         timer.schedule(
 238             new TimerTask() {
 239                 @Override
 240                 public void run() {
 241                     exitRequested = true;
 242                 }
 243             },
 244             delay * 1000);
 245     }
 246 
 247     /**
 248      * Set up benchmark harness.
 249      */
 250     static void setupHarness() {
 251         try {
 252             harness = new Harness(confstr);
 253         } catch (ConfigFormatException e) {
 254             String errmsg = e.getMessage();
 255             if (errmsg != null) {
 256                 die("Error parsing config file: " + errmsg);
 257             } else {
 258                 die("Error: illegal config file syntax");
 259             }
 260         } catch (IOException e) {
 261             die("Error: failed to read config file");
 262         }
 263     }
 264 
 265     /**
 266      * Setup benchmark reporter.
 267      */
 268     static void setupReporter() {
 269         String title = "Object Serialization Benchmark, v" + VERSION;
 270         switch (format) {
 271             case TEXT:
 272                 reporter = new TextReporter(repstr, title);
 273                 break;
 274 
 275             case HTML:
 276                 reporter = new HtmlReporter(repstr, title);
 277                 break;
 278 
 279             case XML:
 280                 reporter = new XmlReporter(repstr, title);
 281                 break;
 282 
 283             default:
 284                 die("Error: unrecognized format type");
 285         }
 286     }
 287 
 288     /**
 289      * Run benchmarks.
 290      */
 291     static void runBenchmarks() {
 292         harness.runBenchmarks(reporter, verbose);
 293     }
 294 }