• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 Google Inc.
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 com.google.caliper.runner;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import static org.mockito.Mockito.when;
23 
24 import com.google.caliper.Benchmark;
25 import com.google.caliper.options.CaliperOptions;
26 import com.google.caliper.platform.Platform;
27 import com.google.caliper.platform.SupportedPlatform;
28 import com.google.caliper.runner.Instrument.Instrumentation;
29 import com.google.caliper.worker.Worker;
30 import com.google.common.collect.ImmutableSet;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 
38 import java.lang.reflect.Method;
39 
40 /**
41  * Tests {@link ExperimentingRunnerModule}.
42  */
43 @RunWith(MockitoJUnitRunner.class)
44 public class ExperimentingRunnerModuleTest {
45   private ExperimentingRunnerModule module = new ExperimentingRunnerModule();
46   private Instrument instrumentA = new FakeInstrument();
47   private Instrument instrumentB = new FakeInstrument();
48 
49   @Mock CaliperOptions options;
50 
51   private Method methodA;
52   private Method methodB;
53   private Method methodC;
54 
setUp()55   @Before public void setUp() throws Exception {
56     methodA = TestBenchmark.class.getDeclaredMethod("a");
57     methodB = TestBenchmark.class.getDeclaredMethod("b");
58     methodC = TestBenchmark.class.getDeclaredMethod("c");
59   }
60 
provideInstrumentations_noNames()61   @Test public void provideInstrumentations_noNames() throws Exception {
62     when(options.benchmarkMethodNames()).thenReturn(ImmutableSet.<String>of());
63     assertEquals(
64         new ImmutableSet.Builder<Instrumentation>()
65             .add(instrumentA.createInstrumentation(methodA))
66             .add(instrumentA.createInstrumentation(methodB))
67             .add(instrumentA.createInstrumentation(methodC))
68             .add(instrumentB.createInstrumentation(methodA))
69             .add(instrumentB.createInstrumentation(methodB))
70             .add(instrumentB.createInstrumentation(methodC))
71             .build(),
72         module.provideInstrumentations(options,
73             BenchmarkClass.forClass(TestBenchmark.class),
74             ImmutableSet.of(instrumentA, instrumentB)));
75   }
76 
77   @SuppressWarnings("unchecked")
provideInstrumentations_withNames()78   @Test public void provideInstrumentations_withNames() throws Exception {
79     when(options.benchmarkMethodNames()).thenReturn(ImmutableSet.of("b"),
80         ImmutableSet.of("a", "c"));
81     assertEquals(
82         new ImmutableSet.Builder<Instrumentation>()
83             .add(instrumentA.createInstrumentation(methodB))
84             .add(instrumentB.createInstrumentation(methodB))
85             .build(),
86         module.provideInstrumentations(options,
87             BenchmarkClass.forClass(TestBenchmark.class),
88             ImmutableSet.of(instrumentA, instrumentB)));
89     assertEquals(
90         new ImmutableSet.Builder<Instrumentation>()
91             .add(instrumentA.createInstrumentation(methodA))
92             .add(instrumentA.createInstrumentation(methodC))
93             .add(instrumentB.createInstrumentation(methodA))
94             .add(instrumentB.createInstrumentation(methodC))
95             .build(),
96         module.provideInstrumentations(options,
97             BenchmarkClass.forClass(TestBenchmark.class),
98             ImmutableSet.of(instrumentA, instrumentB)));
99   }
100 
provideInstrumentations_withInvalidName()101   @Test public void provideInstrumentations_withInvalidName() {
102     when(options.benchmarkMethodNames()).thenReturn(
103         ImmutableSet.of("a", "c", "bad"));
104     try {
105       module.provideInstrumentations(options,
106           BenchmarkClass.forClass(TestBenchmark.class),
107           ImmutableSet.of(instrumentA, instrumentB));
108       fail("should have thrown for invalid benchmark method name");
109     } catch (Exception expected) {
110       assertTrue(expected.getMessage().contains("[bad]"));
111     }
112   }
113 
114   static final class TestBenchmark {
a()115     @Benchmark void a() {}
b()116     @Benchmark void b() {}
c()117     @Benchmark void c() {}
118   }
119 
120   @SupportedPlatform(Platform.Type.JVM)
121   static final class FakeInstrument extends Instrument {
isBenchmarkMethod(Method method)122     @Override public boolean isBenchmarkMethod(Method method) {
123       return true;
124     }
125 
126     @Override
createInstrumentation(Method benchmarkMethod)127     public Instrumentation createInstrumentation(Method benchmarkMethod)
128         throws InvalidBenchmarkException {
129       return new Instrumentation(benchmarkMethod) {
130         @Override
131         public Class<? extends Worker> workerClass() {
132           throw new UnsupportedOperationException();
133         }
134 
135         @Override
136         public void dryRun(Object benchmark) throws InvalidBenchmarkException {}
137 
138         @Override
139         MeasurementCollectingVisitor getMeasurementCollectingVisitor() {
140           throw new UnsupportedOperationException();
141         }
142       };
143     }
144 
schedulingPolicy()145     @Override public TrialSchedulingPolicy schedulingPolicy() {
146       return TrialSchedulingPolicy.SERIAL;
147     }
148   }
149 }
150