Skip to content

Instantly share code, notes, and snippets.

@rolroralra
Last active September 18, 2022 10:43
Show Gist options
  • Save rolroralra/b086d6323137edc7d4928894774019dc to your computer and use it in GitHub Desktop.
Save rolroralra/b086d6323137edc7d4928894774019dc to your computer and use it in GitHub Desktop.
BMT (Performance Test)

JMH (Java Microbenchmark Harness)

https://ysjee141.github.io/blog/quality/java-benchmark/

Example Code

import java.util.concurrent.TimeUnit;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;


/**
 * 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                                  Mode  Cnt   Score   Error  Units
 * ParallelStreamBenchmark.iterativeSum       avgt   10   2.840 ± 0.024  ms/op
 * ParallelStreamBenchmark.parallelRangedSum  avgt   10   1.253 ± 0.219  ms/op
 * ParallelStreamBenchmark.parallelSum        avgt   10  75.781 ± 4.941  ms/op
 * ParallelStreamBenchmark.rangedSum          avgt   10   3.841 ± 0.005  ms/op
 * ParallelStreamBenchmark.sequentialSum      avgt   10  64.648 ± 1.354  ms/op
 */
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(value = 2, jvmArgs = { "-Xms4G", "-Xmx4G" })
//@Measurement(iterations = 2)
//@Warmup(iterations = 3)
public class ParallelStreamBenchmark {

  private static final long N = 10_000_000L;

  @Benchmark
  public long iterativeSum() {
    long result = 0;
    for (long i = 1L; i <= N; i++) {
      result += i;
    }
    return result;
  }

  @Benchmark
  public long sequentialSum() {
    return Stream.iterate(1L, i -> i + 1).limit(N).reduce(0L, Long::sum);
  }

  @Benchmark
  public long parallelSum() {
    return Stream.iterate(1L, i -> i + 1).limit(N).parallel().reduce(0L, Long::sum);
  }

  @Benchmark
  public long rangedSum() {
    return LongStream.rangeClosed(1, N).reduce(0L, Long::sum);
  }

  @Benchmark
  public long parallelRangedSum() {
    return LongStream.rangeClosed(1, N).parallel().reduce(0L, Long::sum);
  }

  @TearDown(Level.Invocation)
  public void tearDown() {
    System.gc();
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment