static BenchmarkClass forClass(Class<?> theClass) throws InvalidBenchmarkException { return new BenchmarkClass(theClass); }
public void setUpBenchmark(Object benchmarkInstance) throws UserCodeException { boolean setupSuccess = false; try { callSetUp(benchmarkInstance); setupSuccess = true; } finally { // If setUp fails, we should call tearDown. If this method throws an exception, we // need to call tearDown from here, because no one else has the reference to the // Benchmark. if (!setupSuccess) { callTearDown(benchmarkInstance); } } }
@Provides BenchmarkClass provideBenchmarkClass() throws InvalidBenchmarkException { return BenchmarkClass.forClass(benchmarkClassObject); }
ImmutableSet.Builder<Experiment> builder = ImmutableSet.builder(); for (Experiment experiment : experiments) { Class<?> clazz = benchmarkClass.benchmarkClass(); try { Object benchmark = injector.createChildInjector(ExperimentModule.forExperiment(experiment)) .getInstance(Key.get(clazz)); benchmarkClass.setUpBenchmark(benchmark); try { experiment.instrumentation().dryRun(benchmark); } finally { benchmarkClass.cleanup(benchmark);
@VisibleForTesting static List<String> getJvmArgs(VirtualMachine vm, BenchmarkClass benchmarkClass) { List<String> args = Lists.newArrayList(); String jdkPath = vm.config.javaExecutable().getAbsolutePath(); args.add(jdkPath); logger.fine(String.format("Java(%s) Path: %s", vm.name, jdkPath)); ImmutableList<String> jvmOptions = vm.config.options(); args.addAll(jvmOptions); logger.fine(String.format("Java(%s) args: %s", vm.name, jvmOptions)); ImmutableSet<String> benchmarkJvmOptions = benchmarkClass.vmOptions(); args.addAll(benchmarkJvmOptions); logger.fine(String.format("Benchmark(%s) Java args: %s", benchmarkClass.name(), benchmarkJvmOptions)); String classPath = getClassPath(); Collections.addAll(args, "-cp", classPath); logger.finer(String.format("Class path: %s", classPath)); return args; }
@Provides @AfterExperimentMethods ImmutableSet<Method> provideAfterExperimentMethods( BenchmarkClass benchmarkClass) { return benchmarkClass.afterExperimentMethods(); } }
@Provides @BeforeExperimentMethods ImmutableSet<Method> provideBeforeExperimentMethods( BenchmarkClass benchmarkClass) { return benchmarkClass.beforeExperimentMethods(); }
@Override public String toString() { return name(); }
@Provides ImmutableSet<Instrumentation> provideInstrumentations(CaliperOptions options, BenchmarkClass benchmarkClass, ImmutableSet<Instrument> instruments) throws InvalidBenchmarkException { ImmutableSet.Builder<Instrumentation> builder = ImmutableSet.builder(); ImmutableSet<String> benchmarkMethodNames = options.benchmarkMethodNames(); for (Instrument instrument : instruments) { for (Method method : findAllBenchmarkMethods(benchmarkClass.benchmarkClass(), instrument)) { if (benchmarkMethodNames.isEmpty() || benchmarkMethodNames.contains(method.getName())) { builder.add(instrument.createInstrumentation(method)); } } } return builder.build(); }
public void cleanup(Object benchmark) throws UserCodeException { callTearDown(benchmark); }
private BenchmarkClass(Class<?> theClass) throws InvalidBenchmarkException { this.theClass = checkNotNull(theClass); if (!theClass.getSuperclass().equals(Object.class)) { throw new InvalidBenchmarkException( "%s must not extend any class other than %s. Prefer composition.", theClass, Object.class); } if (Modifier.isAbstract(theClass.getModifiers())) { throw new InvalidBenchmarkException("Class '%s' is abstract", theClass); } // TODO: check for nested, non-static classes (non-abstract, but no constructor?) // this will fail later anyway (no way to declare parameterless nested constr., but // maybe signal this better? this.userParameters = ParameterSet.create(theClass, Param.class); this.benchmarkFlags = getVmOptions(theClass); }
ImmutableSet.Builder<Experiment> builder = ImmutableSet.builder(); for (Experiment experiment : experiments) { Class<?> clazz = benchmarkClass.benchmarkClass(); try { Object benchmark = injector.createChildInjector(ExperimentModule.forExperiment(experiment)) .getInstance(Key.get(clazz)); benchmarkClass.setUpBenchmark(benchmark); try { experiment.instrumentation().dryRun(benchmark); } finally { benchmarkClass.cleanup(benchmark);
@VisibleForTesting static List<String> getJvmArgs(VirtualMachine vm, BenchmarkClass benchmarkClass) { List<String> args = Lists.newArrayList(); String jdkPath = vm.config.javaExecutable().getAbsolutePath(); args.add(jdkPath); logger.fine(String.format("Java(%s) Path: %s", vm.name, jdkPath)); ImmutableList<String> jvmOptions = vm.config.options(); args.addAll(jvmOptions); logger.fine(String.format("Java(%s) args: %s", vm.name, jvmOptions)); ImmutableSet<String> benchmarkJvmOptions = benchmarkClass.vmOptions(); args.addAll(benchmarkJvmOptions); logger.fine(String.format("Benchmark(%s) Java args: %s", benchmarkClass.name(), benchmarkJvmOptions)); String classPath = getClassPath(); Collections.addAll(args, "-cp", classPath); logger.finer(String.format("Class path: %s", classPath)); return args; }
@Provides @AfterExperimentMethods ImmutableSet<Method> provideAfterExperimentMethods( BenchmarkClass benchmarkClass) { return benchmarkClass.afterExperimentMethods(); } }
@Provides @BeforeExperimentMethods ImmutableSet<Method> provideBeforeExperimentMethods( BenchmarkClass benchmarkClass) { return benchmarkClass.beforeExperimentMethods(); }
@Override public String toString() { return name(); }
@Provides ImmutableSet<Instrumentation> provideInstrumentations(CaliperOptions options, BenchmarkClass benchmarkClass, ImmutableSet<Instrument> instruments) throws InvalidBenchmarkException { ImmutableSet.Builder<Instrumentation> builder = ImmutableSet.builder(); ImmutableSet<String> benchmarkMethodNames = options.benchmarkMethodNames(); Set<String> unusedBenchmarkNames = new HashSet<String>(benchmarkMethodNames); for (Instrument instrument : instruments) { for (Method method : findAllBenchmarkMethods(benchmarkClass.benchmarkClass(), instrument)) { if (benchmarkMethodNames.isEmpty() || benchmarkMethodNames.contains(method.getName())) { builder.add(instrument.createInstrumentation(method)); unusedBenchmarkNames.remove(method.getName()); } } } if (!unusedBenchmarkNames.isEmpty()) { throw new InvalidBenchmarkException( "Invalid benchmark method(s) specified in options: " + unusedBenchmarkNames); } return builder.build(); }
public void cleanup(Object benchmark) throws UserCodeException { callTearDown(benchmark); }
private BenchmarkClass(Class<?> theClass) throws InvalidBenchmarkException { this.theClass = checkNotNull(theClass); if (!theClass.getSuperclass().equals(Object.class)) { throw new InvalidBenchmarkException( "%s must not extend any class other than %s. Prefer composition.", theClass, Object.class); } if (Modifier.isAbstract(theClass.getModifiers())) { throw new InvalidBenchmarkException("Class '%s' is abstract", theClass); } // TODO: check for nested, non-static classes (non-abstract, but no constructor?) // this will fail later anyway (no way to declare parameterless nested constr., but // maybe signal this better? this.userParameters = ParameterSet.create(theClass, Param.class); this.benchmarkFlags = getVmOptions(theClass); }
public void setUpBenchmark(Object benchmarkInstance) throws UserCodeException { boolean setupSuccess = false; try { callSetUp(benchmarkInstance); setupSuccess = true; } finally { // If setUp fails, we should call tearDown. If this method throws an exception, we // need to call tearDown from here, because no one else has the reference to the // Benchmark. if (!setupSuccess) { try { callTearDown(benchmarkInstance); } catch (UserCodeException e) { // The exception thrown during setUp shouldn't be lost, as it's probably more // important to the user. logger.log( Level.INFO, "in @AfterExperiment methods called because @BeforeExperiment methods failed", e); } } } }