1 /* 2 * Copyright (C) 2009 The Android Open Source Project 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 17 package vogar.target; 18 19 import com.google.caliper.runner.CaliperMain; 20 import com.google.common.collect.ImmutableList; 21 import java.io.PrintWriter; 22 import vogar.Result; 23 import vogar.monitor.TargetMonitor; 24 25 /** 26 * Runs a <a href="http://code.google.com/p/caliper/">Caliper</a> benchmark. 27 */ 28 public final class CaliperRunner implements vogar.target.Runner { 29 30 private final TargetMonitor monitor; 31 private final boolean profile; 32 private final Class<?> testClass; 33 private final String[] args; 34 CaliperRunner(TargetMonitor monitor, boolean profile, Class<?> testClass, String[] args)35 public CaliperRunner(TargetMonitor monitor, boolean profile, Class<?> testClass, 36 String[] args) { 37 this.monitor = monitor; 38 this.profile = profile; 39 this.testClass = testClass; 40 this.args = args; 41 } 42 run(Profiler profiler)43 public boolean run(Profiler profiler) { 44 monitor.outcomeStarted(getClass(), testClass.getName()); 45 ImmutableList.Builder<String> builder = ImmutableList.<String>builder() 46 .add(testClass.getName()) 47 .add(args); 48 49 // Make sure that the results are output to the correct location so that vogar will 50 // copy them back to the ./vogar-results/ directory. 51 builder.add("-Cresults.file.options.dir=" + System.getProperty("java.io.tmpdir")); 52 53 // TODO(paulduffin): Remove once caliper supports suitable defaults for Android. 54 // Temporary change to force caliper to use a heap of 256M for each of it's workers when 55 // running on Android. 56 if (System.getProperty("java.specification.name").equals("Dalvik Core Library")) { 57 builder.add("-Cvm.args=-Xmx256M -Xms256M"); 58 } 59 60 if (profile) { 61 // The --dry-run option causes Caliper to run the benchmark once, rather than hundreds 62 // if not thousands of times, and to run it in the main Caliper process rather than in 63 // a separate Worker process, one for each benchmark run. That is needed when profiling 64 // as otherwise the profiler just profiles the main Caliper process rather than the 65 // benchmark. 66 builder.add("--dry-run"); 67 } 68 ImmutableList<String> argList = builder.build(); 69 String[] arguments = argList.toArray(new String[argList.size()]); 70 try { 71 if (profiler != null) { 72 profiler.start(); 73 } 74 PrintWriter stdout = new PrintWriter(System.out); 75 PrintWriter stderr = new PrintWriter(System.err); 76 CaliperMain.exitlessMain(arguments, stdout, stderr); 77 } catch (Exception ex) { 78 ex.printStackTrace(); 79 } finally { 80 if (profiler != null) { 81 profiler.stop(); 82 } 83 } 84 monitor.outcomeFinished(Result.SUCCESS); 85 return true; 86 } 87 } 88