Home | History | Annotate | Download | only in target
      1 /*
      2  * Copyright (C) 2015 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 package vogar.target;
     17 
     18 import com.google.caliper.runner.BenchmarkClassChecker;
     19 import com.google.caliper.util.InvalidCommandException;
     20 import java.util.List;
     21 import java.util.concurrent.atomic.AtomicReference;
     22 import javax.annotation.Nullable;
     23 import vogar.monitor.TargetMonitor;
     24 
     25 /**
     26  * Supports treating a class as a Caliper benchmark.
     27  */
     28 public class CaliperRunnerFactory implements RunnerFactory {
     29 
     30     @Nullable
     31     private final BenchmarkClassChecker benchmarkClassChecker;
     32 
     33     public CaliperRunnerFactory(List<String> argsList) {
     34         BenchmarkClassChecker benchmarkClassChecker;
     35         try {
     36             // Command line arguments can affect the set of available instruments so pass that
     37             // information on to the checker. Unfortunately, at this point we do not know whether
     38             // the arguments are actually valid for Caliper, if they are not then this will fail.
     39             // If that happens then we simply have to assume that the classes being tested are not
     40             // Caliper benchmarks.
     41             benchmarkClassChecker = BenchmarkClassChecker.create(argsList);
     42         } catch (InvalidCommandException e) {
     43             // The arguments are invalid for Caliper.
     44             System.out.println("Warning: Arguments are invalid for Caliper: " + e.getMessage());
     45             benchmarkClassChecker = null;
     46         }
     47 
     48         this.benchmarkClassChecker = benchmarkClassChecker;
     49     }
     50 
     51     @Override @Nullable
     52     public TargetRunner newRunner(TargetMonitor monitor, String qualification,
     53             Class<?> klass, AtomicReference<String> skipPastReference,
     54             TestEnvironment testEnvironment, int timeoutSeconds, String[] args) {
     55         if (benchmarkClassChecker != null && benchmarkClassChecker.isBenchmark(klass)) {
     56             return new CaliperTargetRunner(monitor, klass, args);
     57         } else {
     58             return null;
     59         }
     60     }
     61 }
     62