Skip to content

Commit

Permalink
Make MetricFactory.loadMetric non-static.
Browse files Browse the repository at this point in the history
  • Loading branch information
axel22 committed Jan 25, 2022
1 parent 0f811a0 commit 72d9304
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class MetricFactory {

public static void loadMetric(Config config, String name) {
public void loadMetric(Config config, String name) {
switch (name) {
case "peak-time":
config.metric = new PeakTimeMetric();
Expand All @@ -53,15 +53,24 @@ public static void loadMetric(Config config, String name) {
default:
String className = classNameFor(name);
try {
Class<?> cls = Class.forName(className);
Class<?> cls;
try {
cls = Class.forName("org.graalvm.polybench." + className);
} catch (ClassNotFoundException e) {
cls = Class.forName("com.oracle.graalvm.polybench." + className);
}
config.metric = (Metric) cls.getConstructor().newInstance();
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | ClassCastException e) {
throw new IllegalArgumentException("Unknown metric: " + name + " (" + e.getClass().getSimpleName() + ", " + e.getMessage() + ")");
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | ClassCastException | ClassNotFoundException e) {
throw failureException(name, e);
}
}
}

private static String classNameFor(String metricName) {
private IllegalArgumentException failureException(String name, Exception e) {
return new IllegalArgumentException("Unknown metric: " + name + " (" + e.getClass().getSimpleName() + ", " + e.getMessage() + ")");
}

private String classNameFor(String metricName) {
String[] words = metricName.split("-");
StringBuilder result = new StringBuilder(Character.toUpperCase(words[0].charAt(0)) + words[0].substring(1));
for (int i = 1; i < words.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static class ArgumentParser {
this.consumers.add(new ArgumentConsumer("--path", (value, config) -> config.path = value));
this.consumers.add(new ArgumentConsumer("--class-name", (value, config) -> config.className = value));
this.consumers.add(new ArgumentConsumer("--mode", (value, config) -> config.mode = Config.Mode.parse(value)));
this.consumers.add(new ArgumentConsumer("--metric", (value, config) -> MetricFactory.loadMetric(config, value)));
this.consumers.add(new ArgumentConsumer("--metric", (value, config) -> (new MetricFactory()).loadMetric(config, value)));
this.consumers.add(new ArgumentConsumer("-w", (value, config) -> config.warmupIterations = Integer.parseInt(value)));
this.consumers.add(new ArgumentConsumer("-i", (value, config) -> config.iterations = Integer.parseInt(value)));
this.consumers.add(new ArgumentConsumer("--shared-engine", (value, config) -> config.initMultiEngine().sharedEngine = Boolean.parseBoolean(value)));
Expand Down

0 comments on commit 72d9304

Please sign in to comment.