test/java/rmi/reliability/benchmark/bench/serial/Main.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2008, 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 
  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             }


   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             }