|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 The Guava Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.common.io; |
| 16 | + |
| 17 | +import com.google.caliper.BeforeExperiment; |
| 18 | +import com.google.caliper.Benchmark; |
| 19 | +import com.google.caliper.Param; |
| 20 | + |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +/** |
| 24 | + * Benchmark for {@code BaseEncoding} performance. |
| 25 | + */ |
| 26 | +public class BaseEncodingBenchmark { |
| 27 | + enum EncodingOption { |
| 28 | + BASE64(BaseEncoding.base64()), |
| 29 | + BASE64_URL(BaseEncoding.base64Url()), |
| 30 | + BASE32(BaseEncoding.base32()), |
| 31 | + BASE32_HEX(BaseEncoding.base32Hex()), |
| 32 | + BASE16(BaseEncoding.base16()); |
| 33 | + |
| 34 | + final BaseEncoding encoding; |
| 35 | + |
| 36 | + EncodingOption(BaseEncoding encoding) { |
| 37 | + this.encoding = encoding; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Param |
| 42 | + EncodingOption encoding; |
| 43 | + |
| 44 | + @Param({"10", "100", "10000"}) |
| 45 | + int n; |
| 46 | + |
| 47 | + private final byte[][] inputs = new byte[0x1000][]; |
| 48 | + |
| 49 | + @BeforeExperiment |
| 50 | + public void setUp() { |
| 51 | + Random rng = new Random(); |
| 52 | + for (int i = 0; i < inputs.length; i++) { |
| 53 | + inputs[i] = new byte[n]; |
| 54 | + rng.nextBytes(inputs[i]); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Benchmark public int encode(int reps) { |
| 59 | + int tmp = 0; |
| 60 | + for (int i = 0; i < reps; i++) { |
| 61 | + tmp += System.identityHashCode(encoding.encoding.encode(inputs[i & 0xFFF])); |
| 62 | + } |
| 63 | + return tmp; |
| 64 | + } |
| 65 | +} |
0 commit comments