Home | History | Annotate | Download | only in hash
      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 
     23 import java.security.MessageDigest;
     24 
     25 /**
     26  * Benchmarks for comparing instance creation of {@link MessageDigest}s.
     27  *
     28  * @author Kurt Alfred Kluever
     29  */
     30 public class MessageDigestCreationBenchmark {
     31 
     32   @Param({"MD5", "SHA-1", "SHA-256", "SHA-512"})
     33   private String algorithm;
     34 
     35   private MessageDigest md;
     36 
     37   @BeforeExperiment void setUp() throws Exception {
     38     md = MessageDigest.getInstance(algorithm);
     39   }
     40 
     41   @Benchmark int getInstance(int reps) throws Exception {
     42     int retValue = 0;
     43     for (int i = 0; i < reps; i++) {
     44       retValue ^= MessageDigest.getInstance(algorithm).getDigestLength();
     45     }
     46     return retValue;
     47   }
     48 
     49   @Benchmark int clone(int reps) throws Exception {
     50     int retValue = 0;
     51     for (int i = 0; i < reps; i++) {
     52       retValue ^= ((MessageDigest) md.clone()).getDigestLength();
     53     }
     54     return retValue;
     55   }
     56 }
     57