package _1_simple_stream;

import org.junit.Test;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
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 org.openjdk.jmh.runner.options.TimeValue;
import org.openjdk.jmh.runner.options.VerboseMode;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class IterationTest {
    @Test
    public void launchBenchmark() throws Exception {
        Options opt = new OptionsBuilder()
                // Specify which benchmarks to run.
                // You can be more specific if you'd like to run only one benchmark per test.
                .include(this.getClass().getSimpleName())
                // Set the following options as needed
                .mode(Mode.AverageTime)
                .timeUnit(TimeUnit.MICROSECONDS)
                .warmupIterations(10)
                .measurementIterations(10)
                .forks(1)
                .shouldFailOnError(true)
                .shouldDoGC(true)
                .jvmArgs("-XX:+UnlockDiagnosticVMOptions")
                .build();

        new Runner(opt).run();
    }

    @State(Scope.Thread)
    public static class BenchmarkState {
        @Setup(Level.Trial)
        public void
        initialize() {
        }
    }

    @Benchmark
    public void sequentialIteration(BenchmarkState state, Blackhole bh) {
        for (int i = 0; i < 1_000; i++) {
            int a = i * i;
            bh.consume(a);
        }
    }

    @Benchmark
    public void sequentialStreamIteration(BenchmarkState state, Blackhole bh) {
        IntStream.iterate(0, i -> i)
                .limit(1_000)
                .forEach(i -> {
                    int a = i * i;
                    bh.consume(a);
                });
    }

    @Benchmark
    public void parallelStreamIteration(BenchmarkState state, Blackhole bh) {
        IntStream.iterate(0, i -> i)
                .parallel()
                .limit(1_000)
                .forEach(i -> {
                    int a = i * i;
                    bh.consume(a);
                });
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(IterationTest.class.getSimpleName())
                .warmupIterations(2)
                .warmupTime(TimeValue.milliseconds(2))
                .measurementIterations(2)
                .measurementTime(TimeValue.milliseconds(2))
                .verbosity(VerboseMode.NORMAL)
                .forks(1)
                .build();

        new Runner(opt).run();
    }
}

@@ -717,24 +717,33 @@
 ss Iteration
+Benchmark
 Test %7B%0A    @
@@ -847,154 +847,8 @@
 r()%0A
-                // Specify which benchmarks to run.%0A                // You can be more specific if you'd like to run only one benchmark per test.%0A
     
@@ -905,63 +905,8 @@
 ())%0A
-                // Set the following options as needed%0A
     
@@ -1016,34 +1016,33 @@
 armupIterations(
-10
+5
 )%0A              
@@ -1070,10 +1070,9 @@
 ons(
-10
+5
 )%0A  
@@ -1140,101 +1140,8 @@
 ue)%0A
-                .shouldDoGC(true)%0A                .jvmArgs(%22-XX:+UnlockDiagnosticVMOptions%22)%0A
     
@@ -2310,16 +2310,25 @@
 teration
+Benchmark
 Test.cla

Tweak benchmark