• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 com.google.caliper.Benchmark;
20 import com.google.caliper.Param;
21 import java.util.Arrays;
22 import java.util.Collections;
23 
24 /**
25  * Some microbenchmarks for the {@link MoreObjects.ToStringHelper} class.
26  *
27  * @author Osvaldo Doederlein
28  */
29 public class ToStringHelperBenchmark {
30 
31   @Param({"0", "1", "5"})
32   int dataSize;
33 
34   @Param({"false", "true"})
35   boolean omitNulls;
36 
37   enum Dataset {
38     SMALL {
39       @Override
addEntries(MoreObjects.ToStringHelper helper)40       void addEntries(MoreObjects.ToStringHelper helper) {
41         helper
42             .add(SHORT_NAME, 10)
43             .addValue(10L)
44             .add(SHORT_NAME, 3.14f)
45             .addValue(3.14d)
46             .add(LONG_NAME, false)
47             .add(LONG_NAME, LONG_NAME);
48       }
49     },
50     CONDITIONAL {
51       @Override
addEntries(MoreObjects.ToStringHelper helper)52       void addEntries(MoreObjects.ToStringHelper helper) {
53         helper
54             .add(SHORT_NAME, "x")
55             .add(LONG_NAME, "y")
56             .add(SHORT_NAME, null)
57             .add(LONG_NAME, null)
58             .addValue("z")
59             .addValue("")
60             .addValue(null)
61             .add(SHORT_NAME, Arrays.asList("A"))
62             .add(LONG_NAME, Arrays.asList("B"))
63             .add(SHORT_NAME, Arrays.asList())
64             .add(LONG_NAME, Arrays.asList())
65             .addValue(Arrays.asList("C"))
66             .addValue(Arrays.asList())
67             .add(SHORT_NAME, Collections.singletonMap("k1", "v1"))
68             .add(LONG_NAME, Collections.singletonMap("k2", "v2"))
69             .addValue(Collections.singletonMap("k3", "v3"))
70             .addValue(Collections.emptyMap())
71             .addValue(null)
72             .add(SHORT_NAME, java.util.Optional.of("1"))
73             .add(LONG_NAME, java.util.Optional.of("1"))
74             .add(SHORT_NAME, java.util.Optional.empty())
75             .add(LONG_NAME, java.util.Optional.empty())
76             .add(SHORT_NAME, Optional.of("2"))
77             .add(SHORT_NAME, Optional.absent())
78             .addValue(null)
79             .add(SHORT_NAME, new int[] {1})
80             .add(LONG_NAME, new int[] {2})
81             .addValue(new int[] {3})
82             .addValue(new int[] {})
83             .addValue(null);
84       }
85     },
86     UNCONDITIONAL {
87       @Override
addEntries(MoreObjects.ToStringHelper helper)88       void addEntries(MoreObjects.ToStringHelper helper) {
89         helper
90             .add(SHORT_NAME, false)
91             .add(LONG_NAME, false)
92             .addValue(true)
93             .add(SHORT_NAME, (byte) 1)
94             .add(LONG_NAME, (byte) 2)
95             .addValue((byte) 3)
96             .add(SHORT_NAME, 'A')
97             .add(LONG_NAME, 'B')
98             .addValue('C')
99             .add(SHORT_NAME, (short) 4)
100             .add(LONG_NAME, (short) 5)
101             .addValue((short) 6)
102             .add(SHORT_NAME, 7)
103             .add(LONG_NAME, 8)
104             .addValue(9)
105             .add(SHORT_NAME, 10L)
106             .add(LONG_NAME, 11L)
107             .addValue(12L)
108             .add(SHORT_NAME, 13.0f)
109             .add(LONG_NAME, 14.0f)
110             .addValue(15.0f);
111       }
112     };
113 
addEntries(MoreObjects.ToStringHelper helper)114     void addEntries(MoreObjects.ToStringHelper helper) {}
115   }
116 
117   @Param Dataset dataset;
118 
119   private static final String SHORT_NAME = "userId";
120   private static final String LONG_NAME = "fluxCapacitorFailureRate95Percentile";
121 
newHelper()122   private MoreObjects.ToStringHelper newHelper() {
123     MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper("klass");
124     if (omitNulls) {
125       helper = helper.omitNullValues();
126     }
127     return helper;
128   }
129 
130   @Benchmark
toString(int reps)131   int toString(int reps) {
132     int dummy = 0;
133     for (int i = 0; i < reps; i++) {
134       MoreObjects.ToStringHelper helper = newHelper();
135       for (int j = 0; j < dataSize; ++j) {
136         dataset.addEntries(helper);
137       }
138       dummy ^= helper.toString().hashCode();
139     }
140     return dummy;
141   }
142 
143   // When omitEmptyValues() is released, remove this method and add a new @Param "omitEmptyValues".
144 }
145