Package com.google.caliper.runner

Source Code of com.google.caliper.runner.ExperimentingRunnerModuleTest$FakeInstrument

/*
* Copyright (C) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.caliper.runner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import com.google.caliper.Benchmark;
import com.google.caliper.options.CaliperOptions;
import com.google.caliper.runner.Instrument.Instrumentation;
import com.google.caliper.worker.Worker;
import com.google.common.collect.ImmutableSet;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.lang.reflect.Method;

/**
* Tests {@link ExperimentingRunnerModule}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ExperimentingRunnerModuleTest {
  private ExperimentingRunnerModule module = new ExperimentingRunnerModule();
  private Instrument instrumentA = new FakeInstrument();
  private Instrument instrumentB = new FakeInstrument();

  @Mock CaliperOptions options;

  private Method methodA;
  private Method methodB;
  private Method methodC;

  @Before public void setUp() throws Exception {
    methodA = TestBenchmark.class.getDeclaredMethod("a");
    methodB = TestBenchmark.class.getDeclaredMethod("b");
    methodC = TestBenchmark.class.getDeclaredMethod("c");
  }

  @Test public void provideInstrumentations_noNames() throws Exception {
    when(options.benchmarkMethodNames()).thenReturn(ImmutableSet.<String>of());
    assertEquals(
        new ImmutableSet.Builder<Instrumentation>()
            .add(instrumentA.createInstrumentation(methodA))
            .add(instrumentA.createInstrumentation(methodB))
            .add(instrumentA.createInstrumentation(methodC))
            .add(instrumentB.createInstrumentation(methodA))
            .add(instrumentB.createInstrumentation(methodB))
            .add(instrumentB.createInstrumentation(methodC))
            .build(),
        module.provideInstrumentations(options,
            BenchmarkClass.forClass(TestBenchmark.class),
            ImmutableSet.of(instrumentA, instrumentB)));
  }

  @SuppressWarnings("unchecked")
  @Test public void provideInstrumentations_withNames() throws Exception {
    when(options.benchmarkMethodNames()).thenReturn(ImmutableSet.of("b"),
        ImmutableSet.of("a", "c"));
    assertEquals(
        new ImmutableSet.Builder<Instrumentation>()
            .add(instrumentA.createInstrumentation(methodB))
            .add(instrumentB.createInstrumentation(methodB))
            .build(),
        module.provideInstrumentations(options,
            BenchmarkClass.forClass(TestBenchmark.class),
            ImmutableSet.of(instrumentA, instrumentB)));
    assertEquals(
        new ImmutableSet.Builder<Instrumentation>()
            .add(instrumentA.createInstrumentation(methodA))
            .add(instrumentA.createInstrumentation(methodC))
            .add(instrumentB.createInstrumentation(methodA))
            .add(instrumentB.createInstrumentation(methodC))
            .build(),
        module.provideInstrumentations(options,
            BenchmarkClass.forClass(TestBenchmark.class),
            ImmutableSet.of(instrumentA, instrumentB)));
  }

  static final class TestBenchmark {
    @Benchmark void a() {}
    @Benchmark void b() {}
    @Benchmark void c() {}
  }

  static final class FakeInstrument extends Instrument {
    @Override public boolean isBenchmarkMethod(Method method) {
      return true;
    }

    @Override
    public Instrumentation createInstrumentation(Method benchmarkMethod)
        throws InvalidBenchmarkException {
      return new Instrumentation(benchmarkMethod) {
        @Override
        public Class<? extends Worker> workerClass() {
          throw new UnsupportedOperationException();
        }

        @Override
        public void dryRun(Object benchmark) throws InvalidBenchmarkException {}

        @Override
        MeasurementCollectingVisitor getMeasurementCollectingVisitor() {
          throw new UnsupportedOperationException();
        }
      };
    }
  }
}
TOP

Related Classes of com.google.caliper.runner.ExperimentingRunnerModuleTest$FakeInstrument

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.