blob: 8d2d4b13c8f98d76f8e0fc5aaa3109c272ae9b01 [file] [log] [blame]
Jesse Wilson1440b362009-12-15 18:54:02 -08001/*
2 * Copyright (C) 2009 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.caliper;
18
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
21
22import java.lang.reflect.Method;
23import java.lang.reflect.Modifier;
24import java.lang.reflect.Type;
25import java.util.Arrays;
26import java.util.Collection;
27import java.util.Map;
28import java.util.Set;
29
30/**
31 * A convenience class for implementing benchmarks in plain code.
32 * Implementing classes must have a no-arguments constructor.
33 *
34 * <h3>Benchmarks</h3>
35 * The benchmarks of a suite are defined by . They may be
36 * static. They are not permitted to take parameters . . ..
37 *
38 * <h3>Parameters</h3>
39 * Implementing classes may be configured using parameters. Each parameter is a
40 * property of a benchmark, plus the default values that fulfill it. Parameters
41 * are specified by annotated fields:
42 * <pre>
43 * {@literal @}Param int length;
44 * </pre>
45 * The available values for a parameter are specified by another field with the
46 * same name plus the {@code Values} suffix. The type of this field must be an
47 * {@code Iterable} of the parameter's type.
48 * <pre>
49 * Iterable&lt;Integer&gt; lengthValues = Arrays.asList(10, 100, 1000, 10000);
50 * </pre>
51 * Alternatively, the available values may be specified with a method. The
52 * method's name follows the same naming convention and returns the same type.
53 * Such methods may not accept parameters of their own.
54 * <pre>
55 * Iterable&lt;Integer&gt; lengthValues() {
56 * return Arrays.asList(10, 100, 1000, 10000);
57 * }
58 * </pre>
59 */
60public abstract class SimpleBenchmark implements Benchmark {
61
62 private static final Class<?>[] ARGUMENT_TYPES = { int.class };
63
64 private final Map<String, Parameter<?>> parameters;
65 private final Map<String, Method> methods;
66
67 protected SimpleBenchmark() {
68 parameters = Parameter.forClass(getClass());
69 methods = createTimedMethods();
70
71 if (methods.isEmpty()) {
72 throw new ConfigurationException(
73 "No benchmarks defined in " + getClass().getName());
74 }
75 }
76
77 protected void setUp() throws Exception {}
78
79 public Set<String> parameterNames() {
80 return ImmutableSet.<String>builder()
81 .add("benchmark")
82 .addAll(parameters.keySet())
83 .build();
84 }
85
86 public Set<String> parameterValues(String parameterName) {
87 if ("benchmark".equals(parameterName)) {
88 return methods.keySet();
89 }
90
91 try {
92 TypeConverter typeConverter = new TypeConverter();
93 Parameter<?> parameter = parameters.get(parameterName);
94 if (parameter == null) {
95 throw new IllegalArgumentException();
96 }
97 Collection<?> values = parameter.values();
98 Type type = parameter.getType();
99
100 ImmutableSet.Builder<String> result = ImmutableSet.builder();
101 for (Object value : values) {
102 result.add(typeConverter.toString(value, type));
103 }
104 return result.build();
105 } catch (Exception e) {
106 throw new ExecutionException(e);
107 }
108 }
109
110 public TimedRunnable createBenchmark(Map<String, String> parameterValues) {
111 TypeConverter typeConverter = new TypeConverter();
112
113 if (!parameterNames().equals(parameterValues.keySet())) {
114 throw new IllegalArgumentException("Invalid parameters specified. Expected "
115 + parameterNames() + " but was " + parameterValues.keySet());
116 }
117
118 try {
119 final SimpleBenchmark copyOfSelf = getClass().newInstance();
120 final Method method = methods.get(parameterValues.get("benchmark"));
121
122 for (Map.Entry<String, String> entry : parameterValues.entrySet()) {
123 String parameterName = entry.getKey();
124 if ("benchmark".equals(parameterName)) {
125 continue;
126 }
127
128 Parameter parameter = parameters.get(parameterName);
129 Object value = typeConverter.fromString(entry.getValue(), parameter.getType());
130 parameter.set(copyOfSelf, value);
131 }
132 copyOfSelf.setUp();
133
134 return new TimedRunnable() {
135 public Object run(int reps) throws Exception {
136 return method.invoke(copyOfSelf, reps);
137 }
138 };
139
140 } catch (Exception e) {
141 throw new ExecutionException(e);
142 }
143 }
144
145 /**
146 * Returns a spec for each benchmark defined in the specified class. The
147 * returned specs have no parameter values; those must be added separately.
148 */
149 private Map<String, Method> createTimedMethods() {
150 ImmutableMap.Builder<String, Method> result = ImmutableMap.builder();
151 for (final Method method : getClass().getDeclaredMethods()) {
152 int modifiers = method.getModifiers();
153 if (!method.getName().startsWith("time")) {
154 continue;
155 }
156
157 if (!Modifier.isPublic(modifiers)
158 || Modifier.isStatic(modifiers)
159 || Modifier.isAbstract(modifiers)
160 || !Arrays.equals(method.getParameterTypes(), ARGUMENT_TYPES)) {
161 throw new ConfigurationException("Timed methods must be public, "
162 + "non-static, non-abstract and take a single int parameter. "
163 + "But " + method + " violates these requirements.");
164 }
165
166 result.put(method.getName().substring(4), method);
167 }
168
169 return result.build();
170 }
171}