Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/reliability/benchmark/bench/serial/Main.java
38899 views
/*1* Copyright (c) 1999, 2013, 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* @build bench.BenchInfo bench.HtmlReporter bench.Util bench.Benchmark29* @build bench.Reporter bench.XmlReporter bench.ConfigFormatException30* @build bench.Harness bench.TextReporter31* @build bench.serial.BooleanArrays bench.serial.Booleans32* @build bench.serial.ByteArrays bench.serial.Bytes bench.serial.CharArrays33* @build bench.serial.Chars bench.serial.ClassDesc bench.serial.Cons34* @build bench.serial.CustomDefaultObjTrees bench.serial.CustomObjTrees35* @build bench.serial.DoubleArrays bench.serial.Doubles36* @build bench.serial.ExternObjTrees bench.serial.FloatArrays37* @build bench.serial.Floats bench.serial.GetPutFieldTrees38* @build bench.serial.IntArrays bench.serial.Ints bench.serial.LongArrays39* @build bench.serial.Longs bench.serial.Main bench.serial.ObjArrays40* @build bench.serial.ObjTrees bench.serial.ProxyArrays41* @build bench.serial.ProxyClassDesc bench.serial.RepeatObjs42* @build bench.serial.ReplaceTrees bench.serial.ShortArrays43* @build bench.serial.Shorts bench.serial.SmallObjTrees44* @build bench.serial.StreamBuffer bench.serial.Strings45* @run main/othervm/timeout=1800 bench.serial.Main -c jtreg-config46* @author Mike Warres, Nigel Daley47*/4849package bench.serial;5051import bench.ConfigFormatException;52import bench.Harness;53import bench.HtmlReporter;54import bench.Reporter;55import bench.TextReporter;56import bench.XmlReporter;57import java.io.File;58import java.io.FileInputStream;59import java.io.FileNotFoundException;60import java.io.FileOutputStream;61import java.io.InputStream;62import java.io.IOException;63import java.io.OutputStream;64import java.io.PrintStream;65import java.util.Timer;66import java.util.TimerTask;6768/**69* Object serialization benchmark mainline.70*/71public class Main {7273static final String CONFFILE = "config";74static final String VERSION = "1.3";75static final String TEST_SRC_PATH = System.getProperty("test.src") + File.separator;7677static final int TEXT = 0;78static final int HTML = 1;79static final int XML = 2;8081static boolean verbose;82static boolean list;83static boolean exitOnTimer;84static int testDurationSeconds;85static volatile boolean exitRequested;86static Timer timer;87static int format = TEXT;88static InputStream confstr;89static OutputStream repstr;90static Harness harness;91static Reporter reporter;9293/**94* Print help message.95*/96static void usage() {97PrintStream p = System.err;98p.println("\nUsage: java -jar serialbench.jar [-options]");99p.println("\nwhere options are:");100p.println(" -h print this message");101p.println(" -v verbose mode");102p.println(" -l list configuration file");103p.println(" -t <num hours> repeat benchmarks for specified number of hours");104p.println(" -o <file> specify output file");105p.println(" -c <file> specify (non-default) configuration file");106p.println(" -html format output as html (default is text)");107p.println(" -xml format output as xml");108}109110/**111* Throw RuntimeException that wrap message.112*113* @param mesg a message will be wrapped in the RuntimeException.114*/115static void die(String mesg) {116throw new RuntimeException(mesg);117}118119/**120* Mainline parses command line, then hands off to benchmark harness.121*122* @param args123*/124public static void main(String[] args) {125parseArgs(args);126setupStreams();127if (list) {128listConfig();129} else {130setupHarness();131setupReporter();132if (exitOnTimer) {133setupTimer(testDurationSeconds);134do {135runBenchmarks();136} while (!exitRequested);137} else {138runBenchmarks();139}140}141}142143/**144* Parse command-line arguments.145*/146static void parseArgs(String[] args) {147for (int i = 0; i < args.length; i++) {148switch (args[i]) {149case "-h":150usage();151System.exit(0);152break;153case "-v":154verbose = true;155break;156case "-l":157list = true;158break;159case "-t":160if (++i >= args.length)161die("Error: no timeout value specified");162try {163exitOnTimer = true;164testDurationSeconds = Integer.parseInt(args[i]) * 3600;165} catch (NumberFormatException e) {166die("Error: unable to determine timeout value");167}168break;169case "-o":170if (++i >= args.length)171die("Error: no output file specified");172try {173repstr = new FileOutputStream(args[i]);174} catch (FileNotFoundException e) {175die("Error: unable to open \"" + args[i] + "\"");176}177break;178case "-c":179if (++i >= args.length)180die("Error: no config file specified");181String confFileName = TEST_SRC_PATH + args[i];182try {183confstr = new FileInputStream(confFileName);184} catch (FileNotFoundException e) {185die("Error: unable to open \"" + confFileName + "\"");186}187break;188case "-html":189if (format != TEXT)190die("Error: conflicting formats");191format = HTML;192break;193case "-xml":194if (format != TEXT)195die("Error: conflicting formats");196format = XML;197break;198default:199usage();200die("Illegal option: \"" + args[i] + "\"");201}202}203}204205/**206* Set up configuration file and report streams, if not set already.207*/208static void setupStreams() {209if (repstr == null)210repstr = System.out;211if (confstr == null)212confstr = Main.class.getResourceAsStream(TEST_SRC_PATH + CONFFILE);213if (confstr == null)214die("Error: unable to find default config file");215}216217/**218* Print contents of configuration file to selected output stream.219*/220static void listConfig() {221try {222byte[] buf = new byte[256];223int len;224while ((len = confstr.read(buf)) != -1)225repstr.write(buf, 0, len);226} catch (IOException e) {227die("Error: failed to list config file");228}229}230231/**232* Set up the timer to end the test.233*234* @param delay the amount of delay, in seconds, before requesting235* the process exit236*/237static void setupTimer(int delay) {238timer = new Timer(true);239timer.schedule(240new TimerTask() {241@Override242public void run() {243exitRequested = true;244}245},246delay * 1000);247}248249/**250* Set up benchmark harness.251*/252static void setupHarness() {253try {254harness = new Harness(confstr);255} catch (ConfigFormatException e) {256String errmsg = e.getMessage();257if (errmsg != null) {258die("Error parsing config file: " + errmsg);259} else {260die("Error: illegal config file syntax");261}262} catch (IOException e) {263die("Error: failed to read config file");264}265}266267/**268* Setup benchmark reporter.269*/270static void setupReporter() {271String title = "Object Serialization Benchmark, v" + VERSION;272switch (format) {273case TEXT:274reporter = new TextReporter(repstr, title);275break;276277case HTML:278reporter = new HtmlReporter(repstr, title);279break;280281case XML:282reporter = new XmlReporter(repstr, title);283break;284285default:286die("Error: unrecognized format type");287}288}289290/**291* Run benchmarks.292*/293static void runBenchmarks() {294harness.runBenchmarks(reporter, verbose);295}296}297298299