View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.rng.examples.jmh.core;
19  
20  import org.apache.commons.rng.UniformRandomProvider;
21  import org.openjdk.jmh.annotations.Benchmark;
22  import org.openjdk.jmh.annotations.Scope;
23  import org.openjdk.jmh.annotations.State;
24  
25  /**
26   * Executes benchmark to compare the speed of generation of random numbers from the
27   * various source providers for {@link UniformRandomProvider#nextInt()} and
28   * {@link UniformRandomProvider#nextInt(int)}.
29   */
30  public class NextIntGenerationPerformance extends AbstractBenchmark {
31      /** The value. Must NOT be final to prevent JVM optimisation! */
32      private int value;
33  
34      /**
35       * The benchmark state (retrieve the various "RandomSource"s).
36       */
37      @State(Scope.Benchmark)
38      public static class Sources extends BaselineSources {
39          /** {@inheritDoc} */
40          @Override
41          protected UniformRandomProvider createBaseline() {
42              return BaselineUtils.getNextInt();
43          }
44      }
45  
46      /**
47       * Baseline for a JMH method call with no return value.
48       */
49      @Benchmark
50      public void baselineVoid() {
51          // Do nothing, this is a baseline
52      }
53  
54      /**
55       * Baseline for a JMH method call returning an {@code int}.
56       *
57       * @return the value
58       */
59      @Benchmark
60      public int baselineInt() {
61          return value;
62      }
63  
64      /**
65       * Exercise the {@link UniformRandomProvider#nextInt()} method.
66       *
67       * @param sources Source of randomness.
68       * @return the int
69       */
70      @Benchmark
71      public int nextInt(Sources sources) {
72          return sources.getGenerator().nextInt();
73      }
74  
75      /**
76       * Exercise the {@link UniformRandomProvider#nextInt(int)} method.
77       *
78       * @param sources Source of randomness.
79       * @return the int
80       */
81      @Benchmark
82      public int nextIntN(Sources sources) {
83          return sources.getGenerator().nextInt(BaselineGenerationPerformance.NEXT_INT_LIMIT);
84      }
85  }