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
17package com.google.common.hash;
18
19import com.google.caliper.BeforeExperiment;
20import com.google.caliper.Benchmark;
21import com.google.caliper.Param;
22import com.google.common.hash.HashFunction;
23import com.google.common.hash.Hashing;
24
25import java.security.MessageDigest;
26import java.security.NoSuchAlgorithmException;
27import java.util.Random;
28
29/**
30 * Benchmarks for comparing {@link MessageDigest}s and {@link HashFunction}s that wrap
31 * {@link MessageDigest}s.
32 *
33 * <p>Parameters for the benchmark are:
34 * <ul>
35 * <li>size: The length of the byte array to hash.
36 * <li>algorithm: the algorithm to hash with (e.g. MD5, SHA1, etc.).
37 * <li>hashMethod: how to hash the data (using the Hashing API or the MessageDigest API).
38 * </ul>
39 *
40 * @author Kurt Alfred Kluever
41 */
42public class MessageDigestAlgorithmBenchmark {
43  @Param({"10", "1000", "100000", "1000000"}) int size;
44  @Param Algorithm algorithm;
45  @Param HashMethod hashMethod;
46
47  private enum HashMethod {
48    MESSAGE_DIGEST_API() {
49      @Override public byte[] hash(Algorithm algorithm, byte[] input) {
50        MessageDigest md = algorithm.getMessageDigest();
51        md.update(input);
52        return md.digest();
53      }
54    },
55    HASH_FUNCTION_API() {
56      @Override public byte[] hash(Algorithm algorithm, byte[] input) {
57        return algorithm.getHashFunction().hashBytes(input).asBytes();
58      }
59    };
60    public abstract byte[] hash(Algorithm algorithm, byte[] input);
61  }
62
63  private enum Algorithm {
64    MD5("MD5", Hashing.md5()),
65    SHA_1("SHA-1", Hashing.sha1()),
66    SHA_256("SHA-256", Hashing.sha256()),
67    SHA_512("SHA-512", Hashing.sha512());
68
69    private final String algorithmName;
70    private final HashFunction hashFn;
71    Algorithm(String algorithmName, HashFunction hashFn) {
72      this.algorithmName = algorithmName;
73      this.hashFn = hashFn;
74    }
75    public MessageDigest getMessageDigest() {
76      try {
77        return MessageDigest.getInstance(algorithmName);
78      } catch (NoSuchAlgorithmException e) {
79        throw new AssertionError(e);
80      }
81    }
82    public HashFunction getHashFunction() {
83      return hashFn;
84    }
85  }
86
87  // Use a constant seed for all of the benchmarks to ensure apples to apples comparisons.
88  private static final int RANDOM_SEED = new Random().nextInt();
89
90  private byte[] testBytes;
91
92  @BeforeExperiment void setUp() {
93    testBytes = new byte[size];
94    new Random(RANDOM_SEED).nextBytes(testBytes);
95  }
96
97  @Benchmark byte hashing(int reps) {
98    byte result = 0x01;
99    HashMethod hashMethod = this.hashMethod;
100    Algorithm algorithm = this.algorithm;
101    for (int i = 0; i < reps; i++) {
102      result ^= hashMethod.hash(algorithm, testBytes)[0];
103    }
104    return result;
105  }
106}
107