• 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 
22 /**
23  * Some microbenchmarks for the {@link Objects.ToStringHelper} class.
24  *
25  * @author Osvaldo Doederlein
26  */
27 public class ToStringHelperBenchmark {
28 
29   @Param({"0", "2", "5", "10"}) int dataSize;
30   private static final String NAME = "abcdefgh";
31   private static final String NAME3 = Strings.repeat(NAME, 3);
32 
addEntries(Objects.ToStringHelper helper)33   private static void addEntries(Objects.ToStringHelper helper) {
34     helper
35       .add(NAME, 10)
36       .addValue(10L)
37       .add(NAME, 3.14f)
38       .addValue(3.14d)
39       .add(NAME3, false)
40       .add(NAME3, NAME3)
41       .add(NAME3, 'x');
42   }
43 
toString(int reps)44   @Benchmark int toString(int reps) {
45     int dummy = 0;
46     for (int i = 0; i < reps; i++) {
47       Objects.ToStringHelper helper = Objects.toStringHelper("klass").omitNullValues();
48       for (int j = 0; j < dataSize; ++j) {
49         addEntries(helper);
50       }
51       dummy ^= helper
52           .toString().hashCode();
53     }
54     return dummy;
55   }
56 }
57