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 Serialization benchmark test. This java class is used to run the
27  *          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      */
usage()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      */
die(String mesg)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      */
main(String[] args)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      */
parseArgs(String[] args)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                     }
169                     break;
170                 case "-o":
171                     if (++i >= args.length)
172                         die("Error: no output file specified");
173                     try {
174                         repstr = new FileOutputStream(args[i]);
175                     } catch (FileNotFoundException e) {
176                         die("Error: unable to open \"" + args[i] + "\"");
177                     }
178                     break;
179                 case "-c":
180                     if (++i >= args.length)
181                         die("Error: no config file specified");
182                     String confFileName = TEST_SRC_PATH + args[i];
183                     try {
184                         confstr = new FileInputStream(confFileName);
185                     } catch (FileNotFoundException e) {
186                         die("Error: unable to open \"" + confFileName + "\"");
187                     }
188                     break;
189                 case "-html":
190                     if (format != TEXT)
191                         die("Error: conflicting formats");
192                     format = HTML;
193                     break;
194                 case "-xml":
195                     if (format != TEXT)
196                         die("Error: conflicting formats");
197                     format = XML;
198                     break;
199                 default:
200                     usage();
201                     die("Illegal option: \"" + args[i] + "\"");
202             }
203         }
204     }
205 
206     /**
207      * Set up configuration file and report streams, if not set already.
208      */
setupStreams()209     static void setupStreams() {
210         if (repstr == null)
211             repstr = System.out;
212         if (confstr == null)
213             confstr = Main.class.getResourceAsStream(TEST_SRC_PATH + CONFFILE);
214         if (confstr == null)
215             die("Error: unable to find default config file");
216     }
217 
218     /**
219      * Print contents of configuration file to selected output stream.
220      */
listConfig()221     static void listConfig() {
222         try {
223             byte[] buf = new byte[256];
224             int len;
225             while ((len = confstr.read(buf)) != -1)
226                 repstr.write(buf, 0, len);
227         } catch (IOException e) {
228             die("Error: failed to list config file");
229         }
230     }
231 
232     /**
233      * Set up the timer to end the test.
234      *
235      * @param delay the amount of delay, in seconds, before requesting
236      * the process exit
237      */
setupTimer(int delay)238     static void setupTimer(int delay) {
239         timer = new Timer(true);
240         timer.schedule(
241             new TimerTask() {
242                 @Override
243                 public void run() {
244                     exitRequested = true;
245                 }
246             },
247             delay * 1000);
248     }
249 
250     /**
251      * Set up benchmark harness.
252      */
setupHarness()253     static void setupHarness() {
254         try {
255             harness = new Harness(confstr);
256         } catch (ConfigFormatException e) {
257             String errmsg = e.getMessage();
258             if (errmsg != null) {
259                 die("Error parsing config file: " + errmsg);
260             } else {
261                 die("Error: illegal config file syntax");
262             }
263         } catch (IOException e) {
264             die("Error: failed to read config file");
265         }
266     }
267 
268     /**
269      * Setup benchmark reporter.
270      */
setupReporter()271     static void setupReporter() {
272         String title = "Object Serialization Benchmark, v" + VERSION;
273         switch (format) {
274             case TEXT:
275                 reporter = new TextReporter(repstr, title);
276                 break;
277 
278             case HTML:
279                 reporter = new HtmlReporter(repstr, title);
280                 break;
281 
282             case XML:
283                 reporter = new XmlReporter(repstr, title);
284                 break;
285 
286             default:
287                 die("Error: unrecognized format type");
288         }
289     }
290 
291     /**
292      * Run benchmarks.
293      */
runBenchmarks()294     static void runBenchmarks() {
295         harness.runBenchmarks(reporter, verbose);
296     }
297 }
298