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