test/micro/org/openjdk/bench/java/util/stream/pipeline/Terminal.java
author redestad
Fri, 16 Nov 2018 23:39:51 +0100
changeset 52595 16609197022c
permissions -rw-r--r--
8061281: Microbenchmark suite build support, directory layout and sample benchmarks 8061282: Migrate jmh-jdk-microbenchmarks into the JDK Reviewed-by: ecaspole, mchung, erikj, ihse Contributed-by: magnus.ihse.bursie@oracle.com, erik.joelsson@oracle.com, claes.redestad@oracle.com, sfriberg@kth.se
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     1
/*
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     4
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     8
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    13
 * accompanied this code).
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    14
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    18
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    21
 * questions.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    22
 */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    23
package org.openjdk.bench.java.util.stream.pipeline;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    24
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    25
import org.openjdk.jmh.annotations.Benchmark;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    26
import org.openjdk.jmh.annotations.BenchmarkMode;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    27
import org.openjdk.jmh.annotations.Mode;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    28
import org.openjdk.jmh.annotations.OutputTimeUnit;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    29
import org.openjdk.jmh.annotations.Param;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    30
import org.openjdk.jmh.annotations.Scope;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    31
import org.openjdk.jmh.annotations.Setup;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    32
import org.openjdk.jmh.annotations.State;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    33
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    34
import java.util.Iterator;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    35
import java.util.concurrent.TimeUnit;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    36
import java.util.concurrent.atomic.LongAdder;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    37
import java.util.function.LongConsumer;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    38
import java.util.stream.LongStream;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    39
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    40
/**
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    41
 * Benchmark for forEach()/iterator()/into() operations;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    42
 * Testing which one is faster for semantically-equvalent operations.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    43
 */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    44
@BenchmarkMode(Mode.Throughput)
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    45
@OutputTimeUnit(TimeUnit.SECONDS)
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    46
@State(Scope.Thread)
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    47
public class Terminal {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    48
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    49
    /**
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    50
     * Implementation notes:
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    51
     *   - parallel version requires thread-safe sink, we use the same for sequential version for better comparison
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    52
     *   - operations are explicit inner classes to untangle unwanted lambda effects
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    53
     */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    54
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    55
    @Param("100000")
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    56
    private int size;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    57
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    58
    private LongAdder sink;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    59
    private LongConsumer block;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    60
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    61
    @Setup
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    62
    public void setup() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    63
        sink = new LongAdder();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    64
        block = new LongConsumer() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    65
            @Override
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    66
            public void accept(long v) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    67
                sink.add(v);
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    68
            }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    69
        };
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    70
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    71
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    72
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    73
    public long baseline_prim_acc() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    74
        long s = 0;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    75
        for (long l = 0L; l < size; l++) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    76
            s += l;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    77
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    78
        sink.add(s);
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    79
        return sink.sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    80
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    81
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    82
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    83
    public long baseline_prim_sink() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    84
        for (long l = 0L; l < size; l++) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    85
            sink.add(l);
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    86
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    87
        return sink.sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    88
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    89
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    90
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    91
    public long baseline_iterator_acc() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    92
        long s = 0;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    93
        for (Iterator<Long> iterator = LongStream.range(0, size).boxed().iterator(); iterator.hasNext(); ) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    94
            Long l = iterator.next();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    95
            s += l;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    96
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    97
        sink.add(s);
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    98
        return sink.sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    99
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   100
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   101
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   102
    public long baseline_iterator_sink() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   103
        for (Iterator<Long> iterator = LongStream.range(0, size).boxed().iterator(); iterator.hasNext(); ) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   104
            sink.add(iterator.next());
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   105
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   106
        return sink.sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   107
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   108
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   109
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   110
    public long seq_iterator() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   111
        Iterator<Long> i = LongStream.range(0, size).boxed().iterator();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   112
        while (i.hasNext()) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   113
            sink.add(i.next());
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   114
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   115
        return sink.sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   116
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   117
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   118
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   119
    public long par_iterator() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   120
        Iterator<Long> i = LongStream.range(0, size).parallel().boxed().iterator();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   121
        while (i.hasNext()) {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   122
            sink.add(i.next());
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   123
        }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   124
        return sink.sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   125
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   126
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   127
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   128
    public long seq_forEach() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   129
        LongStream.range(1, size).forEach(block);
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   130
        return sink.sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   131
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   132
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   133
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   134
    public long par_forEach() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   135
        LongStream.range(1, size).parallel().forEach(block);
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   136
        return sink.sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   137
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   138
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   139
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   140
    public long seq_into() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   141
        return LongStream.range(1, size)
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   142
                .collect(LongAdder::new, LongAdder::add, (la1, la2) -> la1.add(la2.sum())).sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   143
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   144
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   145
    @Benchmark
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   146
    public long par_into() {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   147
        return LongStream.range(1, size).parallel()
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   148
                .collect(LongAdder::new, LongAdder::add, (la1, la2) -> la1.add(la2.sum())).sum();
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   149
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   150
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   151
}