/*
 * Copyright (C) 2014 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.common.io;

import com.google.caliper.BeforeExperiment;
import com.google.caliper.Benchmark;
import com.google.caliper.Param;

import java.util.Random;

/**
 * Benchmark for {@code BaseEncoding} performance.
 */
public class BaseEncodingBenchmark {
  enum EncodingOption {
    BASE64(BaseEncoding.base64()),
    BASE64_URL(BaseEncoding.base64Url()),
    BASE32(BaseEncoding.base32()),
    BASE32_HEX(BaseEncoding.base32Hex()),
    BASE16(BaseEncoding.base16());
    
    final BaseEncoding encoding;
    
    EncodingOption(BaseEncoding encoding) {
      this.encoding = encoding;
    }
  }
  
  @Param
  EncodingOption encoding;
  
  @Param({"10", "100", "10000"})
  int n;
  
  private final byte[][] inputs = new byte[0x1000][];

  @BeforeExperiment
  public void setUp() {
    Random rng = new Random();
    for (int i = 0; i < inputs.length; i++) {
      inputs[i] = new byte[n];
      rng.nextBytes(inputs[i]);
    }
  }
  
  @Benchmark public int encode(int reps) {
    int tmp = 0;
    for (int i = 0; i < reps; i++) {
      tmp += System.identityHashCode(encoding.encoding.encode(inputs[i & 0xFFF]));
    }
    return tmp;
  }
}

@@ -735,16 +735,159 @@
 Param;%0A%0A
+import java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0Aimport java.io.StringReader;%0Aimport java.io.StringWriter;%0A
 import j
@@ -1000,16 +1000,115 @@
 hmark %7B%0A
+  private static final int INPUTS_COUNT = 0x1000;%0A  private static final int INPUTS_MASK = 0xFFF;%0A%0A
   enum E
@@ -1553,17 +1553,25 @@
 yte%5B%5D%5B%5D 
-i
+encodingI
 nputs = 
@@ -1583,16 +1583,90 @@
 yte%5B
-0x1000%5D%5B
+INPUTS_COUNT%5D%5B%5D;%0A  private final String%5B%5D decodingInputs = new String%5BINPUTS_COUNT
 %5D;%0A%0A
@@ -1764,17 +1764,25 @@
  0; i %3C 
-i
+encodingI
 nputs.le
@@ -1800,17 +1800,25 @@
 %7B%0A      
-i
+encodingI
 nputs%5Bi%5D
@@ -1853,17 +1853,96 @@
 xtBytes(
-i
+encodingInputs%5Bi%5D);%0A      decodingInputs%5Bi%5D = encoding.encoding.encode(encodingI
 nputs%5Bi%5D
@@ -2120,27 +2120,1106 @@
 ode(
-inputs%5Bi & 0xFFF%5D))
+encodingInputs%5Bi & INPUTS_MASK%5D));%0A    %7D%0A    return tmp;%0A  %7D%0A%0A  @Benchmark public int decode(int reps) %7B%0A    int tmp = 0;%0A    for (int i = 0; i %3C reps; i++) %7B%0A      tmp += System.identityHashCode(encoding.encoding.decode(decodingInputs%5Bi & INPUTS_MASK%5D));%0A    %7D%0A    return tmp;%0A  %7D%0A%0A  @Benchmark public int encodingStream(int reps) throws IOException %7B%0A    int tmp = 0;%0A    for (int i = 0; i %3C reps; i++) %7B%0A      StringWriter target = new StringWriter(2 * n);%0A      OutputStream encodingStream = encoding.encoding.encodingStream(target);%0A      encodingStream.write(encodingInputs%5Bi & INPUTS_MASK%5D);%0A      encodingStream.close();%0A      tmp += target.getBuffer().length();%0A    %7D%0A    return tmp;%0A  %7D%0A%0A  @Benchmark public int decodingStream(int reps) throws IOException %7B%0A    int tmp = 0;%0A    byte%5B%5D target = new byte%5Bn%5D;%0A    for (int i = 0; i %3C reps; i++) %7B%0A      StringReader source = new StringReader(decodingInputs%5Bi & INPUTS_MASK%5D);%0A      InputStream decodingStream = encoding.encoding.decodingStream(source);%0A      decodingStream.read(target);%0A      decodingStream.close();%0A      tmp += target%5B0%5D
 ;%0A  

Add BaseEncoding benchmarks for decode and stream methods ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=84860248