• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.Param;
22 import com.google.caliper.api.SkipThisScenarioException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.LinkedList;
26 import java.util.NoSuchElementException;
27 import java.util.function.Supplier;
28 import java.util.stream.Stream;
29 
30 /**
31  * Test stream operation speed.
32  *
33  * @author Louis Wasserman
34  */
35 public class StreamsBenchmark {
36   @Param({"1", "10", "100", "1000", "10000"})
37   private int size;
38 
39   enum CollectionType {
40     ARRAY_LIST(ArrayList::new),
41     LINKED_LIST(LinkedList::new);
42 
43     final Supplier<Collection<Object>> supplier;
44 
CollectionType(Supplier<Collection<Object>> supplier)45     private CollectionType(Supplier<Collection<Object>> supplier) {
46       this.supplier = supplier;
47     }
48   }
49 
50   @Param private CollectionType source;
51 
52   enum Operation {
53     FIND_FIRST {
54       @Override
operate(Stream<?> stream)55       Object operate(Stream<?> stream) {
56         return stream.findFirst();
57       }
58     },
59     STREAMS_ONLY_ELEMENT {
60       @Override
operate(Stream<?> stream)61       Object operate(Stream<?> stream) {
62         try {
63           return stream.collect(MoreCollectors.onlyElement());
64         } catch (IllegalArgumentException | NoSuchElementException e) {
65           throw new SkipThisScenarioException();
66         }
67       }
68     },
69     STREAMS_FIND_LAST {
70       @Override
operate(Stream<?> stream)71       Object operate(Stream<?> stream) {
72         return Streams.findLast(stream);
73       }
74     },
75     REDUCE_LAST {
76       @Override
operate(Stream<?> stream)77       Object operate(Stream<?> stream) {
78         return stream.reduce((a, b) -> b);
79       }
80     },
81     REDUCE_LAST_PARALLEL {
82       @Override
operate(Stream<?> stream)83       Object operate(Stream<?> stream) {
84         return stream.parallel().reduce((a, b) -> b);
85       }
86     };
87 
operate(Stream<?> stream)88     abstract Object operate(Stream<?> stream);
89   }
90 
91   @Param private Operation operation;
92 
93   Collection<Object> collection;
94 
95   @BeforeExperiment
setUp()96   void setUp() {
97     collection = source.supplier.get();
98     for (int i = 0; i < size; i++) {
99       collection.add(new Object());
100     }
101   }
102 
103   @Benchmark
runOperation(int reps)104   int runOperation(int reps) {
105     int result = 0;
106     for (int i = 0; i < reps; i++) {
107       result += System.identityHashCode(operation.operate(collection.stream()));
108     }
109     return result;
110   }
111 }
112