1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.benchmark;
2021import org.apache.commons.cli.CommandLine;
22import org.apache.commons.cli.Options;
23import org.apache.log4j.Logger;
2425/**26 * Command line options for benchmarks27 */28publicclassBenchmarkOption {
29/** Option for help */30publicstaticfinalBenchmarkOption HELP =
31newBenchmarkOption("h", "help", false, "Help");
32/** Option for verbose */33publicstaticfinalBenchmarkOption VERBOSE =
34newBenchmarkOption("v", "verbose", false, "Verbose");
35/** Option for number of workers */36publicstaticfinalBenchmarkOption WORKERS =
37newBenchmarkOption("w", "workers", true, "Number of workers",
38"Need to choose the number of workers (-w)");
39/** Option for number of supersteps */40publicstaticfinalBenchmarkOption SUPERSTEPS =
41newBenchmarkOption("s", "supersteps", true,
42"Supersteps to execute before finishing",
43"Need to set the number of supersteps (-s)");
44/** Option for number of vertices */45publicstaticfinalBenchmarkOption VERTICES =
46newBenchmarkOption("V", "aggregateVertices", true,
47"Aggregate vertices", "Need to set the aggregate vertices (-V)");
48/** Option for number of edges per vertex */49publicstaticfinalBenchmarkOption EDGES_PER_VERTEX =
50newBenchmarkOption("e", "edgesPerVertex", true,
51"Edges per vertex",
52"Need to set the number of edges per vertex (-e)");
53/** Option for minimum ratio of partition-local edges */54publicstaticfinalBenchmarkOption LOCAL_EDGES_MIN_RATIO =
55newBenchmarkOption(
56"l", "localEdgesMinRatio", true,
57"Minimum ratio of partition-local edges (default is 0)");
58/** Option for using Jython */59publicstaticfinalBenchmarkOption JYTHON =
60newBenchmarkOption("j", "jython", false, "Use jython implementation");
61/** Option for path to script for computation */62publicstaticfinalBenchmarkOption SCRIPT_PATH =
63newBenchmarkOption("sp", "scriptPath", true,
64"Path to script for computation, can be local or HDFS path");
6566/** Short option */67private String shortOption;
68/** Long option */69private String longOption;
70/** True iff option requires an argument */71privateboolean hasArgument;
72/** Description of the option */73private String description;
74/**75 * Message to print if the option is missing, null if the option is not76 * required77 */78private String missingMessage;
7980/**81 * Constructor for option which is not required82 *83 * @param shortOption Short option84 * @param longOption Long option85 * @param hasArgument True iff option requires argument86 * @param description Description of the option87 */88publicBenchmarkOption(String shortOption, String longOption,
89boolean hasArgument, String description) {
90this(shortOption, longOption, hasArgument, description, null);
91 }
9293/**94 * Constructor for option which is not required95 *96 * @param shortOption Short option97 * @param longOption Long option98 * @param hasArgument True iff option requires argument99 * @param description Description of the option100 * @param missingMessage Message to print if the option is missing101 */102publicBenchmarkOption(String shortOption, String longOption,
103boolean hasArgument, String description, String missingMessage) {
104this.shortOption = shortOption;
105this.longOption = longOption;
106this.hasArgument = hasArgument;
107this.description = description;
108this.missingMessage = missingMessage;
109 }
110111/**112 * Check if the option is required113 *114 * @return True iff the option is required115 */116publicboolean isRequired() {
117return missingMessage != null;
118 }
119120/**121 * Add option to cli Options122 *123 * @param options Cli Options124 */125publicvoid addToOptions(Options options) {
126 options.addOption(shortOption, longOption, hasArgument, description);
127 }
128129/**130 * If option is not required just return true.131 * If option is required, check if it's present in CommandLine,132 * and if it's not print the missingMessage to log.133 *134 * @param cmd CommandLine135 * @param log Logger to print the missing message to136 * @return False iff the option is required but is not specified in cmd137 */138publicboolean checkOption(CommandLine cmd, Logger log) {
139if (!isRequired()) {
140returntrue;
141 }
142if (!cmd.hasOption(shortOption)) {
143 log.info(missingMessage);
144return false;
145 }
146returntrue;
147 }
148149/**150 * Check if the option is present in CommandLine151 *152 * @param cmd CommandLine153 * @return True iff the option is present in CommandLine154 */155publicboolean optionTurnedOn(CommandLine cmd) {
156return cmd.hasOption(shortOption);
157 }
158159/**160 * Retrieve the argument, if any, of this option161 *162 * @param cmd CommandLine163 * @return Value of the argument if option is set and has an argument,164 * otherwise null165 */166public String getOptionValue(CommandLine cmd) {
167return cmd.getOptionValue(shortOption);
168 }
169170/**171 * Retrieve the argument of this option as integer value172 *173 * @param cmd CommandLine174 * @return Value of the argument as integer value175 */176publicint getOptionIntValue(CommandLine cmd) {
177return Integer.parseInt(getOptionValue(cmd));
178 }
179180/**181 * Retrieve the argument of this option as integer value,182 * or default value if option is not set183 *184 * @param cmd CommandLine185 * @param defaultValue Default value186 * @return Value of the argument as integer value,187 * or default value if option is not set188 */189publicint getOptionIntValue(CommandLine cmd, int defaultValue) {
190return optionTurnedOn(cmd) ? getOptionIntValue(cmd) : defaultValue;
191 }
192193/**194 * Retrieve the argument of this option as long value195 *196 * @param cmd CommandLine197 * @return Value of the argument as long value198 */199publiclong getOptionLongValue(CommandLine cmd) {
200return Long.parseLong(getOptionValue(cmd));
201 }
202203/**204 * Retrieve the argument of this option as float value205 *206 * @param cmd CommandLine207 * @return Value of the argument as float value208 */209publicfloat getOptionFloatValue(CommandLine cmd) {
210return Float.parseFloat(getOptionValue(cmd));
211 }
212213/**214 * Retrieve the argument of this option as float value,215 * or default value if option is not set216 *217 * @param cmd CommandLine218 * @param defaultValue Default value219 * @return Value of the argument as float value,220 * or default value if option is not set221 */222publicfloat getOptionFloatValue(CommandLine cmd, float defaultValue) {
223return optionTurnedOn(cmd) ? getOptionFloatValue(cmd) : defaultValue;
224 }
225 }