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

Java example source code file (HashCodeBenchmark.java)

This example Java source code file (HashCodeBenchmark.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

anding_booleans, arrays_equals, beforeexperiment, equalsimplementation, hashcodebenchmark, last_byte, message_digest_is_equal, not_at_all, one_percent_in, override, param, random, security, util, wheretodiffer, xoring_to_int

The HashCodeBenchmark.java Java example source code

/*
 * Copyright (C) 2015 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.hash;

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

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Random;

/**
 * Benchmarks for comparing the various {@link HashCode#equals} methods.
 *
 * <p>Parameters for the benchmark are:
 * <ul>
 * <li>size: the length of the byte array to hash
 * <li>whereToDiffer: where in the array the bytes should differ
 * <li>equalsImpl: which implementation of array equality to use
 * </ul>
 *
 * <p>Important note: the primary goal of this benchmark is to ensure that varying {@code
 * whereToDiffer} produces no observable change in performance. We want to make sure that the
 * array equals implementation is *not* short-circuiting to prevent timing-based attacks. Being
 * fast is only a secondary goal.
 *
 * @author Kurt Alfred Kluever
 */
public class HashCodeBenchmark {

  // Use a statically configured random instance for all of the benchmarks
  private static final Random random = new Random(42);

  @Param({"1000", "100000"})
  private int size;

  @Param WhereToDiffer whereToDiffer;

  @Param EqualsImplementation equalsImpl;

  private enum WhereToDiffer {
    ONE_PERCENT_IN,
    LAST_BYTE,
    NOT_AT_ALL;
  }

  private enum EqualsImplementation {
    ANDING_BOOLEANS {
      @Override boolean doEquals(byte[] a, byte[] b) {
        if (a.length != b.length) {
          return false;
        }
        boolean areEqual = true;
        for (int i = 0; i < a.length; i++) {
          areEqual &= (a[i] == b[i]);
        }
        return areEqual;
      }
    },
    XORING_TO_BYTE {
      @Override boolean doEquals(byte[] a, byte[] b) {
        if (a.length != b.length) {
          return false;
        }
        byte result = 0;
        for (int i = 0; i < a.length; i++) {
          result = (byte) (result | a[i] ^ b[i]);
        }
        return (result == 0);
      }
    },
    XORING_TO_INT {
      @Override boolean doEquals(byte[] a, byte[] b) {
        if (a.length != b.length) {
          return false;
        }
        int result = 0;
        for (int i = 0; i < a.length; i++) {
          result |= a[i] ^ b[i];
        }
        return (result == 0);
      }
    },
    MESSAGE_DIGEST_IS_EQUAL {
      @Override boolean doEquals(byte[] a, byte[] b) {
        return MessageDigest.isEqual(a, b);
      }
    },
    ARRAYS_EQUALS {
      @Override boolean doEquals(byte[] a, byte[] b) {
        return Arrays.equals(a, b);
      }
    };

    abstract boolean doEquals(byte[] a, byte[] b);
  }

  private byte[] testBytesA;
  private byte[] testBytesB;

  @BeforeExperiment void setUp() {
    testBytesA = new byte[size];
    random.nextBytes(testBytesA);
    testBytesB = Arrays.copyOf(testBytesA, size);
    int indexToDifferAt = -1;
    switch (whereToDiffer) {
      case ONE_PERCENT_IN:
        indexToDifferAt = (int) (size * 0.01);
        break;
      case LAST_BYTE:
        indexToDifferAt = size - 1;
        break;
      case NOT_AT_ALL:
    }
    if (indexToDifferAt != -1) {
      testBytesA[indexToDifferAt] = (byte) (testBytesB[indexToDifferAt] - 1);
    }
  }

  @Benchmark boolean hashFunction(int reps) {
    boolean result = true;
    for (int i = 0; i < reps; i++) {
      result ^= equalsImpl.doEquals(testBytesA, testBytesB);
    }
    return result;
  }
}

Other Java examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.