commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Artem Barger (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (RNG-2) Allow creation and execution of JMH based benchmarks
Date Tue, 09 Aug 2016 12:31:20 GMT

    [ https://issues.apache.org/jira/browse/RNG-2?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15413446#comment-15413446
] 

Artem Barger commented on RNG-2:
--------------------------------

For example I've create a simple benchmark, to estimate value of PI

{code:java}
/*
 * Copyright (c) 2014, Oracle America, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 *  * Neither the name of Oracle nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.apache.commons.rng.internal.benchmark;

import org.apache.commons.rng.RandomSource;
import org.apache.commons.rng.UniformRandomProvider;
import org.openjdk.jmh.annotations.*;

import java.util.concurrent.TimeUnit;

/**
 * Benchmark which compare performance of different implementations of the
 * {@link org.apache.commons.rng.internal.source32.IntProvider} by running a simple workload
of PI computation.
 *
 * The computation estimates value of PI by computing the probability of point p=(x, y) to
lay down in the circle of
 * radius one inscribed in the square. The probability could be computed by area_of_circle/area_of_square,
where it's
 * easy to see that area_of_circle = PI * R^2 and area_of_square=4*R^2, therefore the probability
is PI/4.
 *
 * Now, the computation will run Monte Carlo simulation to produce 10^6 pairs of (x, y) and
count the portion of pairs
 * which satisfies inequality x^2 + y^2 <=1 to overall number of points (10^6). Obviously
the portion value is the
 * approximation of the probability of point to lay down inside the circle and could be used
to approximate PI, since
 * PI ~= 4 * portion, where portion = (points inside the circle)/(overall points).
 *
 */
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
public class PiComputationBenchmark {

    @State(Scope.Benchmark)
    /**
     * The benchmark state to initialize the random source provide with different algorithm.
     */
    public static class RandomIntBasedSources {

        /**
         * A list of int random source providers.
         */
        @Param({"JDK", "WELL_512_A", "WELL_1024_A", "WELL_19937_A", "WELL_19937_C",
                "WELL_44497_A", "WELL_44497_B", "MT", "ISAAC"})
        String randomSourceName;

        /**
         * Number of pairs to generate during Monte-Carlo simulation.
         */
        @Param({"1000000"})
        long pairsToGenerate;

        UniformRandomProvider provider;

        @Setup
        public void setup() {
            final RandomSource randomSource = RandomSource.valueOf(randomSourceName);
            provider = RandomSource.create(randomSource);
        }
    }

    @Benchmark
    public double computePi(RandomIntBasedSources data) {
        long pairsInCircle = 0;
        for (int i = 0; i < data.pairsToGenerate; i++) {
            double x = data.provider.nextDouble();
            double y = data.provider.nextDouble();
            if ((x*x + y*y) <= 1) {
                pairsInCircle ++;
            }
        }

        return (4d*pairsInCircle)/data.pairsToGenerate;
    }

}
{code}

and was able to get following results out of it:
{noformat}
# Run complete. Total time: 00:01:35

Benchmark                         (pairsToGenerate)  (randomSourceName)  Mode  Cnt      Score
      Error  Units
PiComputationBenchmark.computePi            1000000                 JDK  avgt    5  44076.310
±  2794.888  us/op
PiComputationBenchmark.computePi            1000000          WELL_512_A  avgt    5  19374.112
±  4973.542  us/op
PiComputationBenchmark.computePi            1000000         WELL_1024_A  avgt    5  20575.240
±  4298.444  us/op
PiComputationBenchmark.computePi            1000000        WELL_19937_A  avgt    5  42208.136
±  2062.648  us/op
PiComputationBenchmark.computePi            1000000        WELL_19937_C  avgt    5  45105.231
±  2752.918  us/op
PiComputationBenchmark.computePi            1000000        WELL_44497_A  avgt    5  49710.663
± 15786.830  us/op
PiComputationBenchmark.computePi            1000000        WELL_44497_B  avgt    5  48984.700
±  2449.719  us/op
PiComputationBenchmark.computePi            1000000                  MT  avgt    5  22466.565
±  1377.585  us/op
PiComputationBenchmark.computePi            1000000               ISAAC  avgt    5  21615.283
±  1080.331  us/op
{noformat}

> Allow creation and execution of JMH based benchmarks
> ----------------------------------------------------
>
>                 Key: RNG-2
>                 URL: https://issues.apache.org/jira/browse/RNG-2
>             Project: Commons RNG
>          Issue Type: Bug
>            Reporter: Artem Barger
>         Attachments: RNG-2.patch
>
>
> Add relevant maven changes to enable creation and execution of benchmarks based on JMH
(Java Microbenchmarks harness) framework.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message