Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/Harness.java
41161 views
/*1* Copyright (c) 1999, 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*25*/2627package bench;2829import java.io.InputStream;30import java.io.InputStreamReader;31import java.io.StreamTokenizer;32import java.io.IOException;33import java.util.Vector;343536/**37* Benchmark harness. Responsible for parsing config file and running38* benchmarks.39*/40public class Harness {4142BenchInfo[] binfo;4344/**45* Create new benchmark harness with given configuration and reporter.46* Throws ConfigFormatException if there was an error parsing the config47* file.48* <p>49* <b>Config file syntax:</b>50* <p>51* '#' marks the beginning of a comment. Blank lines are ignored. All52* other lines should adhere to the following format:53* <pre>54* <weight> <name> <class> [<args>]55* </pre>56* <weight> is a floating point value which is multiplied times the57* benchmark's execution time to determine its weighted score. The58* total score of the benchmark suite is the sum of all weighted scores59* of its benchmarks.60* <p>61* <name> is a name used to identify the benchmark on the benchmark62* report. If the name contains whitespace, the quote character '"' should63* be used as a delimiter.64* <p>65* <class> is the full name (including the package) of the class66* containing the benchmark implementation. This class must implement67* bench.Benchmark.68* <p>69* [<args>] is a variable-length list of runtime arguments to pass to70* the benchmark. Arguments containing whitespace should use the quote71* character '"' as a delimiter.72* <p>73* <b>Example:</b>74* <pre>75* 3.5 "My benchmark" bench.serial.Test first second "third arg"76* </pre>77*/78public Harness(InputStream in) throws IOException, ConfigFormatException {79Vector bvec = new Vector();80StreamTokenizer tokens = new StreamTokenizer(new InputStreamReader(in));8182tokens.resetSyntax();83tokens.wordChars(0, 255);84tokens.whitespaceChars(0, ' ');85tokens.commentChar('#');86tokens.quoteChar('"');87tokens.eolIsSignificant(true);8889tokens.nextToken();90while (tokens.ttype != StreamTokenizer.TT_EOF) {91switch (tokens.ttype) {92case StreamTokenizer.TT_WORD:93case '"': // parse line94bvec.add(parseBenchInfo(tokens));95break;9697default: // ignore98tokens.nextToken();99break;100}101}102binfo = (BenchInfo[]) bvec.toArray(new BenchInfo[bvec.size()]);103}104105BenchInfo parseBenchInfo(StreamTokenizer tokens)106throws IOException, ConfigFormatException107{108float weight = parseBenchWeight(tokens);109String name = parseBenchName(tokens);110Benchmark bench = parseBenchClass(tokens);111String[] args = parseBenchArgs(tokens);112if (tokens.ttype == StreamTokenizer.TT_EOL)113tokens.nextToken();114return new BenchInfo(bench, name, weight, args);115}116117float parseBenchWeight(StreamTokenizer tokens)118throws IOException, ConfigFormatException119{120float weight;121switch (tokens.ttype) {122case StreamTokenizer.TT_WORD:123case '"':124try {125weight = Float.parseFloat(tokens.sval);126} catch (NumberFormatException e) {127throw new ConfigFormatException("illegal weight value \"" +128tokens.sval + "\" on line " + tokens.lineno());129}130tokens.nextToken();131return weight;132133default:134throw new ConfigFormatException("missing weight value on line "135+ tokens.lineno());136}137}138139String parseBenchName(StreamTokenizer tokens)140throws IOException, ConfigFormatException141{142String name;143switch (tokens.ttype) {144case StreamTokenizer.TT_WORD:145case '"':146name = tokens.sval;147tokens.nextToken();148return name;149150default:151throw new ConfigFormatException("missing benchmark name on " +152"line " + tokens.lineno());153}154}155156Benchmark parseBenchClass(StreamTokenizer tokens)157throws IOException, ConfigFormatException158{159Benchmark bench;160switch (tokens.ttype) {161case StreamTokenizer.TT_WORD:162case '"':163try {164Class cls = Class.forName(tokens.sval);165bench = (Benchmark) cls.newInstance();166} catch (Exception e) {167throw new ConfigFormatException("unable to instantiate " +168"benchmark \"" + tokens.sval + "\" on line " +169tokens.lineno());170}171tokens.nextToken();172return bench;173174default:175throw new ConfigFormatException("missing benchmark class " +176"name on line " + tokens.lineno());177}178}179180String[] parseBenchArgs(StreamTokenizer tokens)181throws IOException, ConfigFormatException182{183Vector vec = new Vector();184for (;;) {185switch (tokens.ttype) {186case StreamTokenizer.TT_EOF:187case StreamTokenizer.TT_EOL:188return (String[]) vec.toArray(new String[vec.size()]);189190case StreamTokenizer.TT_WORD:191case '"':192vec.add(tokens.sval);193tokens.nextToken();194break;195196default:197throw new ConfigFormatException("unrecognized arg token " +198"on line " + tokens.lineno());199}200}201}202203/**204* Run benchmarks, writing results to the given reporter.205*/206public void runBenchmarks(Reporter reporter, boolean verbose) {207for (int i = 0; i < binfo.length; i++) {208if (verbose)209System.out.println("Running benchmark " + i + " (" +210binfo[i].getName() + ")");211try {212binfo[i].runBenchmark();213} catch (Exception e) {214System.err.println("Error: benchmark " + i + " failed: " + e);215e.printStackTrace();216}217cleanup();218}219try {220reporter.writeReport(binfo, System.getProperties());221} catch (IOException e) {222System.err.println("Error: failed to write benchmark report");223}224}225226/**227* Clean up method that is invoked after the completion of each benchmark.228* The default implementation calls System.gc(); subclasses may override229* this to perform additional cleanup measures.230*/231protected void cleanup() {232System.gc();233}234}235236237