1 /*
2  * Copyright (C) 2014 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.util.concurrent;
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.VmOptions;
23 import com.google.common.util.concurrent.AbstractFutureBenchmarks.Facade;
24 import com.google.common.util.concurrent.AbstractFutureBenchmarks.Impl;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.CancellationException;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 
32 /** A benchmark that times how long it takes to add a given number of */
33 @VmOptions({"-Xms8g", "-Xmx8g"})
34 public class SingleThreadAbstractFutureBenchmark {
35   @Param Impl impl;
36 
37   private final Exception exception = new Exception();
38   private Facade<?> notDoneFuture;
39 
40   @BeforeExperiment
setUp()41   void setUp() throws Exception {
42     notDoneFuture = impl.newFacade();
43   }
44 
45   @Benchmark
timeComplete_Normal(int reps)46   public long timeComplete_Normal(int reps) throws Exception {
47     long r = 0;
48     List<Facade<Integer>> list = new ArrayList<>(reps);
49     for (int i = 0; i < reps; i++) {
50       final Facade<Integer> localFuture = impl.newFacade();
51       list.add(localFuture);
52       localFuture.set(i);
53     }
54     for (int i = 0; i < reps; i++) {
55       r += list.get(i).get();
56     }
57     return r;
58   }
59 
60   @Benchmark
timeComplete_Failure(int reps)61   public long timeComplete_Failure(int reps) throws Exception {
62     long r = 0;
63     List<Facade<Integer>> list = new ArrayList<>(reps);
64     for (int i = 0; i < reps; i++) {
65       final Facade<Integer> localFuture = impl.newFacade();
66       list.add(localFuture);
67       localFuture.setException(exception);
68     }
69     for (int i = 0; i < reps; i++) {
70       Facade<Integer> facade = list.get(i);
71       try {
72         facade.get();
73         r++;
74       } catch (ExecutionException e) {
75         r += 2;
76       }
77     }
78     return r;
79   }
80 
81   @Benchmark
timeComplete_Cancel(int reps)82   public long timeComplete_Cancel(int reps) throws Exception {
83     long r = 0;
84     List<Facade<Integer>> list = new ArrayList<>(reps);
85     for (int i = 0; i < reps; i++) {
86       final Facade<Integer> localFuture = impl.newFacade();
87       list.add(localFuture);
88       localFuture.cancel(false);
89     }
90     for (int i = 0; i < reps; i++) {
91       Facade<Integer> facade = list.get(i);
92       try {
93         facade.get();
94         r++;
95       } catch (CancellationException e) {
96         r += 2;
97       }
98     }
99     return r;
100   }
101 
102   @Benchmark
timeGetWith0Timeout(long reps)103   public long timeGetWith0Timeout(long reps) throws Exception {
104     Facade<?> f = notDoneFuture;
105     long r = 0;
106     for (int i = 0; i < reps; i++) {
107       try {
108         f.get(0, TimeUnit.SECONDS);
109         r += 1;
110       } catch (TimeoutException e) {
111         r += 2;
112       }
113     }
114     return r;
115   }
116 
117   @Benchmark
timeGetWithSmallTimeout(long reps)118   public long timeGetWithSmallTimeout(long reps) throws Exception {
119     Facade<?> f = notDoneFuture;
120     long r = 0;
121     for (int i = 0; i < reps; i++) {
122       try {
123         f.get(500, TimeUnit.NANOSECONDS);
124         r += 1;
125       } catch (TimeoutException e) {
126         r += 2;
127       }
128     }
129     return r;
130   }
131 }
132