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#nextBoolean()}. 28 */ 29 public class NextBooleanGenerationPerformance extends AbstractBenchmark { 30 /** The value. Must NOT be final to prevent JVM optimisation! */ 31 private boolean value; 32 33 /** 34 * The benchmark state (retrieve the various "RandomSource"s). 35 */ 36 @State(Scope.Benchmark) 37 public static class Sources extends BaselineSources { 38 /** {@inheritDoc} */ 39 @Override 40 protected UniformRandomProvider createBaseline() { 41 return BaselineUtils.getNextBoolean(); 42 } 43 } 44 45 /** 46 * Baseline for a JMH method call with no return value. 47 */ 48 @Benchmark 49 public void baselineVoid() { 50 // Do nothing, this is a baseline 51 } 52 53 /** 54 * Baseline for a JMH method call returning a {@code boolean}. 55 * 56 * @return the value 57 */ 58 @Benchmark 59 public boolean baselineBoolean() { 60 return value; 61 } 62 63 /** 64 * Exercise the {@link UniformRandomProvider#nextBoolean()} method. 65 * 66 * @param sources Source of randomness. 67 * @return the boolean 68 */ 69 @Benchmark 70 public boolean nextBoolean(Sources sources) { 71 return sources.getGenerator().nextBoolean(); 72 } 73 }