Path: blob/master/test/micro/org/openjdk/bench/javax/crypto/Crypto.java
41159 views
/*1* Copyright (c) 2014, 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.javax.crypto;2324import java.security.InvalidKeyException;25import java.security.NoSuchAlgorithmException;26import java.util.Random;27import java.util.concurrent.TimeUnit;28import javax.crypto.BadPaddingException;29import javax.crypto.Cipher;30import javax.crypto.IllegalBlockSizeException;31import javax.crypto.KeyGenerator;32import javax.crypto.NoSuchPaddingException;33import javax.crypto.spec.SecretKeySpec;34import org.openjdk.jmh.annotations.Benchmark;35import org.openjdk.jmh.annotations.Fork;36import org.openjdk.jmh.annotations.Measurement;37import org.openjdk.jmh.annotations.OutputTimeUnit;38import org.openjdk.jmh.annotations.Param;39import org.openjdk.jmh.annotations.Scope;40import org.openjdk.jmh.annotations.Setup;41import org.openjdk.jmh.annotations.State;42import org.openjdk.jmh.annotations.Warmup;4344/**45* Tests various encryption algorithms with the JCE framework. Sets Fork46* parameters as these tests are rather allocation intensive. Reduced numbers of47* forks and iteration as benchmarks are stable.48*/49@State(Scope.Thread)50@OutputTimeUnit(TimeUnit.MILLISECONDS)51@Warmup(iterations = 5)52@Measurement(iterations = 10)53@Fork(jvmArgsAppend = {"-Xms1024m", "-Xmx1024m", "-Xmn768m", "-XX:+UseParallelGC"}, value = 5)54public class Crypto {5556@Param({"64", "1024", "16384"})57private int length;5859@Param({"AES", "Blowfish", "DES", "DESede"})60private String cipherName;6162private SecretKeySpec secretKey;63private Cipher encryptCipher;64private Cipher decryptCipher;65private byte[] plainBytes;66private byte[] encryptedBytes;6768@Setup69public void setupSubclass() throws NoSuchAlgorithmException, NoSuchPaddingException,70InvalidKeyException, IllegalBlockSizeException, BadPaddingException {7172// Setup ciphers for encrypt/decrypt73byte[] encodedKey = KeyGenerator.getInstance(cipherName).generateKey().getEncoded();74secretKey = new SecretKeySpec(encodedKey, cipherName);7576encryptCipher = Cipher.getInstance(cipherName);77encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);7879decryptCipher = Cipher.getInstance(cipherName);80decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);8182// Generate data to encrypt/decrypt83plainBytes = new byte[length];84new Random(1234567890).nextBytes(plainBytes);85encryptedBytes = encryptCipher.doFinal(plainBytes);86}8788/**89* Encrypt byte array90*91* @return encrypted byte array92* @throws javax.crypto.IllegalBlockSizeException93* @throws javax.crypto.BadPaddingException94*/95@Benchmark96public byte[] encrypt() throws IllegalBlockSizeException, BadPaddingException {97return encryptCipher.doFinal(plainBytes);98}99100/**101* Decrypt byte array102*103* @return decrypted byte array104* @throws javax.crypto.IllegalBlockSizeException105* @throws javax.crypto.BadPaddingException106*/107@Benchmark108public byte[] decrypt() throws IllegalBlockSizeException, BadPaddingException {109return decryptCipher.doFinal(encryptedBytes);110}111}112113114