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.ops.ref;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Param;
  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.concurrent.TimeUnit;
  35 import java.util.function.Predicate;
  36 import java.util.stream.LongStream;
  37 
  38 /**
  39  * Benchmark for noneMatch() operation.
  40  * Focuses on short-circuiting behavior.
  41  */
  42 @BenchmarkMode(Mode.Throughput)
  43 @OutputTimeUnit(TimeUnit.SECONDS)
  44 @State(Scope.Thread)
  45 public class NoneMatchShort {
  46 
  47     /**
  48      * Implementation notes:
  49      *   - operations are explicit inner classes to untangle unwanted lambda effects
  50      *   - test the predicate which will become true closer to start, in the middle, and closer to the end
  51      */
  52 
  53     @Param("100000")
  54     private int size;
  55 
  56     @Param("100")
  57     private int offset;
  58 
  59     private Predicate<Long> pMid, pStart, pEnd;
  60 
  61     @Setup
  62     public void setup() {
  63         pStart = new Predicate<Long>() {
  64             @Override
  65             public boolean test(Long v) {
  66                 return v > offset;
  67             }
  68         };
  69         pMid = new Predicate<Long>() {
  70             @Override
  71             public boolean test(Long v) {
  72                 return v > size / 2;
  73             }
  74         };
  75         pEnd = new Predicate<Long>() {
  76             @Override
  77             public boolean test(Long v) {
  78                 return v > size - offset;
  79             }
  80         };
  81     }
  82 
  83     @Benchmark
  84     public boolean seq_start() {
  85         return LongStream.range(0, size).boxed().noneMatch(pStart);
  86     }
  87 
  88     @Benchmark
  89     public boolean seq_mid() {
  90         return LongStream.range(0, size).boxed().noneMatch(pMid);
  91     }
  92 
  93     @Benchmark
  94     public boolean seq_end() {
  95         return LongStream.range(0, size).boxed().noneMatch(pEnd);
  96     }
  97 
  98     @Benchmark
  99     public boolean par_start() {
 100         return LongStream.range(0, size).parallel().boxed().noneMatch(pStart);
 101     }
 102 
 103     @Benchmark
 104     public boolean par_mid() {
 105         return LongStream.range(0, size).parallel().boxed().noneMatch(pMid);
 106     }
 107 
 108     @Benchmark
 109     public boolean par_end() {
 110         return LongStream.range(0, size).parallel().boxed().noneMatch(pEnd);
 111     }
 112 
 113 }