Repository: commons-rng Updated Branches: refs/heads/master 857cbb05c -> 64439c25a
Missing sub-directory. Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/20f33fb6 Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/20f33fb6 Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/20f33fb6 Branch: refs/heads/master Commit: 20f33fb6b43d83176cef0c568ad35b851c5e239d Parents: 857cbb0 Author: Gilles <er...@apache.org> Authored: Wed Feb 14 17:30:40 2018 +0100 Committer: Gilles <er...@apache.org> Committed: Wed Feb 14 17:30:40 2018 +0100 ---------------------------------------------------------------------- .../rng/examples/jmh/GenerationPerformance.java | 194 ++++++++++++++ .../distribution/NextGaussianPerformance.java | 68 +++++ .../jmh/distribution/SamplersPerformance.java | 264 +++++++++++++++++++ .../examples/jmh/distribution/package-info.java | 22 ++ .../commons/rng/examples/jmh/package-info.java | 27 ++ .../commons/rng/jmh/GenerationPerformance.java | 194 -------------- .../distribution/NextGaussianPerformance.java | 68 ----- .../jmh/distribution/SamplersPerformance.java | 264 ------------------- .../rng/jmh/distribution/package-info.java | 22 -- .../apache/commons/rng/jmh/package-info.java | 27 -- 10 files changed, 575 insertions(+), 575 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/GenerationPerformance.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/GenerationPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/GenerationPerformance.java new file mode 100644 index 0000000..c22cf7c --- /dev/null +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/GenerationPerformance.java @@ -0,0 +1,194 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.rng.examples.jmh; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.infra.Blackhole; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.simple.RandomSource; + +/** + * Executes benchmark to compare the speed of generation of random numbers + * from the various source providers. + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@State(Scope.Benchmark) +@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"}) +public class GenerationPerformance { + /** + * The benchmark state (retrieve the various "RandomSource"s). + */ + @State(Scope.Benchmark) + public static class Sources { + /** + * RNG providers. + */ + @Param({"JDK", + "WELL_512_A", + "WELL_1024_A", + "WELL_19937_A", + "WELL_19937_C", + "WELL_44497_A", + "WELL_44497_B", + "MT", + "ISAAC", + "SPLIT_MIX_64", + "MWC_256", + "KISS", + "XOR_SHIFT_1024_S", + "TWO_CMRES", + "MT_64" }) + private String randomSourceName; + + /** RNG. */ + private UniformRandomProvider provider; + + /** + * @return the RNG. + */ + public UniformRandomProvider getGenerator() { + return provider; + } + + /** Intantiates generator. */ + @Setup + public void setup() { + final RandomSource randomSource = RandomSource.valueOf(randomSourceName); + provider = RandomSource.create(randomSource); + } + } + + /** + * Number of random values to generate. + */ + @Param({"1", "100", "10000", "1000000"}) + private int numValues; + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void nextBoolean(Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.getGenerator().nextBoolean()); + } + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void nextInt(Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.getGenerator().nextInt()); + } + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void nextIntN(Sources sources, + Blackhole bh) { + final int n = 10; + for (int i = 0; i < numValues; i++) { + bh.consume(sources.getGenerator().nextInt(n)); + } + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void nextLong(Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.getGenerator().nextLong()); + } + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void nextLongN(Sources sources, + Blackhole bh) { + final long n = 2L * Integer.MAX_VALUE; + for (int i = 0; i < numValues; i++) { + bh.consume(sources.getGenerator().nextLong(n)); + } + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void nextFloat(Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.getGenerator().nextFloat()); + } + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void nextDouble(Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.getGenerator().nextDouble()); + } + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void nextBytes(Sources sources, + Blackhole bh) { + final byte[] result = new byte[numValues]; + sources.getGenerator().nextBytes(result); + bh.consume(result); + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/NextGaussianPerformance.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/NextGaussianPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/NextGaussianPerformance.java new file mode 100644 index 0000000..2b65432 --- /dev/null +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/NextGaussianPerformance.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.rng.examples.jmh.distribution; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.infra.Blackhole; +import java.util.concurrent.TimeUnit; +import java.util.Random; + +/** + * Benchmark for {@link Random#nextGaussian()} in order to compare + * the speed of generation of normally-distributed random numbers. + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@State(Scope.Benchmark) +@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"}) +public class NextGaussianPerformance { + /** Number of samples per run. */ + private static final int NUM_SAMPLES = 10000000; + /** JDK's generator. */ + private final Random random = new Random(); + /** + * Exercises the JDK's Gaussian sampler. + * + * @param bh Data sink. + */ + private void runSample(Blackhole bh) { + for (int i = 0; i < NUM_SAMPLES; i++) { + bh.consume(random.nextGaussian()); + } + } + + // Benchmarks methods below. + + /** + * @param bh Data sink. + */ + @Benchmark + public void runJDKRandomGaussianSampler(Blackhole bh) { + runSample(bh); + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/SamplersPerformance.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/SamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/SamplersPerformance.java new file mode 100644 index 0000000..963d8bd --- /dev/null +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/SamplersPerformance.java @@ -0,0 +1,264 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.rng.examples.jmh.distribution; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.infra.Blackhole; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.simple.RandomSource; +import org.apache.commons.rng.sampling.distribution.ContinuousSampler; +import org.apache.commons.rng.sampling.distribution.DiscreteSampler; +import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler; +import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler; +import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler; +import org.apache.commons.rng.sampling.distribution.AhrensDieterExponentialSampler; +import org.apache.commons.rng.sampling.distribution.AhrensDieterMarsagliaTsangGammaSampler; +import org.apache.commons.rng.sampling.distribution.LogNormalSampler; +import org.apache.commons.rng.sampling.distribution.ChengBetaSampler; +import org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler; +import org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler; +import org.apache.commons.rng.sampling.distribution.RejectionInversionZipfSampler; +import org.apache.commons.rng.sampling.distribution.PoissonSampler; + +/** + * Executes benchmark to compare the speed of generation of random numbers + * from the various source providers. + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@State(Scope.Benchmark) +@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"}) +public class SamplersPerformance { + /** Number of samples per run. */ + private static final int NUM_SAMPLES = 10000000; + + /** + * The benchmark state (retrieve the various "RandomSource"s). + */ + @State(Scope.Benchmark) + public static class Sources { + /** + * RNG providers. + */ + @Param({"JDK", + "WELL_512_A", + "WELL_1024_A", + "WELL_19937_A", + "WELL_19937_C", + "WELL_44497_A", + "WELL_44497_B", + "MT", + "ISAAC", + "SPLIT_MIX_64", + "MWC_256", + "KISS", + "XOR_SHIFT_1024_S", + "TWO_CMRES", + "MT_64" }) + private String randomSourceName; + + /** RNG. */ + private UniformRandomProvider generator; + + /** + * @return the RNG. + */ + public UniformRandomProvider getGenerator() { + return generator; + } + + /** Intantiates generator. */ + @Setup + public void setup() { + final RandomSource randomSource = RandomSource.valueOf(randomSourceName); + generator = RandomSource.create(randomSource); + } + } + + /** + * Exercises a continuous sampler. + * + * @param sampler Sampler. + * @param bh Data sink. + */ + private void runSample(ContinuousSampler sampler, + Blackhole bh) { + for (int i = 0; i < NUM_SAMPLES; i++) { + bh.consume(sampler.sample()); + } + } + + /** + * Exercises a discrete sampler. + * + * @param sampler Sampler. + * @param bh Data sink. + */ + private void runSample(DiscreteSampler sampler, + Blackhole bh) { + for (int i = 0; i < NUM_SAMPLES; i++) { + bh.consume(sampler.sample()); + } + } + + // Benchmarks methods below. + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runBoxMullerNormalizedGaussianSampler(Sources sources, + Blackhole bh) { + runSample(new BoxMullerNormalizedGaussianSampler(sources.getGenerator()), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runMarsagliaNormalizedGaussianSampler(Sources sources, + Blackhole bh) { + runSample(new MarsagliaNormalizedGaussianSampler(sources.getGenerator()), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runZigguratNormalizedGaussianSampler(Sources sources, + Blackhole bh) { + runSample(new ZigguratNormalizedGaussianSampler(sources.getGenerator()), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runAhrensDieterExponentialSampler(Sources sources, + Blackhole bh) { + runSample(new AhrensDieterExponentialSampler(sources.getGenerator(), 4.56), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runAhrensDieterMarsagliaTsangGammaSampler(Sources sources, + Blackhole bh) { + runSample(new AhrensDieterMarsagliaTsangGammaSampler(sources.getGenerator(), 9.8, 0.76), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runBoxMullerLogNormalSampler(Sources sources, + Blackhole bh) { + runSample(new LogNormalSampler(new BoxMullerNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runMarsagliaLogNormalSampler(Sources sources, + Blackhole bh) { + runSample(new LogNormalSampler(new MarsagliaNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runZigguratLogNormalSampler(Sources sources, + Blackhole bh) { + runSample(new LogNormalSampler(new ZigguratNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runChengBetaSampler(Sources sources, + Blackhole bh) { + runSample(new ChengBetaSampler(sources.getGenerator(), 0.45, 6.7), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runContinuousUniformSampler(Sources sources, + Blackhole bh) { + runSample(new ContinuousUniformSampler(sources.getGenerator(), 123.4, 5678.9), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runDiscreteUniformSampler(Sources sources, + Blackhole bh) { + runSample(new DiscreteUniformSampler(sources.getGenerator(), -98, 76), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runRejectionInversionZipfSampler(Sources sources, + Blackhole bh) { + runSample(new RejectionInversionZipfSampler(sources.getGenerator(), 43, 2.1), bh); + } + + /** + * @param sources Source of randomness. + * @param bh Data sink. + */ + @Benchmark + public void runPoissonSampler(Sources sources, + Blackhole bh) { + runSample(new PoissonSampler(sources.getGenerator(), 8.9), bh); + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/package-info.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/package-info.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/package-info.java new file mode 100644 index 0000000..517063d --- /dev/null +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/distribution/package-info.java @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Benchmarks for samplers. + */ + +package org.apache.commons.rng.examples.jmh.distribution; http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/package-info.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/package-info.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/package-info.java new file mode 100644 index 0000000..f6a9ab7 --- /dev/null +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/package-info.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * <h3>Performance benchmarks</h3> + * + * <p> + * This package contains code to perform a + * <a href="http://openjdk.java.net/projects/code-tools/jmh">JMH</a> run. + * </p> + */ + +package org.apache.commons.rng.examples.jmh; http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/GenerationPerformance.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/GenerationPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/GenerationPerformance.java deleted file mode 100644 index c22cf7c..0000000 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/GenerationPerformance.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.rng.examples.jmh; - -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Param; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.infra.Blackhole; -import java.util.concurrent.TimeUnit; - -import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.rng.simple.RandomSource; - -/** - * Executes benchmark to compare the speed of generation of random numbers - * from the various source providers. - */ -@BenchmarkMode(Mode.AverageTime) -@OutputTimeUnit(TimeUnit.MICROSECONDS) -@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) -@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) -@State(Scope.Benchmark) -@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"}) -public class GenerationPerformance { - /** - * The benchmark state (retrieve the various "RandomSource"s). - */ - @State(Scope.Benchmark) - public static class Sources { - /** - * RNG providers. - */ - @Param({"JDK", - "WELL_512_A", - "WELL_1024_A", - "WELL_19937_A", - "WELL_19937_C", - "WELL_44497_A", - "WELL_44497_B", - "MT", - "ISAAC", - "SPLIT_MIX_64", - "MWC_256", - "KISS", - "XOR_SHIFT_1024_S", - "TWO_CMRES", - "MT_64" }) - private String randomSourceName; - - /** RNG. */ - private UniformRandomProvider provider; - - /** - * @return the RNG. - */ - public UniformRandomProvider getGenerator() { - return provider; - } - - /** Intantiates generator. */ - @Setup - public void setup() { - final RandomSource randomSource = RandomSource.valueOf(randomSourceName); - provider = RandomSource.create(randomSource); - } - } - - /** - * Number of random values to generate. - */ - @Param({"1", "100", "10000", "1000000"}) - private int numValues; - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void nextBoolean(Sources sources, - Blackhole bh) { - for (int i = 0; i < numValues; i++) { - bh.consume(sources.getGenerator().nextBoolean()); - } - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void nextInt(Sources sources, - Blackhole bh) { - for (int i = 0; i < numValues; i++) { - bh.consume(sources.getGenerator().nextInt()); - } - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void nextIntN(Sources sources, - Blackhole bh) { - final int n = 10; - for (int i = 0; i < numValues; i++) { - bh.consume(sources.getGenerator().nextInt(n)); - } - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void nextLong(Sources sources, - Blackhole bh) { - for (int i = 0; i < numValues; i++) { - bh.consume(sources.getGenerator().nextLong()); - } - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void nextLongN(Sources sources, - Blackhole bh) { - final long n = 2L * Integer.MAX_VALUE; - for (int i = 0; i < numValues; i++) { - bh.consume(sources.getGenerator().nextLong(n)); - } - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void nextFloat(Sources sources, - Blackhole bh) { - for (int i = 0; i < numValues; i++) { - bh.consume(sources.getGenerator().nextFloat()); - } - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void nextDouble(Sources sources, - Blackhole bh) { - for (int i = 0; i < numValues; i++) { - bh.consume(sources.getGenerator().nextDouble()); - } - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void nextBytes(Sources sources, - Blackhole bh) { - final byte[] result = new byte[numValues]; - sources.getGenerator().nextBytes(result); - bh.consume(result); - } -} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/NextGaussianPerformance.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/NextGaussianPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/NextGaussianPerformance.java deleted file mode 100644 index 2b65432..0000000 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/NextGaussianPerformance.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.rng.examples.jmh.distribution; - -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.infra.Blackhole; -import java.util.concurrent.TimeUnit; -import java.util.Random; - -/** - * Benchmark for {@link Random#nextGaussian()} in order to compare - * the speed of generation of normally-distributed random numbers. - */ -@BenchmarkMode(Mode.AverageTime) -@OutputTimeUnit(TimeUnit.MICROSECONDS) -@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) -@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) -@State(Scope.Benchmark) -@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"}) -public class NextGaussianPerformance { - /** Number of samples per run. */ - private static final int NUM_SAMPLES = 10000000; - /** JDK's generator. */ - private final Random random = new Random(); - /** - * Exercises the JDK's Gaussian sampler. - * - * @param bh Data sink. - */ - private void runSample(Blackhole bh) { - for (int i = 0; i < NUM_SAMPLES; i++) { - bh.consume(random.nextGaussian()); - } - } - - // Benchmarks methods below. - - /** - * @param bh Data sink. - */ - @Benchmark - public void runJDKRandomGaussianSampler(Blackhole bh) { - runSample(bh); - } -} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java deleted file mode 100644 index 963d8bd..0000000 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.rng.examples.jmh.distribution; - -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Param; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.infra.Blackhole; -import java.util.concurrent.TimeUnit; - -import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.rng.simple.RandomSource; -import org.apache.commons.rng.sampling.distribution.ContinuousSampler; -import org.apache.commons.rng.sampling.distribution.DiscreteSampler; -import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler; -import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler; -import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler; -import org.apache.commons.rng.sampling.distribution.AhrensDieterExponentialSampler; -import org.apache.commons.rng.sampling.distribution.AhrensDieterMarsagliaTsangGammaSampler; -import org.apache.commons.rng.sampling.distribution.LogNormalSampler; -import org.apache.commons.rng.sampling.distribution.ChengBetaSampler; -import org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler; -import org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler; -import org.apache.commons.rng.sampling.distribution.RejectionInversionZipfSampler; -import org.apache.commons.rng.sampling.distribution.PoissonSampler; - -/** - * Executes benchmark to compare the speed of generation of random numbers - * from the various source providers. - */ -@BenchmarkMode(Mode.AverageTime) -@OutputTimeUnit(TimeUnit.MICROSECONDS) -@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) -@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) -@State(Scope.Benchmark) -@Fork(value = 1, jvmArgs = {"-server", "-Xms128M", "-Xmx128M"}) -public class SamplersPerformance { - /** Number of samples per run. */ - private static final int NUM_SAMPLES = 10000000; - - /** - * The benchmark state (retrieve the various "RandomSource"s). - */ - @State(Scope.Benchmark) - public static class Sources { - /** - * RNG providers. - */ - @Param({"JDK", - "WELL_512_A", - "WELL_1024_A", - "WELL_19937_A", - "WELL_19937_C", - "WELL_44497_A", - "WELL_44497_B", - "MT", - "ISAAC", - "SPLIT_MIX_64", - "MWC_256", - "KISS", - "XOR_SHIFT_1024_S", - "TWO_CMRES", - "MT_64" }) - private String randomSourceName; - - /** RNG. */ - private UniformRandomProvider generator; - - /** - * @return the RNG. - */ - public UniformRandomProvider getGenerator() { - return generator; - } - - /** Intantiates generator. */ - @Setup - public void setup() { - final RandomSource randomSource = RandomSource.valueOf(randomSourceName); - generator = RandomSource.create(randomSource); - } - } - - /** - * Exercises a continuous sampler. - * - * @param sampler Sampler. - * @param bh Data sink. - */ - private void runSample(ContinuousSampler sampler, - Blackhole bh) { - for (int i = 0; i < NUM_SAMPLES; i++) { - bh.consume(sampler.sample()); - } - } - - /** - * Exercises a discrete sampler. - * - * @param sampler Sampler. - * @param bh Data sink. - */ - private void runSample(DiscreteSampler sampler, - Blackhole bh) { - for (int i = 0; i < NUM_SAMPLES; i++) { - bh.consume(sampler.sample()); - } - } - - // Benchmarks methods below. - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runBoxMullerNormalizedGaussianSampler(Sources sources, - Blackhole bh) { - runSample(new BoxMullerNormalizedGaussianSampler(sources.getGenerator()), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runMarsagliaNormalizedGaussianSampler(Sources sources, - Blackhole bh) { - runSample(new MarsagliaNormalizedGaussianSampler(sources.getGenerator()), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runZigguratNormalizedGaussianSampler(Sources sources, - Blackhole bh) { - runSample(new ZigguratNormalizedGaussianSampler(sources.getGenerator()), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runAhrensDieterExponentialSampler(Sources sources, - Blackhole bh) { - runSample(new AhrensDieterExponentialSampler(sources.getGenerator(), 4.56), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runAhrensDieterMarsagliaTsangGammaSampler(Sources sources, - Blackhole bh) { - runSample(new AhrensDieterMarsagliaTsangGammaSampler(sources.getGenerator(), 9.8, 0.76), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runBoxMullerLogNormalSampler(Sources sources, - Blackhole bh) { - runSample(new LogNormalSampler(new BoxMullerNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runMarsagliaLogNormalSampler(Sources sources, - Blackhole bh) { - runSample(new LogNormalSampler(new MarsagliaNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runZigguratLogNormalSampler(Sources sources, - Blackhole bh) { - runSample(new LogNormalSampler(new ZigguratNormalizedGaussianSampler(sources.getGenerator()), 12.3, 4.6), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runChengBetaSampler(Sources sources, - Blackhole bh) { - runSample(new ChengBetaSampler(sources.getGenerator(), 0.45, 6.7), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runContinuousUniformSampler(Sources sources, - Blackhole bh) { - runSample(new ContinuousUniformSampler(sources.getGenerator(), 123.4, 5678.9), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runDiscreteUniformSampler(Sources sources, - Blackhole bh) { - runSample(new DiscreteUniformSampler(sources.getGenerator(), -98, 76), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runRejectionInversionZipfSampler(Sources sources, - Blackhole bh) { - runSample(new RejectionInversionZipfSampler(sources.getGenerator(), 43, 2.1), bh); - } - - /** - * @param sources Source of randomness. - * @param bh Data sink. - */ - @Benchmark - public void runPoissonSampler(Sources sources, - Blackhole bh) { - runSample(new PoissonSampler(sources.getGenerator(), 8.9), bh); - } -} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/package-info.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/package-info.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/package-info.java deleted file mode 100644 index 517063d..0000000 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Benchmarks for samplers. - */ - -package org.apache.commons.rng.examples.jmh.distribution; http://git-wip-us.apache.org/repos/asf/commons-rng/blob/20f33fb6/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/package-info.java ---------------------------------------------------------------------- diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/package-info.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/package-info.java deleted file mode 100644 index f6a9ab7..0000000 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/jmh/package-info.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * <h3>Performance benchmarks</h3> - * - * <p> - * This package contains code to perform a - * <a href="http://openjdk.java.net/projects/code-tools/jmh">JMH</a> run. - * </p> - */ - -package org.apache.commons.rng.examples.jmh;