The method of generating a plurality of random numbers based on the efficiency of inspection JMH

maven dependence

 <!-- jmh -->
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>1.21</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>1.21</version>
            <scope>provided</scope>
        </dependency>
        <!-- netty -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.42.Final</version>
        </dependency>
        
        
        <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>2.0</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <finalName>microbenchmarks</finalName>
                                    <transformers>
                                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <mainClass>org.openjdk.jmh.Main</mainClass>
                                        </transformer>
                                    </transformers>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

Basic Concepts
Mode
Mode indicates the mode when performed Benchmark JMH used. Measurement usually different dimensions, or different measurement ways. JMH There are currently four modes:
(1) .Throughput: overall throughput, for example, "How many times in one second can call execution."
(2) .AverageTime: the average call time, for example, "xxx ms average time each call."
(3) .SampleTime: randomly sampled, the final output sampling distribution of results, such as "99 percent of calls within xxx ms, 99.99% of calls within milliseconds xxx"
(. 4) .SingleShotTime: one or more iteration are the default mode is 1s, only SingleShotTime is run only once. Often at the same time the number is zero warmup for testing performance when cold start.

The Iteration
the Iteration is the smallest unit JMH testing. In most modes, Iteration represents one second, will continue to call the method requires JMH benchmark in the seconds, then subjected to sampling mode according to the calculated throughput, average execution time calculation.
Warmup
Warmup refers to the act to be preheated before the actual Benchmark. Because there JVM JIT mechanisms of the future, if a function is called multiple times, JVM will try to compile it to become machine code to improve execution speed. So in order to make the results of benchmark closer to the real situation on the need for preheating.

@Benchmark
indicates that the target method is the need for a benchmark, and similar usage of JUnit @Test.
@Mode
Mode As previously mentioned, represents a mode for Benchmark JMH used.
@State
State is used to declare a class is a "state", and then accepts the Scope parameter is used to indicate a range of the shared state. Because many will need some benchmark indicates the status of the class, JMH allows you to put these classes to dependency injection is injected into the benchmark function in the way. Scope divided into two types.
(1) .Thread: This exclusive status for each thread.
(2) .Benchmark: The state shared among all threads.
For the use of the State, the official code sample, there are good examples.
@OutputTimeUnit
time unit used benchmark results.

include
the name of the class where the benchmark note here is the use of regular expressions to match for all classes.
fork
number fork conducted. If the number of fork is 2, then JMH will fork out the two processes to be tested.
warmupIterations
number of iterations preheated.
measurementIterations
iterations actual measurements.

package com.xiaobu;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2020/1/7 10:44
 * @description
 */
@Slf4j
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Threads(50)
@Fork(1)
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 3, time = 5)

public class RandomBenchmark{

    Random random = new Random();
    ThreadLocal<Random> randomThreadLocalHolder = ThreadLocal.withInitial(Random::new);

    @Benchmark
    public int random(){
        return random.nextInt();
    }


    @Benchmark
    public int threadLocalRandom(){
        return ThreadLocalRandom.current().nextInt();
    }

    @Benchmark
    public int randomThreadLocalHolder(){
        return randomThreadLocalHolder.get().nextInt();
    }

    @Benchmark
    public int ntteyRandomThreadLocal(){
        return io.netty.util.internal.ThreadLocalRandom.current().nextInt();
    }

    @SneakyThrows
    public static void main(String[] args) {
        Options opt = new OptionsBuilder().include(RandomBenchmark.class.getSimpleName()).build();
        new Runner(opt).run();
    }

}

1578381146(1).jpg

In concurrent conditions, ThreadLocalRandom highest performance.

Published 152 original articles · won praise 18 · views 70000 +

Guess you like

Origin blog.csdn.net/tanhongwei1994/article/details/103876035