• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.base;
18 
19 import static com.google.common.base.Throwables.lazyStackTrace;
20 import static java.util.Arrays.asList;
21 
22 import com.google.caliper.BeforeExperiment;
23 import com.google.caliper.Benchmark;
24 import com.google.caliper.Param;
25 import com.google.caliper.api.SkipThisScenarioException;
26 import java.util.List;
27 
28 /**
29  * Quick and dirty benchmark of {@link Throwables#lazyStackTrace(Throwable)}. We benchmark a "caller
30  * finder" implementation that might be used in a logging framework.
31  */
32 public class LazyStackTraceBenchmark {
33   @Param({"20", "200", "2000"})
34   int stackDepth;
35 
36   @Param({"-1", "3", "15"})
37   int breakAt;
38 
39   int recursionCount;
40 
41   private static final Object duh = new Object();
42 
43   @Param Mode mode;
44 
45   enum Mode {
46     LAZY_STACK_TRACE {
47       @Override
getStackTrace(Throwable t)48       List<StackTraceElement> getStackTrace(Throwable t) {
49         return lazyStackTrace(t);
50       }
51     },
52     GET_STACK_TRACE {
53       @Override
getStackTrace(Throwable t)54       List<StackTraceElement> getStackTrace(Throwable t) {
55         return asList(t.getStackTrace());
56       }
57     };
58 
timeIt(int reps, int breakAt)59     boolean timeIt(int reps, int breakAt) {
60       boolean dummy = false;
61       for (int i = 0; i < reps; i++) {
62         int f = 0;
63         Throwable t = new Throwable();
64         for (StackTraceElement ste : getStackTrace(t)) {
65           dummy |= ste == duh;
66           if (f++ == breakAt) {
67             break;
68           }
69         }
70       }
71       return dummy;
72     }
73 
getStackTrace(Throwable t)74     abstract List<StackTraceElement> getStackTrace(Throwable t);
75   }
76 
77   @BeforeExperiment
doBefore()78   public void doBefore() {
79     recursionCount = stackDepth - new Throwable().getStackTrace().length - 1;
80     if (recursionCount < 0) {
81       throw new SkipThisScenarioException();
82     }
83   }
84 
85   @Benchmark
timeFindCaller(int reps)86   public boolean timeFindCaller(int reps) {
87     return timeFindCaller(reps, recursionCount);
88   }
89 
timeFindCaller(int reps, int recurse)90   private boolean timeFindCaller(int reps, int recurse) {
91     return recurse > 0 ? timeFindCaller(reps, recurse - 1) : mode.timeIt(reps, breakAt);
92   }
93 }
94