| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
package org.perfidix; |
| 28 | |
|
| 29 | |
import java.lang.reflect.Method; |
| 30 | |
import java.util.ArrayList; |
| 31 | |
import java.util.HashMap; |
| 32 | |
import java.util.Hashtable; |
| 33 | |
import java.util.LinkedHashSet; |
| 34 | |
import java.util.List; |
| 35 | |
import java.util.Map; |
| 36 | |
import java.util.Random; |
| 37 | |
import java.util.Set; |
| 38 | |
|
| 39 | |
import org.perfidix.AbstractConfig.StandardConfig; |
| 40 | |
import org.perfidix.annotation.AfterBenchClass; |
| 41 | |
import org.perfidix.annotation.BeforeBenchClass; |
| 42 | |
import org.perfidix.annotation.Bench; |
| 43 | |
import org.perfidix.element.AbstractMethodArrangement; |
| 44 | |
import org.perfidix.element.BenchmarkElement; |
| 45 | |
import org.perfidix.element.BenchmarkExecutor; |
| 46 | |
import org.perfidix.element.BenchmarkMethod; |
| 47 | |
import org.perfidix.exceptions.PerfidixMethodCheckException; |
| 48 | |
import org.perfidix.exceptions.PerfidixMethodInvocationException; |
| 49 | |
import org.perfidix.meter.AbstractMeter; |
| 50 | |
import org.perfidix.result.BenchmarkResult; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public final class Benchmark { |
| 91 | |
|
| 92 | |
|
| 93 | |
private transient final Set<Class<?>> clazzes; |
| 94 | |
|
| 95 | |
|
| 96 | |
private transient final Set<Object> objects; |
| 97 | |
|
| 98 | |
|
| 99 | 5 | private transient static final Random RAN = new Random(); |
| 100 | |
|
| 101 | |
|
| 102 | |
private transient final AbstractConfig conf; |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | 65 | public Benchmark(final AbstractConfig paramConf) { |
| 111 | 65 | conf = paramConf; |
| 112 | 65 | this.clazzes = new LinkedHashSet<Class<?>>(); |
| 113 | 65 | this.objects = new LinkedHashSet<Object>(); |
| 114 | |
|
| 115 | 65 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public Benchmark() { |
| 121 | 5 | this(new StandardConfig()); |
| 122 | 5 | } |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public void add(final Class<?> clazz) { |
| 132 | 75 | if (this.clazzes.contains(clazz)) { |
| 133 | 10 | throw new IllegalArgumentException("Only one class-instance per benchmark allowed"); |
| 134 | |
} else { |
| 135 | 65 | this.clazzes.add(clazz); |
| 136 | |
} |
| 137 | 65 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public void add(final Object obj) { |
| 147 | 25 | final Class<?> clazz = obj.getClass(); |
| 148 | |
|
| 149 | 25 | if (this.clazzes.contains(clazz)) { |
| 150 | 5 | throw new IllegalArgumentException("Only one class-instance per benchmark allowed"); |
| 151 | |
} else { |
| 152 | 20 | this.clazzes.add(clazz); |
| 153 | 20 | this.objects.add(obj); |
| 154 | |
} |
| 155 | 20 | } |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
public Map<BenchmarkMethod, Integer> getNumberOfMethodsAndRuns() { |
| 163 | 15 | final Map<BenchmarkMethod, Integer> returnVal = new HashMap<BenchmarkMethod, Integer>(); |
| 164 | 15 | final List<BenchmarkMethod> meths = getBenchmarkMethods(); |
| 165 | 15 | for (final BenchmarkMethod meth : meths) { |
| 166 | 80 | int numberOfRuns = BenchmarkMethod.getNumberOfAnnotatedRuns(meth.getMethodToBench()); |
| 167 | 80 | if (numberOfRuns == Bench.NONE_RUN) { |
| 168 | 5 | numberOfRuns = conf.getRuns(); |
| 169 | |
} |
| 170 | 80 | returnVal.put(meth, numberOfRuns); |
| 171 | 80 | } |
| 172 | 15 | return returnVal; |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public BenchmarkResult run() { |
| 181 | 40 | final BenchmarkResult res = new BenchmarkResult(conf.getListener()); |
| 182 | 40 | BenchmarkExecutor.initialize(conf, res); |
| 183 | |
|
| 184 | |
|
| 185 | 40 | final List<BenchmarkElement> elements = getBenchmarkElements(); |
| 186 | |
|
| 187 | |
|
| 188 | 40 | final AbstractMethodArrangement arrangement = |
| 189 | |
AbstractMethodArrangement.getMethodArrangement(elements, conf.getArrangement()); |
| 190 | |
|
| 191 | |
|
| 192 | 40 | final Map<Class<?>, Object> instantiatedObj = instantiateMethods(res); |
| 193 | |
|
| 194 | |
|
| 195 | 40 | final Map<Class<?>, Object> objectsToExecute = executeBeforeBenchClass(instantiatedObj, res); |
| 196 | |
|
| 197 | |
|
| 198 | 40 | for (final BenchmarkElement elem : arrangement) { |
| 199 | |
|
| 200 | 3975 | if (RAN.nextDouble() < conf.getGcProb()) { |
| 201 | 3975 | System.gc(); |
| 202 | |
} |
| 203 | |
|
| 204 | 3975 | final BenchmarkExecutor exec = BenchmarkExecutor.getExecutor(elem); |
| 205 | |
|
| 206 | 3975 | final Object obj = objectsToExecute.get(elem.getMeth().getMethodToBench().getDeclaringClass()); |
| 207 | |
|
| 208 | 3975 | if (obj != null) { |
| 209 | 3925 | exec.executeBeforeMethods(obj); |
| 210 | 3925 | exec.executeBench(obj); |
| 211 | 3925 | exec.executeAfterMethods(obj); |
| 212 | |
} |
| 213 | 3975 | } |
| 214 | |
|
| 215 | |
|
| 216 | 40 | tearDownObjectsToExecute(objectsToExecute, res); |
| 217 | 40 | return res; |
| 218 | |
} |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
private Map<Class<?>, Object> instantiateMethods(final BenchmarkResult res) { |
| 231 | |
|
| 232 | 40 | final Map<Class<?>, Object> objectsToUse = new Hashtable<Class<?>, Object>(); |
| 233 | |
|
| 234 | |
|
| 235 | 40 | for (final Object obj : this.objects) { |
| 236 | 5 | final Class<?> clazz = obj.getClass(); |
| 237 | 5 | objectsToUse.put(clazz, obj); |
| 238 | 5 | } |
| 239 | |
|
| 240 | |
|
| 241 | 40 | for (final Class<?> clazz : clazzes) { |
| 242 | |
|
| 243 | |
|
| 244 | 50 | if (!objectsToUse.containsKey(clazz)) { |
| 245 | |
try { |
| 246 | 45 | final Object obj = clazz.newInstance(); |
| 247 | 45 | objectsToUse.put(clazz, obj); |
| 248 | |
|
| 249 | 0 | } catch (final InstantiationException e) { |
| 250 | 0 | res.addException(new PerfidixMethodInvocationException(e, BeforeBenchClass.class)); |
| 251 | 0 | } catch (final IllegalAccessException e) { |
| 252 | 0 | res.addException(new PerfidixMethodInvocationException(e, BeforeBenchClass.class)); |
| 253 | 45 | } |
| 254 | |
|
| 255 | |
} |
| 256 | 50 | } |
| 257 | |
|
| 258 | 40 | return objectsToUse; |
| 259 | |
} |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
private Map<Class<?>, Object> executeBeforeBenchClass(final Map<Class<?>, Object> instantiatedObj, |
| 271 | |
final BenchmarkResult res) { |
| 272 | |
|
| 273 | 40 | final Map<Class<?>, Object> returnVal = new Hashtable<Class<?>, Object>(); |
| 274 | |
|
| 275 | |
|
| 276 | 40 | for (final Class<?> clazz : instantiatedObj.keySet()) { |
| 277 | |
|
| 278 | 50 | final Object objectToUse = instantiatedObj.get(clazz); |
| 279 | |
|
| 280 | 50 | Method beforeClassMeth = null; |
| 281 | 50 | boolean continueVal = true; |
| 282 | |
try { |
| 283 | 50 | beforeClassMeth = |
| 284 | |
BenchmarkMethod.findAndCheckAnyMethodByAnnotation(clazz, BeforeBenchClass.class); |
| 285 | |
|
| 286 | |
|
| 287 | 0 | } catch (final PerfidixMethodCheckException e) { |
| 288 | 0 | res.addException(e); |
| 289 | 0 | continueVal = false; |
| 290 | 50 | } |
| 291 | |
|
| 292 | 50 | if (continueVal) { |
| 293 | 50 | if (beforeClassMeth == null) { |
| 294 | |
|
| 295 | |
|
| 296 | 15 | returnVal.put(clazz, objectToUse); |
| 297 | |
} else { |
| 298 | |
|
| 299 | |
|
| 300 | 35 | final PerfidixMethodCheckException beforeByCheck = |
| 301 | |
BenchmarkExecutor.checkMethod(objectToUse, BeforeBenchClass.class, beforeClassMeth); |
| 302 | 35 | if (beforeByCheck == null) { |
| 303 | 35 | final PerfidixMethodInvocationException beforeByInvok = |
| 304 | |
BenchmarkExecutor.invokeMethod(objectToUse, BeforeBenchClass.class, |
| 305 | |
beforeClassMeth); |
| 306 | 35 | if (beforeByInvok == null) { |
| 307 | 30 | returnVal.put(clazz, objectToUse); |
| 308 | |
} else { |
| 309 | 5 | res.addException(beforeByInvok); |
| 310 | |
} |
| 311 | 35 | } else { |
| 312 | 0 | res.addException(beforeByCheck); |
| 313 | |
} |
| 314 | |
} |
| 315 | |
} |
| 316 | 50 | } |
| 317 | 40 | return returnVal; |
| 318 | |
} |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
private void tearDownObjectsToExecute(final Map<Class<?>, Object> objects, final BenchmarkResult res) { |
| 330 | |
|
| 331 | |
|
| 332 | 40 | for (final Class<?> clazz : objects.keySet()) { |
| 333 | 45 | final Object objectToUse = objects.get(clazz); |
| 334 | 45 | if (objectToUse != null) { |
| 335 | |
|
| 336 | 45 | Method afterClassMeth = null; |
| 337 | |
try { |
| 338 | 45 | afterClassMeth = |
| 339 | |
BenchmarkMethod.findAndCheckAnyMethodByAnnotation(clazz, AfterBenchClass.class); |
| 340 | 0 | } catch (final PerfidixMethodCheckException e) { |
| 341 | 0 | res.addException(e); |
| 342 | 45 | } |
| 343 | |
|
| 344 | |
|
| 345 | 45 | if (afterClassMeth != null) { |
| 346 | 25 | final PerfidixMethodCheckException afterByCheck = |
| 347 | |
BenchmarkExecutor.checkMethod(objectToUse, AfterBenchClass.class, afterClassMeth); |
| 348 | 25 | if (afterByCheck == null) { |
| 349 | 25 | final PerfidixMethodInvocationException afterByInvok = |
| 350 | |
BenchmarkExecutor |
| 351 | |
.invokeMethod(objectToUse, AfterBenchClass.class, afterClassMeth); |
| 352 | 25 | if (afterByInvok != null) { |
| 353 | 0 | res.addException(afterByInvok); |
| 354 | |
} |
| 355 | 25 | } else { |
| 356 | 0 | res.addException(afterByCheck); |
| 357 | |
} |
| 358 | |
} |
| 359 | |
} |
| 360 | 45 | } |
| 361 | 40 | } |
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
public List<BenchmarkMethod> getBenchmarkMethods() { |
| 369 | |
|
| 370 | 55 | final List<BenchmarkMethod> elems = new ArrayList<BenchmarkMethod>(); |
| 371 | |
|
| 372 | 55 | for (final Class<?> clazz : clazzes) { |
| 373 | 765 | for (final Method meth : clazz.getDeclaredMethods()) { |
| 374 | |
|
| 375 | 680 | if (BenchmarkMethod.isBenchmarkable(meth)) { |
| 376 | 170 | final BenchmarkMethod benchmarkMeth = new BenchmarkMethod(meth); |
| 377 | 170 | elems.add(benchmarkMeth); |
| 378 | |
} |
| 379 | |
} |
| 380 | 85 | } |
| 381 | 55 | return elems; |
| 382 | |
} |
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
public List<BenchmarkElement> getBenchmarkElements() { |
| 391 | |
|
| 392 | 40 | final List<BenchmarkElement> elems = new ArrayList<BenchmarkElement>(); |
| 393 | |
|
| 394 | 40 | final List<BenchmarkMethod> meths = getBenchmarkMethods(); |
| 395 | |
|
| 396 | 40 | for (final BenchmarkMethod meth : meths) { |
| 397 | 90 | int numberOfRuns = BenchmarkMethod.getNumberOfAnnotatedRuns(meth.getMethodToBench()); |
| 398 | 90 | if (numberOfRuns == Bench.NONE_RUN) { |
| 399 | 30 | numberOfRuns = conf.getRuns(); |
| 400 | |
} |
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | 4065 | for (int i = 0; i < numberOfRuns; i++) { |
| 405 | 3975 | elems.add(new BenchmarkElement(meth)); |
| 406 | |
} |
| 407 | 90 | } |
| 408 | |
|
| 409 | 40 | return elems; |
| 410 | |
} |
| 411 | |
} |