View Javadoc

1   /*
2    * DaCapo Benchmark Maven Plugin
3    * Copyright (C) 2010 Technische Universität Darmstadt
4    * info@scalabench.org
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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  package org.scalabench.plugins.dacapo;
19  
20  import static java.lang.String.format;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.model.Resource;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  import org.codehaus.plexus.util.StringUtils;
34  import org.scalabench.plugins.dacapo.config.BenchmarkConfiguration;
35  import org.scalabench.plugins.dacapo.config.Size;
36  import org.scalabench.plugins.dacapo.util.SizeNameComparator;
37  
38  /**
39   * Generate the benchmark's configuration file (<code>.cnf</code>).
40   * 
41   * While most configuration options need to be configured explicitly, the benchmark's dependency declarations are derived
42   * from the POM; all provided-scoped dependencies are assumed to be dependencies of the benchmark itself (as opposed to
43   * dependencies of the harness).
44   * 
45   * @since 0.1.0
46   * 
47   * @threadSafe
48   * @goal configuration
49   * @phase generate-resources
50   * @requiresDependencyResolution compile
51   */
52  public class ConfigurationMojo extends AbstractDacapoBenchmarkMojo {
53      
54      /**
55       * The benchmark's configuration. If not present, a hand-crafted configuration file is assumed to be present.
56       * 
57       * @parameter
58       */
59      private BenchmarkConfiguration benchmark;
60      
61      /**
62       * The directory where the resources generated by this plugin will be put.
63       * 
64       * @required
65       * @readonly
66       * @parameter expression="${project.build.directory}/generated-resources/dacapo-benchmark"
67       */
68      private File resourceOutputDirectory;
69      
70      @Override
71      public void execute() throws MojoExecutionException, MojoFailureException {
72          if (benchmark == null) {
73              logInfo("No benchmark configuration found. Assuming hand-crafted configuration file is present.");
74          } else {
75              checkBenchmarkConfiguration();
76              generateConfiguratonFile();
77          }
78      }
79      
80      private void checkBenchmarkConfiguration() {
81          checkBenchmarkSizes();
82          checkBenchmarkDescription();
83      }
84      
85      private void checkBenchmarkSizes() {
86          if (benchmark.getSizes().isEmpty()) {
87              logWarn("No input sizes configured for benchmark");
88          } else {
89              logInfo("Input sizes configured for benchmark: %s" , StringUtils.join(getSizeNames().iterator(), ", "));
90          }
91      }
92      
93      private void checkBenchmarkDescription() {
94          if (benchmark.getDescription() == null)
95              logWarn("No benchmark description configured");
96      }
97      
98      /**
99       * Returns the names of the benchmark's input sizes, sorted according to common convention from smallest to largest.
100      * 
101      * @return the sorted list of input size names
102      * 
103      * @see org.scalabench.plugins.dacapo.util.SizeNameComparator
104      */
105     public List<String> getSizeNames() {
106         final List<Size> sizes = benchmark.getSizes();
107         final List<String> sizeNames = new ArrayList<String>(sizes.size());
108         for (Size size : sizes)
109             sizeNames.add(size.getName());
110         Collections.sort(sizeNames, new SizeNameComparator());
111         return sizeNames;
112     }
113     
114     private void generateConfiguratonFile() throws MojoExecutionException {
115         final File configurationDirectory = new File(resourceOutputDirectory, "cnf");
116         final File configurationFile = new File(configurationDirectory, getBenchmarkName() + ".cnf");
117         
118         try {
119             if (!configurationDirectory.exists() && !configurationDirectory.mkdirs())
120                 throw new IOException(format("Cannot create configuration directory: %s", configurationDirectory));
121             
122             final ConfigrationWriter writer = new ConfigrationWriter(
123                     getBenchmarkName(),
124                     benchmark,
125                     getLibraryJars());
126             writer.writeConfiguration(configurationFile);
127             
128             final Resource resource = new Resource();
129             resource.setDirectory(resourceOutputDirectory.getAbsolutePath());
130             getProject().addResource(resource);
131         } catch (IOException e) {
132             throw new MojoExecutionException("Cannot generate configuration file", e);
133         }
134     }
135     
136     private List<String> getLibraryJars() throws MojoExecutionException {
137         final Set<Artifact> libraries = getLibraries();
138         final List<String> libraryJars = new ArrayList<String>(libraries.size());
139         for (final Artifact library : libraries)
140             libraryJars.add(library.getFile().getName());
141         return libraryJars;
142     }
143 }