Home | History | Annotate | Download | only in caliper
      1 /*
      2  * Copyright (C) 2016 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.caliper;
     18 
     19 import com.google.caliper.Benchmark;
     20 import com.google.caliper.runner.UserCodeException;
     21 import org.junit.Test;
     22 import org.junit.runner.RunWith;
     23 import org.junit.runners.JUnit4;
     24 import vogar.RunnerType;
     25 import vogar.target.AbstractTestRunnerTest;
     26 import vogar.target.TestRunner;
     27 import vogar.target.TestRunnerProperties;
     28 
     29 /**
     30  * Tests for using TestRunner to run Caliper classes.
     31  */
     32 @RunWith(JUnit4.class)
     33 public class TestRunnerCaliperTest extends AbstractTestRunnerTest {
     34 
     35     @TestRunnerProperties(testClass = CaliperBenchmarkFailing.class)
     36     @Test
     37     public void testRunner_CaliperBenchmark_NoRunner() throws Exception {
     38         TestRunner runner = testRunnerRule.createTestRunner("-i", "runtime");
     39         runner.run();
     40 
     41         expectedResults()
     42                 .unsupported()
     43                 .completedNormally();
     44     }
     45 
     46     @TestRunnerProperties(testClass = CaliperBenchmarkFailing.class,
     47             runnerType = RunnerType.CALIPER)
     48     @Test
     49     public void testRunner_CaliperBenchmarkFailing() throws Exception {
     50         TestRunner runner = testRunnerRule.createTestRunner("-i", "runtime");
     51         runner.run();
     52 
     53         expectedResults()
     54                 .failure(null, (""
     55                         + "Experiment selection: \n"
     56                         + "  Benchmark Methods:   [failingBenchmark]\n"
     57                         + "  Instruments:   [runtime]\n"
     58                         + "  User parameters:   {}\n"
     59                         + "  Virtual machines:  [default]\n"
     60                         + "  Selection type:    Full cartesian product\n"
     61                         + "\n"
     62                         + "This selection yields 1 experiments.\n"
     63                         + UserCodeException.class.getName()
     64                         + ": An exception was thrown from the benchmark code\n"
     65                         + "Caused by: " + IllegalStateException.class.getName()
     66                         + ": " + CaliperBenchmarkFailing.BENCHMARK_FAILED + "\n"))
     67                 .completedNormally();
     68     }
     69 
     70     public static class CaliperBenchmark {
     71 
     72         @Benchmark
     73         public long timeMethod(long reps) {
     74             System.out.println(reps);
     75             return reps;
     76         }
     77     }
     78 
     79     public static class CaliperBenchmarkFailing {
     80 
     81         static final String BENCHMARK_FAILED = "Benchmark failed";
     82 
     83         @Benchmark
     84         public long failingBenchmark(long reps) {
     85             throw new IllegalStateException(BENCHMARK_FAILED);
     86         }
     87     }
     88 }
     89