1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.java.util.stream.tasks.IntegerSum;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Level;
  28 import org.openjdk.jmh.annotations.Mode;
  29 import org.openjdk.jmh.annotations.OutputTimeUnit;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.Setup;
  32 import org.openjdk.jmh.annotations.State;
  33 
  34 import java.util.Arrays;
  35 import java.util.concurrent.RecursiveTask;
  36 import java.util.concurrent.TimeUnit;
  37 import java.util.function.BinaryOperator;
  38 
  39 /**
  40  * Bulk scenario: compute the sum of Integer array.
  41  *
  42  * This test covers bulk infrastructure only. Refer to other tests for lambda-specific cases.
  43  */
  44 @BenchmarkMode(Mode.Throughput)
  45 @OutputTimeUnit(TimeUnit.SECONDS)
  46 @State(Scope.Benchmark)
  47 public class Bulk {
  48 
  49     private IntegerSumProblem problem;
  50 
  51     @Setup(Level.Trial)
  52     public void populateData(){
  53         problem = new IntegerSumProblem();
  54     }
  55 
  56     @Benchmark
  57     public int hm_seq() {
  58         int sum = 0;
  59         for (int v : problem.get()) {
  60             sum += v;
  61         }
  62         return sum;
  63     }
  64 
  65     @Benchmark
  66     public int hm_par() {
  67         return new SumIntTask(problem.get()).invoke();
  68     }
  69 
  70     @Benchmark
  71     public int bulk_seq_inner() {
  72         return Arrays.stream(problem.get()).reduce(0, new BinaryOperator<Integer>() {
  73             @Override
  74             public Integer apply(Integer left, Integer right) {
  75                 return left + right;
  76             }
  77         });
  78     }
  79 
  80     @Benchmark
  81     public int bulk_par_inner() {
  82         return Arrays.stream(problem.get()).parallel().reduce(0, new BinaryOperator<Integer>() {
  83             @Override
  84             public Integer apply(Integer left, Integer right) {
  85                 return left + right;
  86             }
  87         });
  88     }
  89 
  90     static class SumIntTask extends RecursiveTask<Integer> {
  91         private static final int FORK_LIMIT = 1000;
  92         final Integer[] data;
  93         final int start, end;
  94 
  95         SumIntTask(Integer[] data) {
  96             this(data, 0, data.length);
  97         }
  98 
  99         SumIntTask(Integer[] data, int start, int end) {
 100             this.data = data;
 101             this.start = start;
 102             this.end = end;
 103         }
 104 
 105         @Override
 106         protected Integer compute() {
 107             int size = end - start;
 108             if (size > FORK_LIMIT) {
 109                 int mid = start + size / 2;
 110                 SumIntTask t1 = new SumIntTask(data, start, mid);
 111                 SumIntTask t2 = new SumIntTask(data, mid, end);
 112                 t1.fork();
 113                 return t2.invoke() + t1.join();
 114             } else {
 115                 int sum = 0;
 116                 for (int i = start; i < end; i++) {
 117                     sum += data[i];
 118                 }
 119                 return sum;
 120             }
 121         }
 122     }
 123 
 124 }