xref: /aosp_15_r20/external/caliper/caliper/src/test/java/com/google/caliper/worker/RuntimeWorkerTest.java (revision e13194474f9b0035ae014b3193027641ae2d48fb)
1 /*
2  * Copyright (C) 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.caliper.worker;
16 
17 import static com.google.caliper.worker.RuntimeWorker.INITIAL_REPS;
18 import static com.google.caliper.worker.RuntimeWorker.calculateTargetReps;
19 import static java.util.concurrent.TimeUnit.HOURS;
20 import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 import static java.util.concurrent.TimeUnit.NANOSECONDS;
22 import static java.util.concurrent.TimeUnit.SECONDS;
23 import static org.junit.Assert.assertEquals;
24 
25 import com.google.caliper.util.ShortDuration;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 import java.math.BigDecimal;
32 
33 /**
34  * Tests {@link RuntimeWorker}.
35  */
36 @RunWith(JUnit4.class)
37 public class RuntimeWorkerTest {
38   private static final ShortDuration TIMING_INTERVAL = ShortDuration.of(100, MILLISECONDS);
39 
testCalculateTargetReps_tinyBenchmark()40   @Test public void testCalculateTargetReps_tinyBenchmark() {
41     // this is one cycle on a 5GHz machine
42     ShortDuration oneCycle = ShortDuration.of(new BigDecimal("2.0e-10"), SECONDS);
43     long targetReps = calculateTargetReps(INITIAL_REPS,
44         oneCycle.times(INITIAL_REPS).to(NANOSECONDS), TIMING_INTERVAL.to(NANOSECONDS), 0.0);
45     long expectedReps = TIMING_INTERVAL.toPicos() / oneCycle.toPicos();
46     assertEquals(expectedReps, targetReps);
47   }
48 
testCalculateTargetReps_hugeBenchmark()49   @Test public void testCalculateTargetReps_hugeBenchmark() {
50     long targetReps =
51         calculateTargetReps(INITIAL_REPS, HOURS.toNanos(1), TIMING_INTERVAL.to(NANOSECONDS), 0.0);
52     assertEquals(1, targetReps);
53   }
54 
testCalculateTargetReps_applyRandomness()55   @Test public void testCalculateTargetReps_applyRandomness() {
56     long targetReps = calculateTargetReps(INITIAL_REPS, MILLISECONDS.toNanos(100),
57         TIMING_INTERVAL.to(NANOSECONDS), 0.5);
58     assertEquals(110, targetReps);
59   }
60 }
61