Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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; |
| 18 | |
| 19 | import com.google.caliper.UserException.DoesNotScaleLinearlyException; |
| 20 | import com.google.common.util.concurrent.SimpleTimeLimiter; |
| 21 | import com.google.common.util.concurrent.TimeLimiter; |
| 22 | import java.util.concurrent.Callable; |
| 23 | import java.util.concurrent.Executors; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | import junit.framework.TestCase; |
| 26 | |
| 27 | /** |
| 28 | * Test exposing an issue where any warmup that completes enough executions to reach |
| 29 | * Integer.MAX_VALUE reps (either because the benchmark code is optimized away or because the |
| 30 | * warmupMillis are long enough compared to the benchmark execution time). |
| 31 | */ |
| 32 | public class WarmupOverflowTest extends TestCase { |
| 33 | private TimeLimiter timeLimiter; |
| 34 | |
| 35 | @Override public void setUp() { |
| 36 | timeLimiter = new SimpleTimeLimiter(Executors.newSingleThreadExecutor()); |
| 37 | } |
| 38 | |
| 39 | public void testOptimizedAwayBenchmarkDoesNotTakeTooLongToRun() throws Exception { |
| 40 | try { |
| 41 | timeLimiter.callWithTimeout(new Callable<Void>() { |
| 42 | @Override public Void call() throws Exception { |
| 43 | InProcessRunner runner = new InProcessRunner(); |
| 44 | runner.run(OptimizedAwayBenchmark.class.getName(), "--warmupMillis", "3000", |
| 45 | "--measurementType", "TIME"); |
| 46 | return null; |
| 47 | } |
| 48 | }, 90, TimeUnit.SECONDS, false); |
| 49 | } catch (DoesNotScaleLinearlyException expected) { |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public void testLongWarmupMillisDoesNotTakeTooLongToRun() throws Exception { |
| 54 | timeLimiter.callWithTimeout(new Callable<Void>() { |
| 55 | @Override public Void call() throws Exception { |
| 56 | InProcessRunner runner = new InProcessRunner(); |
| 57 | runner.run(RelativelyFastBenchmark.class.getName(), "--warmupMillis", "8000", |
| 58 | "--runMillis", "51", "--measurementType", "TIME"); |
| 59 | return null; |
| 60 | } |
| 61 | }, 90, TimeUnit.SECONDS, false); |
| 62 | } |
| 63 | |
| 64 | public static class OptimizedAwayBenchmark extends SimpleBenchmark { |
| 65 | public void timeIsNullOrEmpty(int reps) { |
| 66 | for (int i = 0; i < reps; i++) { |
| 67 | // do nothing! |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public static class RelativelyFastBenchmark extends SimpleBenchmark { |
| 73 | public long timeSqrt(int reps) { |
| 74 | long result = 0; |
| 75 | for(int i = 0; i < reps; i++) { |
| 76 | result += Math.sqrt(81); |
| 77 | } |
| 78 | return result; |
| 79 | } |
| 80 | } |
| 81 | } |