blob: 85e59af3cee885dd634770b07656cae73789852f [file] [log] [blame]
Paul Duffin7fc0b452015-11-10 17:45:15 +00001/*
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
17package com.google.caliper;
18
19import com.google.caliper.UserException.DoesNotScaleLinearlyException;
20import com.google.common.util.concurrent.SimpleTimeLimiter;
21import com.google.common.util.concurrent.TimeLimiter;
22import java.util.concurrent.Callable;
23import java.util.concurrent.Executors;
24import java.util.concurrent.TimeUnit;
25import 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 */
32public 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}