자바 JMH 기준 튜 토리 얼

15302 단어
Benchmark                        (N)  Mode  Cnt   Score   Error  Units
BenchmarkLoop.loopFor       10000000  avgt   10  61.673 ± 1.251  ms/op
BenchmarkLoop.loopForEach   10000000  avgt   10  67.582 ± 1.034  ms/op
BenchmarkLoop.loopIterator  10000000  avgt   10  66.087 ± 1.534  ms/op
BenchmarkLoop.loopWhile     10000000  avgt   10  60.660 ± 0.279  ms/op

자바 에서 우 리 는 JMH (Java Microbenchmark Harness) 프레임 워 크 를 사용 하여 기능 의 성능 을 평가 할 수 있다.
테스트 를 거치다
  • 승 운 1.21
  • Java 10
  • Maven 의 3.6
  • CPU i7-7700

  • 본 튜 토리 얼 에 서 는 JMH 를 사용 하여 서로 다른 순환 방법 을 측정 하 는 방법 for, while, iterator and foreach 을 보 여 드 리 겠 습 니 다.
    1. JMH
    JHM 을 사용 하려 면 성명 jmh-corejmh-generator-annprocess (JMH 주석) 이 필요 합 니 다.
    pom.xml
    
            1.21
        
    
    	
            
                org.openjdk.jmh
                jmh-core
                ${jmh.version}
            
            
                org.openjdk.jmh
                jmh-generator-annprocess
                ${jmh.version}
            
        

    2. JMH – Mode.AverageTime
    2.1 JMH Mode.AverageTime 예 시 는 1 천만 개의 String List 을 순환 적 으로 포함 하 는 순환 방법의 성능 을 측정 하 는 데 사용 된다.
    BenchmarkLoop.java
    package com.mkyong.benchmark;
    
    import org.openjdk.jmh.annotations.*;
    import org.openjdk.jmh.infra.Blackhole;
    import org.openjdk.jmh.runner.Runner;
    import org.openjdk.jmh.runner.RunnerException;
    import org.openjdk.jmh.runner.options.Options;
    import org.openjdk.jmh.runner.options.OptionsBuilder;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    @State(Scope.Benchmark)
    @Fork(value = 2, jvmArgs = {"-Xms2G", "-Xmx2G"})
    //@Warmup(iterations = 3)
    //@Measurement(iterations = 8)
    public class BenchmarkLoop {
    
        @Param({"10000000"})
        private int N;
    
        private List DATA_FOR_TESTING;
    
        public static void main(String[] args) throws RunnerException {
    
            Options opt = new OptionsBuilder()
                    .include(BenchmarkLoop.class.getSimpleName())
                    .forks(1)
                    .build();
    
            new Runner(opt).run();
        }
    
        @Setup
        public void setup() {
            DATA_FOR_TESTING = createData();
        }
    
        @Benchmark
        public void loopFor(Blackhole bh) {
            for (int i = 0; i < DATA_FOR_TESTING.size(); i++) {
                String s = DATA_FOR_TESTING.get(i); //take out n consume, fair with foreach
                bh.consume(s);
            }
        }
    
        @Benchmark
        public void loopWhile(Blackhole bh) {
            int i = 0;
            while (i < DATA_FOR_TESTING.size()) {
                String s = DATA_FOR_TESTING.get(i);
                bh.consume(s);
                i++;
            }
        }
    
        @Benchmark
        public void loopForEach(Blackhole bh) {
            for (String s : DATA_FOR_TESTING) {
                bh.consume(s);
            }
        }
    
        @Benchmark
        public void loopIterator(Blackhole bh) {
            Iterator iterator = DATA_FOR_TESTING.iterator();
            while (iterator.hasNext()) {
                String s = iterator.next();
                bh.consume(s);
            }
        }
    
        private List createData() {
            List data = new ArrayList<>();
            for (int i = 0; i < N; i++) {
                data.add("Number : " + i);
            }
            return data;
        }
    
    }

    2.2 위의 코드 에서 JMH 는 2 개의 파생 을 만 들 것 이다. 각 파생 은 5 개의 예열 교체 (JVM 예열, 결과 무시) 와 5 개의 측정 교체 (계산 에 사용) 를 포함한다. 예 를 들 어:
    # Run progress: 0.00% complete, ETA 00:13:20
    # Fork: 1 of 2
    # Warmup Iteration   1: 60.920 ms/op
    # Warmup Iteration   2: 60.745 ms/op
    # Warmup Iteration   3: 60.818 ms/op
    # Warmup Iteration   4: 60.659 ms/op
    # Warmup Iteration   5: 60.765 ms/op
    Iteration   1: 63.579 ms/op
    Iteration   2: 61.622 ms/op
    Iteration   3: 61.869 ms/op
    Iteration   4: 61.730 ms/op
    Iteration   5: 62.207 ms/op
    
    # Run progress: 12.50% complete, ETA 00:11:50
    # Fork: 2 of 2
    # Warmup Iteration   1: 60.915 ms/op
    # Warmup Iteration   2: 61.527 ms/op
    # Warmup Iteration   3: 62.329 ms/op
    # Warmup Iteration   4: 62.729 ms/op
    # Warmup Iteration   5: 61.693 ms/op
    Iteration   1: 60.822 ms/op
    Iteration   2: 61.220 ms/op
    Iteration   3: 61.216 ms/op
    Iteration   4: 60.652 ms/op
    Iteration   5: 61.818 ms/op
    
    Result "com.mkyong.benchmark.BenchmarkLoop.loopFor":
      61.673 ±(99.9%) 1.251 ms/op [Average]
      (min, avg, max) = (60.652, 61.673, 63.579), stdev = 0.828
      CI (99.9%): [60.422, 62.925] (assumes normal distribution)

    2.3 예열 교체 와 측정 교 체 는 설정 할 수 있 습 니 다.
    @Warmup(iterations = 3) 		// Warmup Iteration = 3
    @Measurement(iterations = 8) 	// Iteration = 8

    2.4 우 리 는 심지어 진정한 앞 포크 를 작 동 하기 전에 전체 앞 포크 를 가열 할 수 있다.
    @Fork(value = 2, jvmArgs = {"-Xms2G", "-Xmx2G"}, warmups = 2)

    3. JMH – # 1 Maven 을 어떻게 실행 합 니까?
    JMH 기준 테스트 를 실행 할 수 있 는 두 가지 방법 이 있 는데 Maven 을 사용 하거나 JMH Runner 류 를 통 해 직접 실행 할 수 있다.
    3.1 Maven, 이 를 JAR 로 포장 하고 org.openjdk.jmh.Main 클래스 를 통 해 운행 한다.
    pom.xml
    
    	
    		
    			org.apache.maven.plugins
    			maven-shade-plugin
    			3.2.0
    			
    				
    					package
    					
    						shade
    					
    					
    						benchmarks
    						
    							
    								org.openjdk.jmh.Main
    							
    						
    					
    				
    			
    		
    	
    

    3.2 mvn package, JAR 을 정상적으로 시작 하면 하나 benchmarks.jar 를 생 성 합 니 다.
    Terminal
    $ mvn package 
    
    $ java -jar target\benchmarks.jar BenchmarkLoop

    4. JMH – # 2 JMH 러 너 를 어떻게 실행 합 니까?
    JMH Runner 클래스 실행 기준 테스트 를 직접 통과 할 수 있 습 니 다.
    BenchmarkLoop.java
    package com.mkyong.benchmark;
    
    import org.openjdk.jmh.annotations.*;
    import org.openjdk.jmh.runner.Runner;
    import org.openjdk.jmh.runner.RunnerException;
    import org.openjdk.jmh.runner.options.Options;
    import org.openjdk.jmh.runner.options.OptionsBuilder;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    @Fork(value = 2, jvmArgs = {"-Xms2G", "-Xmx2G"})
    public class BenchmarkLoop {
    
        private static final int N = 10_000_000;
    
        private static List DATA_FOR_TESTING = createData();
    
        public static void main(String[] args) throws RunnerException {
    
            Options opt = new OptionsBuilder()
                    .include(BenchmarkLoop.class.getSimpleName())
                    .forks(1)
                    .build();
    
            new Runner(opt).run();
        }
    
        // Benchmark code
    
    }

    5. 결과
    5.1 결 과 를 살 펴 보면 1000 만 개의 String 대상 을 순환 적 으로 포함 하 는 List, 고전적 인 while loop 이 가장 빠 른 순환 이다. 그러나 차이 가 크 지 않다.
    Benchmark                        (N)  Mode  Cnt   Score   Error  Units
    BenchmarkLoop.loopFor       10000000  avgt   10  61.673 ± 1.251  ms/op
    BenchmarkLoop.loopForEach   10000000  avgt   10  67.582 ± 1.034  ms/op
    BenchmarkLoop.loopIterator  10000000  avgt   10  66.087 ± 1.534  ms/op
    BenchmarkLoop.loopWhile     10000000  avgt   10  60.660 ± 0.279  ms/op

    5.2 자세 한 정 보 는 참고 하 시기 바 랍 니 다.
    $ java -jar target\benchmarks.jar BenchmarkLoop
    
    # JMH version: 1.21
    # VM version: JDK 10.0.1, Java HotSpot(TM) 64-Bit Server VM, 10.0.1+10
    # VM invoker: C:\Program Files\Java\jre-10.0.1\bin\java.exe
    # VM options: -Xms2G -Xmx2G
    # Warmup: 5 iterations, 10 s each
    # Measurement: 5 iterations, 10 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Average time, time/op
    # Benchmark: com.mkyong.benchmark.BenchmarkLoop.loopFor
    # Parameters: (N = 10000000)
    
    # Run progress: 0.00% complete, ETA 00:13:20
    # Fork: 1 of 2
    # Warmup Iteration   1: 60.920 ms/op
    # Warmup Iteration   2: 60.745 ms/op
    # Warmup Iteration   3: 60.818 ms/op
    # Warmup Iteration   4: 60.659 ms/op
    # Warmup Iteration   5: 60.765 ms/op
    Iteration   1: 63.579 ms/op
    Iteration   2: 61.622 ms/op
    Iteration   3: 61.869 ms/op
    Iteration   4: 61.730 ms/op
    Iteration   5: 62.207 ms/op
    
    # Run progress: 12.50% complete, ETA 00:11:50
    # Fork: 2 of 2
    # Warmup Iteration   1: 60.915 ms/op
    # Warmup Iteration   2: 61.527 ms/op
    # Warmup Iteration   3: 62.329 ms/op
    # Warmup Iteration   4: 62.729 ms/op
    # Warmup Iteration   5: 61.693 ms/op
    Iteration   1: 60.822 ms/op
    Iteration   2: 61.220 ms/op
    Iteration   3: 61.216 ms/op
    Iteration   4: 60.652 ms/op
    Iteration   5: 61.818 ms/op
    
    
    Result "com.mkyong.benchmark.BenchmarkLoop.loopFor":
      61.673 ±(99.9%) 1.251 ms/op [Average]
      (min, avg, max) = (60.652, 61.673, 63.579), stdev = 0.828
      CI (99.9%): [60.422, 62.925] (assumes normal distribution)
    
    
    # JMH version: 1.21
    # VM version: JDK 10.0.1, Java HotSpot(TM) 64-Bit Server VM, 10.0.1+10
    # VM invoker: C:\Program Files\Java\jre-10.0.1\bin\java.exe
    # VM options: -Xms2G -Xmx2G
    # Warmup: 5 iterations, 10 s each
    # Measurement: 5 iterations, 10 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Average time, time/op
    # Benchmark: com.mkyong.benchmark.BenchmarkLoop.loopForEach
    # Parameters: (N = 10000000)
    
    # Run progress: 25.00% complete, ETA 00:10:08
    # Fork: 1 of 2
    # Warmup Iteration   1: 67.938 ms/op
    # Warmup Iteration   2: 67.921 ms/op
    # Warmup Iteration   3: 68.064 ms/op
    # Warmup Iteration   4: 68.172 ms/op
    # Warmup Iteration   5: 68.181 ms/op
    Iteration   1: 68.378 ms/op
    Iteration   2: 68.069 ms/op
    Iteration   3: 68.487 ms/op
    Iteration   4: 68.300 ms/op
    Iteration   5: 67.635 ms/op
    
    # Run progress: 37.50% complete, ETA 00:08:27
    # Fork: 2 of 2
    # Warmup Iteration   1: 67.303 ms/op
    # Warmup Iteration   2: 67.062 ms/op
    # Warmup Iteration   3: 66.516 ms/op
    # Warmup Iteration   4: 66.973 ms/op
    # Warmup Iteration   5: 66.843 ms/op
    Iteration   1: 67.157 ms/op
    Iteration   2: 66.763 ms/op
    Iteration   3: 67.237 ms/op
    Iteration   4: 67.116 ms/op
    Iteration   5: 66.679 ms/op
    
    
    Result "com.mkyong.benchmark.BenchmarkLoop.loopForEach":
      67.582 ±(99.9%) 1.034 ms/op [Average]
      (min, avg, max) = (66.679, 67.582, 68.487), stdev = 0.684
      CI (99.9%): [66.548, 68.616] (assumes normal distribution)
    
    
    # JMH version: 1.21
    # VM version: JDK 10.0.1, Java HotSpot(TM) 64-Bit Server VM, 10.0.1+10
    # VM invoker: C:\Program Files\Java\jre-10.0.1\bin\java.exe
    # VM options: -Xms2G -Xmx2G
    # Warmup: 5 iterations, 10 s each
    # Measurement: 5 iterations, 10 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Average time, time/op
    # Benchmark: com.mkyong.benchmark.BenchmarkLoop.loopIterator
    # Parameters: (N = 10000000)
    
    # Run progress: 50.00% complete, ETA 00:06:46
    # Fork: 1 of 2
    # Warmup Iteration   1: 67.336 ms/op
    # Warmup Iteration   2: 73.008 ms/op
    # Warmup Iteration   3: 66.646 ms/op
    # Warmup Iteration   4: 70.157 ms/op
    # Warmup Iteration   5: 68.373 ms/op
    Iteration   1: 66.385 ms/op
    Iteration   2: 66.309 ms/op
    Iteration   3: 66.474 ms/op
    Iteration   4: 68.529 ms/op
    Iteration   5: 66.447 ms/op
    
    # Run progress: 62.50% complete, ETA 00:05:04
    # Fork: 2 of 2
    # Warmup Iteration   1: 65.499 ms/op
    # Warmup Iteration   2: 65.540 ms/op
    # Warmup Iteration   3: 67.328 ms/op
    # Warmup Iteration   4: 65.926 ms/op
    # Warmup Iteration   5: 65.790 ms/op
    Iteration   1: 65.350 ms/op
    Iteration   2: 65.634 ms/op
    Iteration   3: 65.353 ms/op
    Iteration   4: 65.164 ms/op
    Iteration   5: 65.225 ms/op
    
    
    Result "com.mkyong.benchmark.BenchmarkLoop.loopIterator":
      66.087 ±(99.9%) 1.534 ms/op [Average]
      (min, avg, max) = (65.164, 66.087, 68.529), stdev = 1.015
      CI (99.9%): [64.553, 67.621] (assumes normal distribution)
    
    
    # JMH version: 1.21
    # VM version: JDK 10.0.1, Java HotSpot(TM) 64-Bit Server VM, 10.0.1+10
    # VM invoker: C:\Program Files\Java\jre-10.0.1\bin\java.exe
    # VM options: -Xms2G -Xmx2G
    # Warmup: 5 iterations, 10 s each
    # Measurement: 5 iterations, 10 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Average time, time/op
    # Benchmark: com.mkyong.benchmark.BenchmarkLoop.loopWhile
    # Parameters: (N = 10000000)
    
    # Run progress: 75.00% complete, ETA 00:03:22
    # Fork: 1 of 2
    # Warmup Iteration   1: 60.290 ms/op
    # Warmup Iteration   2: 60.161 ms/op
    # Warmup Iteration   3: 60.245 ms/op
    # Warmup Iteration   4: 60.613 ms/op
    # Warmup Iteration   5: 60.697 ms/op
    Iteration   1: 60.842 ms/op
    Iteration   2: 61.062 ms/op
    Iteration   3: 60.417 ms/op
    Iteration   4: 60.650 ms/op
    Iteration   5: 60.514 ms/op
    
    # Run progress: 87.50% complete, ETA 00:01:41
    # Fork: 2 of 2
    # Warmup Iteration   1: 60.845 ms/op
    # Warmup Iteration   2: 60.927 ms/op
    # Warmup Iteration   3: 60.832 ms/op
    # Warmup Iteration   4: 60.817 ms/op
    # Warmup Iteration   5: 61.078 ms/op
    Iteration   1: 60.612 ms/op
    Iteration   2: 60.516 ms/op
    Iteration   3: 60.647 ms/op
    Iteration   4: 60.607 ms/op
    Iteration   5: 60.733 ms/op
    
    
    Result "com.mkyong.benchmark.BenchmarkLoop.loopWhile":
      60.660 ±(99.9%) 0.279 ms/op [Average]
      (min, avg, max) = (60.417, 60.660, 61.062), stdev = 0.184
      CI (99.9%): [60.381, 60.939] (assumes normal distribution)
    
    
    # Run complete. Total time: 00:13:31
    
    REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
    why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
    experiments, perform baseline and negative tests that provide experimental control, make sure
    the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
    Do not assume the numbers tell you what you want them to tell.
    
    Benchmark                        (N)  Mode  Cnt   Score   Error  Units
    BenchmarkLoop.loopFor       10000000  avgt   10  61.673 ± 1.251  ms/op
    BenchmarkLoop.loopForEach   10000000  avgt   10  67.582 ± 1.034  ms/op
    BenchmarkLoop.loopIterator  10000000  avgt   10  66.087 ± 1.534  ms/op
    BenchmarkLoop.loopWhile     10000000  avgt   10  60.660 ± 0.279  ms/op

    본 튜 토리 얼 이 JMH 기준 테스트 를 사용 하 는 빠 른 입문 안내 서 를 제공 하 기 를 바 랍 니 다. JMH 예제 에 관 한 더 많은 고급 정 보 는 이 공식 JMH 예제 링크 를 방문 하 십시오.
    정방 향 순환 과 역방향 순환 에 주의 하 는 것 이 어 떻 습 니까? 어느 것 이 더 빠 릅 니까? 이 JMH 테스트 에 방문 하 십시오.
    소스 코드 다운로드
    $ git clone https://github.com/mkyong/jmh-benchmark $mvn 패키지 $java - jar target \ \ benchmarks. jar BenchmarkLoop
    참고 문헌
  • OpenJDK:jmh
  • JHM 예시
  • Maven – 자바 프로젝트 를 만 드 는 방법
  • Java – While vs For vs Iterator 성능 테스트
  • 태그: 기본 자바 JMH 순환 성능
    번역https://mkyong.com/java/java-jmh-benchmark-tutorial/

    좋은 웹페이지 즐겨찾기