alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (BaseEncodingBenchmark.java)

This example Java source code file (BaseEncodingBenchmark.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

baseencodingbenchmark, beforeexperiment, benchmark, encodingoption, inputs_count, inputs_mask, inputstream, ioexception, outputstream, param, random, stringreader, stringwriter, util

The BaseEncodingBenchmark.java Java example source code

/*
 * 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.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Random;

/**
 * Benchmark for {@code BaseEncoding} performance.
 */
public class BaseEncodingBenchmark {
  private static final int INPUTS_COUNT = 0x1000;
  private static final int INPUTS_MASK = 0xFFF;

  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[][] encodingInputs = new byte[INPUTS_COUNT][];
  private final String[] decodingInputs = new String[INPUTS_COUNT];

  @BeforeExperiment
  public void setUp() {
    Random rng = new Random();
    for (int i = 0; i < encodingInputs.length; i++) {
      encodingInputs[i] = new byte[n];
      rng.nextBytes(encodingInputs[i]);
      decodingInputs[i] = encoding.encoding.encode(encodingInputs[i]);
    }
  }

  @Benchmark public int encode(int reps) {
    int tmp = 0;
    for (int i = 0; i < reps; i++) {
      tmp += System.identityHashCode(encoding.encoding.encode(encodingInputs[i & INPUTS_MASK]));
    }
    return tmp;
  }

  @Benchmark public int decode(int reps) {
    int tmp = 0;
    for (int i = 0; i < reps; i++) {
      tmp += System.identityHashCode(encoding.encoding.decode(decodingInputs[i & INPUTS_MASK]));
    }
    return tmp;
  }

  @Benchmark public int encodingStream(int reps) throws IOException {
    int tmp = 0;
    for (int i = 0; i < reps; i++) {
      StringWriter target = new StringWriter(2 * n);
      OutputStream encodingStream = encoding.encoding.encodingStream(target);
      encodingStream.write(encodingInputs[i & INPUTS_MASK]);
      encodingStream.close();
      tmp += target.getBuffer().length();
    }
    return tmp;
  }

  @Benchmark public int decodingStream(int reps) throws IOException {
    int tmp = 0;
    byte[] target = new byte[n];
    for (int i = 0; i < reps; i++) {
      StringReader source = new StringReader(decodingInputs[i & INPUTS_MASK]);
      InputStream decodingStream = encoding.encoding.decodingStream(source);
      decodingStream.read(target);
      decodingStream.close();
      tmp += target[0];
    }
    return tmp;
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java BaseEncodingBenchmark.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.