Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/Main.java
41162 views
/*1* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary The Serialization benchmark test. This java class is used to run the26* test under JTREG.27* @library ../../28* @modules java.desktop29* @build bench.BenchInfo bench.HtmlReporter bench.Util bench.Benchmark30* @build bench.Reporter bench.XmlReporter bench.ConfigFormatException31* @build bench.Harness bench.TextReporter32* @build bench.serial.BooleanArrays bench.serial.Booleans33* @build bench.serial.ByteArrays bench.serial.Bytes bench.serial.CharArrays34* @build bench.serial.Chars bench.serial.ClassDesc bench.serial.Cons35* @build bench.serial.CustomDefaultObjTrees bench.serial.CustomObjTrees36* @build bench.serial.DoubleArrays bench.serial.Doubles37* @build bench.serial.ExternObjTrees bench.serial.FloatArrays38* @build bench.serial.Floats bench.serial.GetPutFieldTrees39* @build bench.serial.IntArrays bench.serial.Ints bench.serial.LongArrays40* @build bench.serial.Longs bench.serial.Main bench.serial.ObjArrays41* @build bench.serial.ObjTrees bench.serial.ProxyArrays42* @build bench.serial.ProxyClassDesc bench.serial.RepeatObjs43* @build bench.serial.ReplaceTrees bench.serial.ShortArrays44* @build bench.serial.Shorts bench.serial.SmallObjTrees45* @build bench.serial.StreamBuffer bench.serial.Strings46* @run main/othervm/timeout=1800 -Xss2m bench.serial.Main -c jtreg-config47* @author Mike Warres, Nigel Daley48*/4950// The -Xss2m supplies additional stack space, as bench.serial.ClassDesc51// consumes a considerable amount of stack.5253package bench.serial;5455import bench.ConfigFormatException;56import bench.Harness;57import bench.HtmlReporter;58import bench.Reporter;59import bench.TextReporter;60import bench.XmlReporter;61import java.io.File;62import java.io.FileInputStream;63import java.io.FileNotFoundException;64import java.io.FileOutputStream;65import java.io.InputStream;66import java.io.IOException;67import java.io.OutputStream;68import java.io.PrintStream;69import java.util.Timer;70import java.util.TimerTask;7172/**73* Object serialization benchmark mainline.74*/75public class Main {7677static final String CONFFILE = "config";78static final String VERSION = "1.3";79static final String TEST_SRC_PATH = System.getProperty("test.src") + File.separator;8081static final int TEXT = 0;82static final int HTML = 1;83static final int XML = 2;8485static boolean verbose;86static boolean list;87static boolean exitOnTimer;88static int testDurationSeconds;89static volatile boolean exitRequested;90static Timer timer;91static int format = TEXT;92static InputStream confstr;93static OutputStream repstr;94static Harness harness;95static Reporter reporter;9697/**98* Print help message.99*/100static void usage() {101PrintStream p = System.err;102p.println("\nUsage: java -jar serialbench.jar [-options]");103p.println("\nwhere options are:");104p.println(" -h print this message");105p.println(" -v verbose mode");106p.println(" -l list configuration file");107p.println(" -t <num hours> repeat benchmarks for specified number of hours");108p.println(" -o <file> specify output file");109p.println(" -c <file> specify (non-default) configuration file");110p.println(" -html format output as html (default is text)");111p.println(" -xml format output as xml");112}113114/**115* Throw RuntimeException that wrap message.116*117* @param mesg a message will be wrapped in the RuntimeException.118*/119static void die(String mesg) {120throw new RuntimeException(mesg);121}122123/**124* Mainline parses command line, then hands off to benchmark harness.125*126* @param args127*/128public static void main(String[] args) {129parseArgs(args);130setupStreams();131if (list) {132listConfig();133} else {134setupHarness();135setupReporter();136if (exitOnTimer) {137setupTimer(testDurationSeconds);138do {139runBenchmarks();140} while (!exitRequested);141} else {142runBenchmarks();143}144}145}146147/**148* Parse command-line arguments.149*/150static void parseArgs(String[] args) {151for (int i = 0; i < args.length; i++) {152switch (args[i]) {153case "-h":154usage();155System.exit(0);156break;157case "-v":158verbose = true;159break;160case "-l":161list = true;162break;163case "-t":164if (++i >= args.length)165die("Error: no timeout value specified");166try {167exitOnTimer = true;168testDurationSeconds = Integer.parseInt(args[i]) * 3600;169} catch (NumberFormatException e) {170die("Error: unable to determine timeout value");171}172break;173case "-o":174if (++i >= args.length)175die("Error: no output file specified");176try {177repstr = new FileOutputStream(args[i]);178} catch (FileNotFoundException e) {179die("Error: unable to open \"" + args[i] + "\"");180}181break;182case "-c":183if (++i >= args.length)184die("Error: no config file specified");185String confFileName = TEST_SRC_PATH + args[i];186try {187confstr = new FileInputStream(confFileName);188} catch (FileNotFoundException e) {189die("Error: unable to open \"" + confFileName + "\"");190}191break;192case "-html":193if (format != TEXT)194die("Error: conflicting formats");195format = HTML;196break;197case "-xml":198if (format != TEXT)199die("Error: conflicting formats");200format = XML;201break;202default:203usage();204die("Illegal option: \"" + args[i] + "\"");205}206}207}208209/**210* Set up configuration file and report streams, if not set already.211*/212static void setupStreams() {213if (repstr == null)214repstr = System.out;215if (confstr == null)216confstr = Main.class.getResourceAsStream(TEST_SRC_PATH + CONFFILE);217if (confstr == null)218die("Error: unable to find default config file");219}220221/**222* Print contents of configuration file to selected output stream.223*/224static void listConfig() {225try {226byte[] buf = new byte[256];227int len;228while ((len = confstr.read(buf)) != -1)229repstr.write(buf, 0, len);230} catch (IOException e) {231die("Error: failed to list config file");232}233}234235/**236* Set up the timer to end the test.237*238* @param delay the amount of delay, in seconds, before requesting239* the process exit240*/241static void setupTimer(int delay) {242timer = new Timer(true);243timer.schedule(244new TimerTask() {245@Override246public void run() {247exitRequested = true;248}249},250delay * 1000);251}252253/**254* Set up benchmark harness.255*/256static void setupHarness() {257try {258harness = new Harness(confstr);259} catch (ConfigFormatException e) {260String errmsg = e.getMessage();261if (errmsg != null) {262die("Error parsing config file: " + errmsg);263} else {264die("Error: illegal config file syntax");265}266} catch (IOException e) {267die("Error: failed to read config file");268}269}270271/**272* Setup benchmark reporter.273*/274static void setupReporter() {275String title = "Object Serialization Benchmark, v" + VERSION;276switch (format) {277case TEXT:278reporter = new TextReporter(repstr, title);279break;280281case HTML:282reporter = new HtmlReporter(repstr, title);283break;284285case XML:286reporter = new XmlReporter(repstr, title);287break;288289default:290die("Error: unrecognized format type");291}292}293294/**295* Run benchmarks.296*/297static void runBenchmarks() {298harness.runBenchmarks(reporter, verbose);299}300}301302303