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.DictionaryWordValue;
  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 import java.util.function.Function;
  39 
  40 
  41 /**
  42  * Bulk scenario: searching max "wordValue" through the dictionary (array of strings).
  43  */
  44 @BenchmarkMode(Mode.Throughput)
  45 @OutputTimeUnit(TimeUnit.SECONDS)
  46 @State(Scope.Benchmark)
  47 public class Bulk {
  48 
  49     @Setup(Level.Trial)
  50     public void loadData() {
  51         // cause classload and problem initialization
  52         DictionaryProblem.class.getName();
  53     }
  54 
  55     @Benchmark
  56     public int hm_seq() {
  57         int max = 0;
  58         for (String s : DictionaryProblem.get()) {
  59             int v = DictionaryProblem.wordValue(s);
  60             if (v > max) {
  61                 max = v;
  62             }
  63         }
  64         return max;
  65     }
  66 
  67     @Benchmark
  68     public int hm_par() {
  69         return new Task(DictionaryProblem.get(), 0, DictionaryProblem.get().length).invoke();
  70     }
  71 
  72     @Benchmark
  73     public int bulk_seq_inner() {
  74         return Arrays.stream(DictionaryProblem.get())
  75                 .map(new Function<String, Integer>() {
  76                     @Override
  77                     public Integer apply(String s) {
  78                         return DictionaryProblem.wordValue(s);
  79                     }
  80                 })
  81                 .reduce(0, new BinaryOperator<Integer>() {
  82                     @Override
  83                     public Integer apply(Integer l, Integer r) {
  84                         return l > r ? l : r;
  85                     }
  86                 });
  87     }
  88 
  89     @Benchmark
  90     public int bulk_par_inner() {
  91         return Arrays.stream(DictionaryProblem.get()).parallel()
  92                 .map(new Function<String, Integer>() {
  93                     @Override
  94                     public Integer apply(String s) {
  95                         return DictionaryProblem.wordValue(s);
  96                     }
  97                 })
  98                 .reduce(0, new BinaryOperator<Integer>() {
  99                     @Override
 100                     public Integer apply(Integer l, Integer r) {
 101                         return l > r ? l : r;
 102                     }
 103                 });
 104     }
 105 
 106 
 107     public static class Task extends RecursiveTask<Integer> {
 108         private static final int FORK_LIMIT = 500;
 109 
 110         private final String[] words;
 111         private final int start, end;
 112 
 113         Task(String[] w, int start, int end) {
 114             this.words = w;
 115             this.start = start;
 116             this.end = end;
 117         }
 118 
 119         @Override
 120         protected Integer compute() {
 121             int size = end - start;
 122             if (size > FORK_LIMIT) {
 123                 int mid = start + size / 2;
 124                 Task t1 = new Task(words, start, mid);
 125                 Task t2 = new Task(words, mid, end);
 126                 t1.fork();
 127                 Integer v2 = t2.invoke();
 128                 Integer v1 = t1.join();
 129                 return Math.max(v1, v2);
 130             } else {
 131                 int max = 0;
 132                 for (int i = start; i < end; i++) {
 133                     int v = DictionaryProblem.wordValue(words[i]);
 134                     if (v > max) {
 135                         max = v;
 136                     }
 137                 }
 138                 return max;
 139             }
 140         }
 141     }
 142 
 143 
 144 }