Path: blob/master/test/micro/org/openjdk/bench/java/security/MessageDigests.java
41161 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.security;2324import java.security.DigestException;25import java.security.MessageDigest;26import java.security.NoSuchAlgorithmException;27import java.security.NoSuchProviderException;28import java.util.Random;29import java.util.concurrent.TimeUnit;30import org.openjdk.jmh.annotations.Benchmark;31import org.openjdk.jmh.annotations.Fork;32import org.openjdk.jmh.annotations.Measurement;33import org.openjdk.jmh.annotations.OutputTimeUnit;34import org.openjdk.jmh.annotations.Param;35import org.openjdk.jmh.annotations.Scope;36import org.openjdk.jmh.annotations.Setup;37import org.openjdk.jmh.annotations.State;38import org.openjdk.jmh.annotations.Warmup;3940/**41* Tests various digester algorithms. Sets Fork parameters as these tests are42* rather allocation intensive. Reduced number of forks and iterations as43* benchmarks are stable.44*/45@State(Scope.Thread)46@OutputTimeUnit(TimeUnit.MILLISECONDS)47@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)48@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)49@Fork(jvmArgsAppend = {"-Xms1024m", "-Xmx1024m", "-Xmn768m", "-XX:+UseParallelGC"}, value = 5)50public class MessageDigests {5152@Param({"64", "1024", "16384"})53private int length;5455@Param({"md2", "md5", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512", "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512"})56private String digesterName;5758@Param({"DEFAULT", "SUN"})59protected String provider;6061private byte[] inputBytes;62private MessageDigest digester;6364@Setup65public void setup() throws NoSuchAlgorithmException, DigestException, NoSuchProviderException {66inputBytes = new byte[length];67new Random(1234567890).nextBytes(inputBytes);68if ("DEFAULT".equals(provider)) {69digester = MessageDigest.getInstance(digesterName);70} else {71digester = MessageDigest.getInstance(digesterName, provider);72}73}7475@Benchmark76public byte[] digest() throws DigestException {77return digester.digest(inputBytes);78}7980@Benchmark81public byte[] getAndDigest() throws DigestException, NoSuchAlgorithmException, NoSuchProviderException {82MessageDigest md;83if ("DEFAULT".equals(provider)) {84md = MessageDigest.getInstance(digesterName);85} else {86md = MessageDigest.getInstance(digesterName, provider);87}88return md.digest(inputBytes);89}90}919293