Path: blob/master/test/micro/org/openjdk/bench/java/security/GetMessageDigest.java
41161 views
/*1* Copyright (c) 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.BenchmarkMode;32import org.openjdk.jmh.annotations.Fork;33import org.openjdk.jmh.annotations.Measurement;34import org.openjdk.jmh.annotations.Mode;35import org.openjdk.jmh.annotations.OutputTimeUnit;36import org.openjdk.jmh.annotations.Param;37import org.openjdk.jmh.annotations.Scope;38import org.openjdk.jmh.annotations.Setup;39import org.openjdk.jmh.annotations.State;40import org.openjdk.jmh.annotations.Warmup;4142/**43* Micros for speed of looking up and instantiating MessageDigests.44*/45@State(Scope.Thread)46@BenchmarkMode(Mode.AverageTime)47@OutputTimeUnit(TimeUnit.NANOSECONDS)48@Warmup(iterations = 5, time = 1)49@Measurement(iterations = 10, time = 1)50@Fork(value = 3)51public class GetMessageDigest {5253@Param({"md5", "SHA-1", "SHA-256"})54private String digesterName;5556private MessageDigest messageDigest;5758@Setup59public void setupMessageDigestForCloning() throws NoSuchAlgorithmException {60messageDigest = MessageDigest.getInstance(digesterName);61}6263@Benchmark64public MessageDigest getInstance() throws NoSuchAlgorithmException {65return MessageDigest.getInstance(digesterName);66}6768@Benchmark69public MessageDigest cloneInstance() throws NoSuchAlgorithmException, CloneNotSupportedException {70return (MessageDigest)messageDigest.clone();71}7273@Benchmark74public MessageDigest getInstanceWithProvider() throws NoSuchAlgorithmException, NoSuchProviderException {75return MessageDigest.getInstance(digesterName, "SUN");76}77}787980