JMH benchmarks. Thanks to Artem Barger.
Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/89a2df07 Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/89a2df07 Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/89a2df07 Branch: refs/heads/master Commit: 89a2df072ca4538dea50ede643a1d1013232044f Parents: 8fac22b Author: Gilles <[email protected]> Authored: Wed Aug 10 20:36:35 2016 +0200 Committer: Gilles <[email protected]> Committed: Wed Aug 10 20:36:35 2016 +0200 ---------------------------------------------------------------------- .../commons/rng/AbstractTestPerformance.java | 58 ++++++++++ .../commons/rng/ComputePiTestPerformance.java | 51 ++------ .../commons/rng/GenerationTestPerformance.java | 115 +++++++++++++++++++ 3 files changed, 183 insertions(+), 41 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-rng/blob/89a2df07/src/test/java/org/apache/commons/rng/AbstractTestPerformance.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/rng/AbstractTestPerformance.java b/src/test/java/org/apache/commons/rng/AbstractTestPerformance.java new file mode 100644 index 0000000..363a784 --- /dev/null +++ b/src/test/java/org/apache/commons/rng/AbstractTestPerformance.java @@ -0,0 +1,58 @@ +/* + * 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; + +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; + +public class AbstractTestPerformance { + /** + * 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", + "XOR_SHIFT_1024_S", + "TWO_CMRES", + "MT_64" }) + String randomSourceName; + + UniformRandomProvider provider; + + @Setup + public void setup() { + final RandomSource randomSource = RandomSource.valueOf(randomSourceName); + provider = RandomSource.create(randomSource); + } + } + +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/89a2df07/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java b/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java index ce5e47e..351fc7d 100644 --- a/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java +++ b/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java @@ -24,8 +24,8 @@ import java.util.concurrent.TimeUnit; * by running a simple workload: computation of \( \pi \). * * The computation estimates the value by computing the probability that - * a point p=(x, y) will lie in the circle of radius \( r = 1 \) inscribed - * in the square. + * a point \( p = (x, y) \) will lie in the circle of radius \( r = 1 \) + * inscribed in the square of side \( r = 1 \). * The probability could be computed by \[ area_{circle} / area_{square} \], * where \( area_{circle} = \pi * r^2 \) and \( area_{square} = 4 r^2 \). * Hence, the probability is \( \frac{\pi}{4} \). @@ -39,49 +39,18 @@ import java.util.concurrent.TimeUnit; @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @Fork(1) -public class ComputePiTestPerformance { +@State(Scope.Benchmark) +public class ComputePiTestPerformance extends AbstractTestPerformance { /** - * The benchmark state (retrieve the various "RandomSource"s). + * Number of 2D-points to generate. */ - @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", - "XOR_SHIFT_1024_S", - "TWO_CMRES", - "MT_64" }) - String randomSourceName; - - /** - * Number of 2D-points to generate. - */ - @Param({"1000000"}) - long numPoints; - - UniformRandomProvider provider; - - @Setup - public void setup() { - final RandomSource randomSource = RandomSource.valueOf(randomSourceName); - provider = RandomSource.create(randomSource); - } - } + @Param({"5000000"}) + long numPoints; @Benchmark public double computePi(Sources data) { long numPointsInCircle = 0; - for (int i = 0; i < data.numPoints; i++) { + for (int i = 0; i < numPoints; i++) { final double x = data.provider.nextDouble(); final double y = data.provider.nextDouble(); final double r2 = x * x + y * y; @@ -90,8 +59,8 @@ public class ComputePiTestPerformance { } } - final double pi = 4 * numPointsInCircle / (double) data.numPoints; - // System.out.println("pi=" + pi); + final double pi = 4 * numPointsInCircle / (double) numPoints; + System.out.println("pi=" + pi); return pi; } } http://git-wip-us.apache.org/repos/asf/commons-rng/blob/89a2df07/src/test/java/org/apache/commons/rng/GenerationTestPerformance.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/rng/GenerationTestPerformance.java b/src/test/java/org/apache/commons/rng/GenerationTestPerformance.java new file mode 100644 index 0000000..21353c6 --- /dev/null +++ b/src/test/java/org/apache/commons/rng/GenerationTestPerformance.java @@ -0,0 +1,115 @@ +/* + * 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; + +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.OutputTimeUnit; +import org.openjdk.jmh.infra.Blackhole; +import java.util.concurrent.TimeUnit; + +/** + * 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(1) +public class GenerationTestPerformance extends AbstractTestPerformance { + /** + * Number of random values to generate. + */ + @Param({"1", "100", "10000", "1000000"}) + int numValues; + + @Benchmark + public void nextBoolean(AbstractTestPerformance.Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.provider.nextBoolean()); + } + } + + @Benchmark + public void nextInt(AbstractTestPerformance.Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.provider.nextInt()); + } + } + + @Benchmark + public void nextIntN(AbstractTestPerformance.Sources sources, + Blackhole bh) { + final int n = 10; + for (int i = 0; i < numValues; i++) { + bh.consume(sources.provider.nextInt(n)); + } + } + + @Benchmark + public void nextLong(AbstractTestPerformance.Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.provider.nextLong()); + } + } + + @Benchmark + public void nextLongN(AbstractTestPerformance.Sources sources, + Blackhole bh) { + final long n = 2L * Integer.MAX_VALUE; + for (int i = 0; i < numValues; i++) { + bh.consume(sources.provider.nextLong(n)); + } + } + + @Benchmark + public void nextFloat(AbstractTestPerformance.Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.provider.nextFloat()); + } + } + + @Benchmark + public void nextDouble(AbstractTestPerformance.Sources sources, + Blackhole bh) { + for (int i = 0; i < numValues; i++) { + bh.consume(sources.provider.nextDouble()); + } + } + + @Benchmark + public void nextBytes(AbstractTestPerformance.Sources sources, + Blackhole bh) { + final byte[] result = new byte[numValues]; + sources.provider.nextBytes(result); + bh.consume(result); + } +}
