validating - Introducing JMH

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/89048582

It's impossible to write JMH code to run it from the command line, but recommended approach is to let the JMH system run the tests for we. 

JMH attempts to make benchmarks as easy as possible.

// validating/jmh/JMH1.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package validating.jmh;

import java.util.*;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
// Increase these three for more accuracy:
@Warmup(iterations = 5) 
@Measurement(iterations = 5)
@Fork(1)
public class JMH1 {
  private long[] la;

  @Setup
  public void setup() {
    la = new long[250_000_000];
  }

  @Benchmark
  public void setAll() {
    Arrays.setAll(la, n -> n);
  }

  @Benchmark
  public void parallelSetAll() {
    Arrays.parallelSetAll(la, n -> n);
  }
}
// validating/jmh/JMH2.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package validating.jmh;

import java.util.*;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(1)
public class JMH2 {
  private long[] la;

  @Param({
    "1",
    "10",
    "100",
    "1000",
    "10000",
    "100000",
    "1000000",
    "10000000",
    "100000000",
    "250000000"
  })
  int size;

  @Setup
  public void setup() {
    la = new long[size];
  }

  @Benchmark
  public void setAll() {
    Arrays.setAll(la, n -> n);
  }

  @Benchmark
  public void parallelSetAll() {
    Arrays.parallelSetAll(la, n -> n);
  }
}
// validating/jmh/JMH3.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package validating.jmh;

import java.util.*;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(1)
public class JMH3 {
  private long[] la;

  @Param({
    "1",
    "10",
    "100",
    "1000",
    "10000",
    "100000",
    "1000000",
    "10000000",
    "100000000",
    "250000000"
  })
  int size;

  @Setup
  public void setup() {
    la = new long[size];
  }

  public static long f(long x) {
    long quadratic = 42 * x * x + 19 * x + 47;
    return Long.divideUnsigned(quadratic, x + 1);
  }

  @Benchmark
  public void setAll() {
    Arrays.setAll(la, n -> f(n));
  }

  @Benchmark
  public void parallelSetAll() {
    Arrays.parallelSetAll(la, n -> f(n));
  }
}

My output:

Benchmark               (size)  Mode  Cnt        Score       Error  Units
JMH1.parallelSetAll        N/A  avgt    5   157727.439 ±  6052.117  us/op
JMH1.setAll                N/A  avgt    5   169563.712 ±  6135.280  us/op
JMH2.parallelSetAll          1  avgt    5        0.036 ±     0.001  us/op
JMH2.parallelSetAll         10  avgt    5       19.264 ±     2.765  us/op
JMH2.parallelSetAll        100  avgt    5       16.902 ±     4.910  us/op
JMH2.parallelSetAll       1000  avgt    5       22.066 ±    21.134  us/op
JMH2.parallelSetAll      10000  avgt    5       18.744 ±     4.856  us/op
JMH2.parallelSetAll     100000  avgt    5       33.685 ±     6.873  us/op
JMH2.parallelSetAll    1000000  avgt    5      221.920 ±    71.894  us/op
JMH2.parallelSetAll   10000000  avgt    5     6421.304 ±   365.326  us/op
JMH2.parallelSetAll  100000000  avgt    5    62885.102 ±  1960.576  us/op
JMH2.parallelSetAll  250000000  avgt    5   157213.034 ±  4628.642  us/op
JMH2.setAll                  1  avgt    5        0.001 ±     0.001  us/op
JMH2.setAll                 10  avgt    5        0.004 ±     0.001  us/op
JMH2.setAll                100  avgt    5        0.032 ±     0.004  us/op
JMH2.setAll               1000  avgt    5        0.324 ±     0.019  us/op
JMH2.setAll              10000  avgt    5        3.382 ±     0.135  us/op
JMH2.setAll             100000  avgt    5       36.090 ±     2.067  us/op
JMH2.setAll            1000000  avgt    5      394.066 ±    46.446  us/op
JMH2.setAll           10000000  avgt    5     6507.366 ±   291.420  us/op
JMH2.setAll          100000000  avgt    5    66977.791 ±  4388.401  us/op
JMH2.setAll          250000000  avgt    5   168200.696 ± 10460.397  us/op
JMH3.parallelSetAll          1  avgt    5        0.044 ±     0.005  us/op
JMH3.parallelSetAll         10  avgt    5       19.242 ±     6.843  us/op
JMH3.parallelSetAll        100  avgt    5       18.658 ±     6.920  us/op
JMH3.parallelSetAll       1000  avgt    5       25.622 ±    13.989  us/op
JMH3.parallelSetAll      10000  avgt    5       51.037 ±     3.264  us/op
JMH3.parallelSetAll     100000  avgt    5      354.044 ±    63.255  us/op
JMH3.parallelSetAll    1000000  avgt    5     3042.517 ±   464.108  us/op
JMH3.parallelSetAll   10000000  avgt    5    27311.351 ±  1740.651  us/op
JMH3.parallelSetAll  100000000  avgt    5   201354.372 ±  6309.172  us/op
JMH3.parallelSetAll  250000000  avgt    5   482230.222 ± 34593.250  us/op
JMH3.setAll                  1  avgt    5        0.011 ±     0.001  us/op
JMH3.setAll                 10  avgt    5        0.084 ±     0.002  us/op
JMH3.setAll                100  avgt    5        0.826 ±     0.005  us/op
JMH3.setAll               1000  avgt    5        8.218 ±     0.141  us/op
JMH3.setAll              10000  avgt    5       83.738 ±     0.937  us/op
JMH3.setAll             100000  avgt    5      804.059 ±    16.302  us/op
JMH3.setAll            1000000  avgt    5     8376.048 ±   101.816  us/op
JMH3.setAll           10000000  avgt    5    83754.080 ±   949.626  us/op
JMH3.setAll          100000000  avgt    5   841625.117 ± 15733.665  us/op
JMH3.setAll          250000000  avgt    5  2097034.637 ± 15399.819  us/op

avgt: Average Time

us/op Microseconds per operation

see more info click JMH samples.

Aprofiler finds the slow spots so we can look for the easiest, most obvious way speed things up.

Note the difference between profiling and benchmarking. Profiling looks at our completed program working on our actual data, whereas benchmarking looks at an isolated fragment of a program, typically to optimize an algorithm

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/jmh/JMH1.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/jmh/JMH2.java

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/jmh/JMH3.java

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/89048582
jmh