本文主要研究下parallelStream的性能。

测试1

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 3, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 20, time = 3, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class StreamBenchTest {
    List<String> data = new ArrayList<>();

    @Setup
    public void init() {
        // prepare
        for(int i=0;i<100;i++){
            data.add(UUID.randomUUID().toString());
        }
    }

    @TearDown
    public void destory() {
        // destory
    }

    @Benchmark
    public void benchStream(){
        data.stream().forEach(e -> {
            e.getBytes();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        });
    }

    @Benchmark
    public void benchParallelStream(){
        data.parallelStream().forEach(e -> {
            e.getBytes();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        });
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(".*" +StreamBenchTest.class.getSimpleName()+ ".*")
                .forks(1)
                .build();
        new Runner(opt).run();
    }

}

parallelStream线程数

默认是Runtime.getRuntime().availableProcessors() - 1,这里为7

运行结果

# Run complete. Total time: 00:02:44

Benchmark                            Mode  Cnt           Score         Error  Units
StreamBenchTest.benchParallelStream  avgt   20   155868805.437 ± 1509175.840  ns/op
StreamBenchTest.benchStream          avgt   20  1147570372.950 ± 6138494.414  ns/op

测试2

将数据data改为30,同时sleep改为100
Benchmark                            Mode  Cnt           Score         Error  Units
StreamBenchTest.benchParallelStream  avgt   20   414230854.631 ±  725294.455  ns/op
StreamBenchTest.benchStream          avgt   20  3107250608.500 ± 4805037.628  ns/op
可以发现sleep越长,parallelStream优势越明显。

小结

parallelStream在阻塞场景下优势更明显,其线程池个数默认为
Runtime.getRuntime().availableProcessors() - 1,如果需修改则需设置-Djava.util.concurrent.ForkJoinPool.common.parallelism=8

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论