• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.util.Random;
26 
27 /** Benchmark for {@code BaseEncoding} performance. */
28 public class BaseEncodingBenchmark {
29   private static final int INPUTS_COUNT = 0x1000;
30   private static final int INPUTS_MASK = 0xFFF;
31 
32   enum EncodingOption {
33     BASE64(BaseEncoding.base64()),
34     BASE64_URL(BaseEncoding.base64Url()),
35     BASE32(BaseEncoding.base32()),
36     BASE32_HEX(BaseEncoding.base32Hex()),
37     BASE16(BaseEncoding.base16());
38 
39     final BaseEncoding encoding;
40 
EncodingOption(BaseEncoding encoding)41     EncodingOption(BaseEncoding encoding) {
42       this.encoding = encoding;
43     }
44   }
45 
46   @Param EncodingOption encoding;
47 
48   @Param({"10", "100", "10000"})
49   int n;
50 
51   private final byte[][] encodingInputs = new byte[INPUTS_COUNT][];
52   private final String[] decodingInputs = new String[INPUTS_COUNT];
53 
54   @BeforeExperiment
setUp()55   public void setUp() {
56     Random rng = new Random();
57     for (int i = 0; i < encodingInputs.length; i++) {
58       encodingInputs[i] = new byte[n];
59       rng.nextBytes(encodingInputs[i]);
60       decodingInputs[i] = encoding.encoding.encode(encodingInputs[i]);
61     }
62   }
63 
64   @Benchmark
encode(int reps)65   public int encode(int reps) {
66     int tmp = 0;
67     for (int i = 0; i < reps; i++) {
68       tmp += System.identityHashCode(encoding.encoding.encode(encodingInputs[i & INPUTS_MASK]));
69     }
70     return tmp;
71   }
72 
73   @Benchmark
decode(int reps)74   public int decode(int reps) {
75     int tmp = 0;
76     for (int i = 0; i < reps; i++) {
77       tmp += System.identityHashCode(encoding.encoding.decode(decodingInputs[i & INPUTS_MASK]));
78     }
79     return tmp;
80   }
81 
82   @Benchmark
encodingStream(int reps)83   public int encodingStream(int reps) throws IOException {
84     int tmp = 0;
85     for (int i = 0; i < reps; i++) {
86       StringWriter target = new StringWriter(2 * n);
87       OutputStream encodingStream = encoding.encoding.encodingStream(target);
88       encodingStream.write(encodingInputs[i & INPUTS_MASK]);
89       encodingStream.close();
90       tmp += target.getBuffer().length();
91     }
92     return tmp;
93   }
94 
95   @Benchmark
decodingStream(int reps)96   public int decodingStream(int reps) throws IOException {
97     int tmp = 0;
98     byte[] target = new byte[n];
99     for (int i = 0; i < reps; i++) {
100       StringReader source = new StringReader(decodingInputs[i & INPUTS_MASK]);
101       InputStream decodingStream = encoding.encoding.decodingStream(source);
102       decodingStream.read(target);
103       decodingStream.close();
104       tmp += target[0];
105     }
106     return tmp;
107   }
108 }
109