This project has retired. For details please refer to its Attic page.
GiraphBenchmark xref
View Javadoc

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.CommandLineParser;
23  import org.apache.commons.cli.HelpFormatter;
24  import org.apache.commons.cli.Options;
25  import org.apache.commons.cli.PosixParser;
26  import org.apache.giraph.conf.GiraphConfiguration;
27  import org.apache.giraph.job.GiraphJob;
28  import org.apache.giraph.utils.LogVersions;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.util.Tool;
31  import org.apache.log4j.Logger;
32  
33  import java.util.Set;
34  
35  /**
36   * Abstract class which benchmarks should extend.
37   */
38  public abstract class GiraphBenchmark implements Tool {
39    /** Class logger */
40    private static final Logger LOG = Logger.getLogger(GiraphBenchmark.class);
41    /** Configuration */
42    private Configuration conf;
43  
44    @Override
45    public void setConf(Configuration conf) {
46      this.conf = conf;
47    }
48  
49    @Override
50    public Configuration getConf() {
51      return conf;
52    }
53  
54    @Override
55    public int 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();
61      for (BenchmarkOption giraphOption : giraphOptions) {
62        giraphOption.addToOptions(options);
63      }
64  
65      HelpFormatter formatter = new HelpFormatter();
66      if (args.length == 0) {
67        formatter.printHelp(getClass().getName(), options, true);
68        return 0;
69      }
70      CommandLineParser parser = new PosixParser();
71      CommandLine cmd = parser.parse(options, args);
72      for (BenchmarkOption giraphOption : giraphOptions) {
73        if (!giraphOption.checkOption(cmd, LOG)) {
74          return -1;
75        }
76      }
77      if (BenchmarkOption.HELP.optionTurnedOn(cmd)) {
78        formatter.printHelp(getClass().getName(), options, true);
79        return 0;
80      }
81  
82      GiraphJob job = new GiraphJob(getConf(), getClass().getName());
83      int workers = Integer.parseInt(BenchmarkOption.WORKERS.getOptionValue(cmd));
84  
85      GiraphConfiguration giraphConf = job.getConfiguration();
86      giraphConf.addWorkerObserverClass(LogVersions.class);
87      giraphConf.addMasterObserverClass(LogVersions.class);
88  
89      giraphConf.setWorkerConfiguration(workers, workers, 100.0f);
90      prepareConfiguration(giraphConf, cmd);
91  
92      boolean isVerbose = false;
93      if (BenchmarkOption.VERBOSE.optionTurnedOn(cmd)) {
94        isVerbose = true;
95      }
96      if (job.run(isVerbose)) {
97        return 0;
98      } else {
99        return -1;
100     }
101   }
102 
103   /**
104    * Get the options to use in this benchmark.
105    * BenchmarkOption.VERBOSE, BenchmarkOption.HELP and BenchmarkOption.WORKERS
106    * will be added automatically, so you don't have to specify those.
107    *
108    * @return Options to use in this benchmark
109    */
110   public abstract Set<BenchmarkOption> getBenchmarkOptions();
111 
112   /**
113    * Process options from CommandLine and prepare configuration for running
114    * the job.
115    * BenchmarkOption.VERBOSE, BenchmarkOption.HELP and BenchmarkOption.WORKERS
116    * will be processed automatically so you don't have to process them.
117    *
118    * @param conf Configuration
119    * @param cmd Command line
120    */
121   protected abstract void prepareConfiguration(GiraphConfiguration conf,
122       CommandLine cmd);
123 }