1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.hash; 18 19 import com.google.caliper.BeforeExperiment; 20 import com.google.caliper.Benchmark; 21 import com.google.caliper.Param; 22 import java.security.MessageDigest; 23 import java.security.NoSuchAlgorithmException; 24 import java.util.Random; 25 26 /** 27 * Benchmarks for comparing {@link MessageDigest}s and {@link com.google.common.hash.HashFunction}s 28 * that wrap {@link MessageDigest}s. 29 * 30 * <p>Parameters for the benchmark are: 31 * 32 * <ul> 33 * <li>size: The length of the byte array to hash. 34 * <li>algorithm: the algorithm to hash with (e.g. MD5, SHA1, etc.). 35 * <li>hashMethod: how to hash the data (using the Hashing API or the MessageDigest API). 36 * </ul> 37 * 38 * @author Kurt Alfred Kluever 39 */ 40 public class MessageDigestAlgorithmBenchmark { 41 @Param({"10", "1000", "100000", "1000000"}) 42 int size; 43 44 @Param Algorithm algorithm; 45 @Param HashMethod hashMethod; 46 47 private enum HashMethod { MESSAGE_DIGEST_API()48 MESSAGE_DIGEST_API() { 49 @Override 50 public byte[] hash(Algorithm algorithm, byte[] input) { 51 MessageDigest md = algorithm.getMessageDigest(); 52 md.update(input); 53 return md.digest(); 54 } 55 }, HASH_FUNCTION_DIRECT()56 HASH_FUNCTION_DIRECT() { 57 @Override 58 public byte[] hash(Algorithm algorithm, byte[] input) { 59 return algorithm.getHashFunction().hashBytes(input).asBytes(); 60 } 61 }, HASH_FUNCTION_VIA_HASHER()62 HASH_FUNCTION_VIA_HASHER() { 63 @Override 64 public byte[] hash(Algorithm algorithm, byte[] input) { 65 return algorithm.getHashFunction().newHasher().putBytes(input).hash().asBytes(); 66 } 67 }; 68 ; 69 hash(Algorithm algorithm, byte[] input)70 public abstract byte[] hash(Algorithm algorithm, byte[] input); 71 } 72 73 private enum Algorithm { 74 MD5("MD5", Hashing.md5()), 75 SHA_1("SHA-1", Hashing.sha1()), 76 SHA_256("SHA-256", Hashing.sha256()), 77 SHA_384("SHA-384", Hashing.sha384()), 78 SHA_512("SHA-512", Hashing.sha512()); 79 80 private final String algorithmName; 81 private final HashFunction hashFn; 82 Algorithm(String algorithmName, HashFunction hashFn)83 Algorithm(String algorithmName, HashFunction hashFn) { 84 this.algorithmName = algorithmName; 85 this.hashFn = hashFn; 86 } 87 getMessageDigest()88 public MessageDigest getMessageDigest() { 89 try { 90 return MessageDigest.getInstance(algorithmName); 91 } catch (NoSuchAlgorithmException e) { 92 throw new AssertionError(e); 93 } 94 } 95 getHashFunction()96 public HashFunction getHashFunction() { 97 return hashFn; 98 } 99 } 100 101 // Use a constant seed for all of the benchmarks to ensure apples to apples comparisons. 102 private static final int RANDOM_SEED = new Random().nextInt(); 103 104 private byte[] testBytes; 105 106 @BeforeExperiment setUp()107 void setUp() { 108 testBytes = new byte[size]; 109 new Random(RANDOM_SEED).nextBytes(testBytes); 110 } 111 112 @Benchmark hashing(int reps)113 byte hashing(int reps) { 114 byte result = 0x01; 115 HashMethod hashMethod = this.hashMethod; 116 Algorithm algorithm = this.algorithm; 117 for (int i = 0; i < reps; i++) { 118 result ^= hashMethod.hash(algorithm, testBytes)[0]; 119 } 120 return result; 121 } 122 } 123