/*
 * Copyright (C) 2010 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;

import com.google.caliper.UserException.DoesNotScaleLinearlyException;
import com.google.common.util.concurrent.SimpleTimeLimiter;
import com.google.common.util.concurrent.TimeLimiter;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;

/**
 * Test exposing an issue where any warmup that completes enough executions to reach
 * Integer.MAX_VALUE reps (either because the benchmark code is optimized away or because the
 * warmupMillis are long enough compared to the benchmark execution time).
 */
public class WarmupOverflowTest extends TestCase {
  private TimeLimiter timeLimiter;

  @Override public void setUp() {
    timeLimiter = new SimpleTimeLimiter(Executors.newSingleThreadExecutor());
  }


  public void testOptimizedAwayBenchmarkDoesNotTakeTooLongToRun() throws Exception {
    try {
      timeLimiter.callWithTimeout(new Callable<Void>() {
        @Override public Void call() throws Exception {
          InProcessRunner runner = new InProcessRunner();
          runner.run(OptimizedAwayBenchmark.class.getName(), "--warmupMillis", "3000",
              "--measurementType", "TIME");
          return null;
        }
      }, 90, TimeUnit.SECONDS, false);
    } catch (DoesNotScaleLinearlyException expected) {
    }
  }


  public void testLongWarmupMillisDoesNotTakeTooLongToRun() throws Exception {
    timeLimiter.callWithTimeout(new Callable<Void>() {
      @Override public Void call() throws Exception {
        InProcessRunner runner = new InProcessRunner();
        runner.run(RelativelyFastBenchmark.class.getName(), "--warmupMillis", "8000",
            "--runMillis", "51", "--measurementType", "TIME");
        return null;
      }
    }, 90, TimeUnit.SECONDS, false);
  }

  public static class OptimizedAwayBenchmark extends SimpleBenchmark {
    public void timeIsNullOrEmpty(int reps) {
      for (int i = 0; i < reps; i++) {
        // do nothing!
      }
    }
  }

  public static class RelativelyFastBenchmark extends SimpleBenchmark {
    public long timeSqrt(int reps) {
      long result = 0;
      for(int i = 0; i < reps; i++) {
        result += Math.sqrt(81);
      }
      return result;
    }
  }
}

@@ -622,79 +622,8 @@
 r;%0A%0A
-import com.google.caliper.UserException.DoesNotScaleLinearlyException;%0A
 impo
@@ -1353,544 +1353,8 @@
 %7D%0A%0A%0A
-  public void testOptimizedAwayBenchmarkDoesNotTakeTooLongToRun() throws Exception %7B%0A    try %7B%0A      timeLimiter.callWithTimeout(new Callable%3CVoid%3E() %7B%0A        @Override public Void call() throws Exception %7B%0A          InProcessRunner runner = new InProcessRunner();%0A          runner.run(OptimizedAwayBenchmark.class.getName(), %22--warmupMillis%22, %223000%22,%0A              %22--measurementType%22, %22TIME%22);%0A          return null;%0A        %7D%0A      %7D, 90, TimeUnit.SECONDS, false);%0A    %7D catch (DoesNotScaleLinearlyException expected) %7B%0A    %7D%0A  %7D%0A%0A%0A
   pu

remove flaky test of legacy behavior ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=27892844