1 /*
   2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.bench.java.security;
  26 
  27 import java.security.DigestException;
  28 import java.security.MessageDigest;
  29 import java.security.NoSuchAlgorithmException;
  30 import java.security.NoSuchProviderException;
  31 import java.util.Random;
  32 import java.util.concurrent.TimeUnit;
  33 import org.openjdk.jmh.annotations.Benchmark;
  34 import org.openjdk.jmh.annotations.Fork;
  35 import org.openjdk.jmh.annotations.Measurement;
  36 import org.openjdk.jmh.annotations.OutputTimeUnit;
  37 import org.openjdk.jmh.annotations.Param;
  38 import org.openjdk.jmh.annotations.Scope;
  39 import org.openjdk.jmh.annotations.Setup;
  40 import org.openjdk.jmh.annotations.State;
  41 import org.openjdk.jmh.annotations.Warmup;
  42 
  43 /**
  44  * Tests various digester algorithms. Sets Fork parameters as these tests are
  45  * rather allocation intensive. Reduced number of forks and iterations as
  46  * benchmarks are stable.
  47  */
  48 @State(Scope.Thread)
  49 @OutputTimeUnit(TimeUnit.MILLISECONDS)
  50 @Warmup(iterations = 5)
  51 @Measurement(iterations = 10)
  52 @Fork(jvmArgsAppend = {"-Xms1024m", "-Xmx1024m", "-Xmn768m", "-XX:+UseParallelGC"}, value = 5)
  53 public class MessageDigests {
  54 
  55     @Param({"64", "1024", "16384"})
  56     private int length;
  57 
  58     @Param({"md2", "md5", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"})
  59     private String digesterName;
  60 
  61     @Param({"DEFAULT", "SUN"})
  62     protected String provider;
  63 
  64     private byte[] inputBytes;
  65     private MessageDigest digester;
  66 
  67     @Setup
  68     public void setup() throws NoSuchAlgorithmException, DigestException, NoSuchProviderException {
  69         inputBytes = new byte[length];
  70         new Random(1234567890).nextBytes(inputBytes);
  71         if ("DEFAULT".equals(provider)) {
  72             digester = MessageDigest.getInstance(digesterName);
  73         } else {
  74             digester = MessageDigest.getInstance(digesterName, provider);
  75         }
  76     }
  77 
  78     @Benchmark
  79     public byte[] digest() throws DigestException {
  80         return digester.digest(inputBytes);
  81     }
  82 }