1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * 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 and
16 * limitations under the License.
17 */
18
19 package org.apache.giraph.benchmark;
20
21 import org.apache.commons.cli.CommandLine;
22 import org.apache.commons.cli.Options;
23 import org.apache.log4j.Logger;
24
25 /**
26 * Command line options for benchmarks
27 */
28 public class BenchmarkOption {
29 /** Option for help */
30 public static final BenchmarkOption HELP =
31 new BenchmarkOption("h", "help", false, "Help");
32 /** Option for verbose */
33 public static final BenchmarkOption VERBOSE =
34 new BenchmarkOption("v", "verbose", false, "Verbose");
35 /** Option for number of workers */
36 public static final BenchmarkOption WORKERS =
37 new BenchmarkOption("w", "workers", true, "Number of workers",
38 "Need to choose the number of workers (-w)");
39 /** Option for number of supersteps */
40 public static final BenchmarkOption SUPERSTEPS =
41 new BenchmarkOption("s", "supersteps", true,
42 "Supersteps to execute before finishing",
43 "Need to set the number of supersteps (-s)");
44 /** Option for number of vertices */
45 public static final BenchmarkOption VERTICES =
46 new BenchmarkOption("V", "aggregateVertices", true,
47 "Aggregate vertices", "Need to set the aggregate vertices (-V)");
48 /** Option for number of edges per vertex */
49 public static final BenchmarkOption EDGES_PER_VERTEX =
50 new BenchmarkOption("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 */
54 public static final BenchmarkOption LOCAL_EDGES_MIN_RATIO =
55 new BenchmarkOption(
56 "l", "localEdgesMinRatio", true,
57 "Minimum ratio of partition-local edges (default is 0)");
58 /** Option for using Jython */
59 public static final BenchmarkOption JYTHON =
60 new BenchmarkOption("j", "jython", false, "Use jython implementation");
61 /** Option for path to script for computation */
62 public static final BenchmarkOption SCRIPT_PATH =
63 new BenchmarkOption("sp", "scriptPath", true,
64 "Path to script for computation, can be local or HDFS path");
65
66 /** Short option */
67 private String shortOption;
68 /** Long option */
69 private String longOption;
70 /** True iff option requires an argument */
71 private boolean hasArgument;
72 /** Description of the option */
73 private String description;
74 /**
75 * Message to print if the option is missing, null if the option is not
76 * required
77 */
78 private String missingMessage;
79
80 /**
81 * Constructor for option which is not required
82 *
83 * @param shortOption Short option
84 * @param longOption Long option
85 * @param hasArgument True iff option requires argument
86 * @param description Description of the option
87 */
88 public BenchmarkOption(String shortOption, String longOption,
89 boolean hasArgument, String description) {
90 this(shortOption, longOption, hasArgument, description, null);
91 }
92
93 /**
94 * Constructor for option which is not required
95 *
96 * @param shortOption Short option
97 * @param longOption Long option
98 * @param hasArgument True iff option requires argument
99 * @param description Description of the option
100 * @param missingMessage Message to print if the option is missing
101 */
102 public BenchmarkOption(String shortOption, String longOption,
103 boolean hasArgument, String description, String missingMessage) {
104 this.shortOption = shortOption;
105 this.longOption = longOption;
106 this.hasArgument = hasArgument;
107 this.description = description;
108 this.missingMessage = missingMessage;
109 }
110
111 /**
112 * Check if the option is required
113 *
114 * @return True iff the option is required
115 */
116 public boolean isRequired() {
117 return missingMessage != null;
118 }
119
120 /**
121 * Add option to cli Options
122 *
123 * @param options Cli Options
124 */
125 public void addToOptions(Options options) {
126 options.addOption(shortOption, longOption, hasArgument, description);
127 }
128
129 /**
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 CommandLine
135 * @param log Logger to print the missing message to
136 * @return False iff the option is required but is not specified in cmd
137 */
138 public boolean checkOption(CommandLine cmd, Logger log) {
139 if (!isRequired()) {
140 return true;
141 }
142 if (!cmd.hasOption(shortOption)) {
143 log.info(missingMessage);
144 return false;
145 }
146 return true;
147 }
148
149 /**
150 * Check if the option is present in CommandLine
151 *
152 * @param cmd CommandLine
153 * @return True iff the option is present in CommandLine
154 */
155 public boolean optionTurnedOn(CommandLine cmd) {
156 return cmd.hasOption(shortOption);
157 }
158
159 /**
160 * Retrieve the argument, if any, of this option
161 *
162 * @param cmd CommandLine
163 * @return Value of the argument if option is set and has an argument,
164 * otherwise null
165 */
166 public String getOptionValue(CommandLine cmd) {
167 return cmd.getOptionValue(shortOption);
168 }
169
170 /**
171 * Retrieve the argument of this option as integer value
172 *
173 * @param cmd CommandLine
174 * @return Value of the argument as integer value
175 */
176 public int getOptionIntValue(CommandLine cmd) {
177 return Integer.parseInt(getOptionValue(cmd));
178 }
179
180 /**
181 * Retrieve the argument of this option as integer value,
182 * or default value if option is not set
183 *
184 * @param cmd CommandLine
185 * @param defaultValue Default value
186 * @return Value of the argument as integer value,
187 * or default value if option is not set
188 */
189 public int getOptionIntValue(CommandLine cmd, int defaultValue) {
190 return optionTurnedOn(cmd) ? getOptionIntValue(cmd) : defaultValue;
191 }
192
193 /**
194 * Retrieve the argument of this option as long value
195 *
196 * @param cmd CommandLine
197 * @return Value of the argument as long value
198 */
199 public long getOptionLongValue(CommandLine cmd) {
200 return Long.parseLong(getOptionValue(cmd));
201 }
202
203 /**
204 * Retrieve the argument of this option as float value
205 *
206 * @param cmd CommandLine
207 * @return Value of the argument as float value
208 */
209 public float getOptionFloatValue(CommandLine cmd) {
210 return Float.parseFloat(getOptionValue(cmd));
211 }
212
213 /**
214 * Retrieve the argument of this option as float value,
215 * or default value if option is not set
216 *
217 * @param cmd CommandLine
218 * @param defaultValue Default value
219 * @return Value of the argument as float value,
220 * or default value if option is not set
221 */
222 public float getOptionFloatValue(CommandLine cmd, float defaultValue) {
223 return optionTurnedOn(cmd) ? getOptionFloatValue(cmd) : defaultValue;
224 }
225 }