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.CommandLineParser;
23import org.apache.commons.cli.HelpFormatter;
24import org.apache.commons.cli.Options;
25import org.apache.commons.cli.PosixParser;
26import org.apache.giraph.conf.GiraphConfiguration;
27import org.apache.giraph.job.GiraphJob;
28import org.apache.giraph.utils.LogVersions;
29import org.apache.hadoop.conf.Configuration;
30import org.apache.hadoop.util.Tool;
31import org.apache.log4j.Logger;
3233import java.util.Set;
3435/**36 * Abstract class which benchmarks should extend.37 */38publicabstractclassGiraphBenchmarkimplements Tool {
39/** Class logger */40privatestaticfinal Logger LOG = Logger.getLogger(GiraphBenchmark.class);
41/** Configuration */42private Configuration conf;
4344 @Override
45publicvoid setConf(Configuration conf) {
46this.conf = conf;
47 }
4849 @Override
50public Configuration getConf() {
51return conf;
52 }
5354 @Override
55publicint run(String[] args) throws Exception {
56 Set<BenchmarkOption> giraphOptions = getBenchmarkOptions();
57 giraphOptions.add(BenchmarkOption.HELP);
58 giraphOptions.add(BenchmarkOption.VERBOSE);
59 giraphOptions.add(BenchmarkOption.WORKERS);
60 Options options = new Options();
61for (BenchmarkOption giraphOption : giraphOptions) {
62 giraphOption.addToOptions(options);
63 }
6465 HelpFormatter formatter = new HelpFormatter();
66if (args.length == 0) {
67 formatter.printHelp(getClass().getName(), options, true);
68return 0;
69 }
70 CommandLineParser parser = new PosixParser();
71 CommandLine cmd = parser.parse(options, args);
72for (BenchmarkOption giraphOption : giraphOptions) {
73if (!giraphOption.checkOption(cmd, LOG)) {
74return -1;
75 }
76 }
77if (BenchmarkOption.HELP.optionTurnedOn(cmd)) {
78 formatter.printHelp(getClass().getName(), options, true);
79return 0;
80 }
8182GiraphJob job = newGiraphJob(getConf(), getClass().getName());
83int workers = Integer.parseInt(BenchmarkOption.WORKERS.getOptionValue(cmd));
8485GiraphConfiguration giraphConf = job.getConfiguration();
86 giraphConf.addWorkerObserverClass(LogVersions.class);
87 giraphConf.addMasterObserverClass(LogVersions.class);
8889 giraphConf.setWorkerConfiguration(workers, workers, 100.0f);
90 prepareConfiguration(giraphConf, cmd);
9192boolean isVerbose = false;
93if (BenchmarkOption.VERBOSE.optionTurnedOn(cmd)) {
94 isVerbose = true;
95 }
96if (job.run(isVerbose)) {
97return 0;
98 } else {
99return -1;
100 }
101 }
102103/**104 * Get the options to use in this benchmark.105 * BenchmarkOption.VERBOSE, BenchmarkOption.HELP and BenchmarkOption.WORKERS106 * will be added automatically, so you don't have to specify those.107 *108 * @return Options to use in this benchmark109 */110publicabstract Set<BenchmarkOption> getBenchmarkOptions();
111112/**113 * Process options from CommandLine and prepare configuration for running114 * the job.115 * BenchmarkOption.VERBOSE, BenchmarkOption.HELP and BenchmarkOption.WORKERS116 * will be processed automatically so you don't have to process them.117 *118 * @param conf Configuration119 * @param cmd Command line120 */121protectedabstractvoid prepareConfiguration(GiraphConfiguration conf,
122 CommandLine cmd);
123 }