1 /*
2  * Copyright (c) 2021, 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.value;
24 
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.BenchmarkMode;
27 import org.openjdk.jmh.annotations.Fork;
28 import org.openjdk.jmh.annotations.Measurement;
29 import org.openjdk.jmh.annotations.Mode;
30 import org.openjdk.jmh.annotations.OutputTimeUnit;
31 import org.openjdk.jmh.annotations.Param;
32 import org.openjdk.jmh.annotations.Scope;
33 import org.openjdk.jmh.annotations.Setup;
34 import org.openjdk.jmh.annotations.State;
35 import org.openjdk.jmh.annotations.Warmup;
36 
37 import java.util.concurrent.TimeUnit;
38 import java.util.stream.DoubleStream;
39 import java.util.stream.IntStream;
40 import java.util.stream.LongStream;
41 import java.util.stream.Stream;
42 
43 /**
44  * Benchmark for sum operation in sized streams.
45  */
46 @Fork(5)
47 @Warmup(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
48 @Measurement(iterations = 20, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
49 @BenchmarkMode(Mode.AverageTime)
50 @OutputTimeUnit(TimeUnit.NANOSECONDS)
51 @State(Scope.Thread)
52 public class SizedSum {
53     @Param({"5", "10"})
54     private int size;
55 
56     @Param({"true", "false"})
57     private boolean polluteTypeProfile;
58 
59     @Setup
setup()60     public void setup() {
61         if (!polluteTypeProfile) return;
62         for(int i=0; i<10000; i++) {
63             IntStream.empty().skip(1).count();
64             LongStream.empty().skip(1).count();
65             DoubleStream.empty().skip(1).count();
66             Stream.empty().skip(1).count();
67         }
68     }
69 
70     @Benchmark
sum0()71     public long sum0() {
72         return IntStream.range(0, size)
73             .sum();
74     }
75 
76     @Benchmark
sum2()77     public long sum2() {
78         return IntStream.range(0, size)
79             .map(x -> x)
80             .map(x -> x)
81             .sum();
82     }
83 
84     @Benchmark
sum4()85     public long sum4() {
86         return IntStream.range(0, size)
87             .map(x -> x)
88             .map(x -> x)
89             .map(x -> x)
90             .map(x -> x)
91             .sum();
92     }
93 
94     @Benchmark
sum6()95     public long sum6() {
96         return IntStream.range(0, size)
97             .map(x -> x)
98             .map(x -> x)
99             .map(x -> x)
100             .map(x -> x)
101             .map(x -> x)
102             .map(x -> x)
103             .sum();
104     }
105 
106     @Benchmark
sum8()107     public long sum8() {
108         return IntStream.range(0, size)
109             .map(x -> x)
110             .map(x -> x)
111             .map(x -> x)
112             .map(x -> x)
113             .map(x -> x)
114             .map(x -> x)
115             .map(x -> x)
116             .map(x -> x)
117             .sum();
118     }
119 
120     @Benchmark
sum10()121     public long sum10() {
122         return IntStream.range(0, size)
123             .map(x -> x)
124             .map(x -> x)
125             .map(x -> x)
126             .map(x -> x)
127             .map(x -> x)
128             .map(x -> x)
129             .map(x -> x)
130             .map(x -> x)
131             .map(x -> x)
132             .map(x -> x)
133             .sum();
134     }
135 
136     @Benchmark
sum10Skip()137     public long sum10Skip() {
138         return IntStream.range(0, size)
139             .skip(1)
140             .map(x -> x)
141             .map(x -> x)
142             .map(x -> x)
143             .map(x -> x)
144             .map(x -> x)
145             .map(x -> x)
146             .map(x -> x)
147             .map(x -> x)
148             .map(x -> x)
149             .map(x -> x)
150             .sum();
151     }
152 }
153