/test/micro/org/openjdk/bench/javax/crypto/Crypto.java

https://github.com/openjdk/jdk · Java · 112 lines · 57 code · 12 blank · 43 comment · 0 complexity · bac614e94126c57b51358a5c424352a8 MD5 · raw file

  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.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. package org.openjdk.bench.javax.crypto;
  24. import java.security.InvalidKeyException;
  25. import java.security.NoSuchAlgorithmException;
  26. import java.util.Random;
  27. import java.util.concurrent.TimeUnit;
  28. import javax.crypto.BadPaddingException;
  29. import javax.crypto.Cipher;
  30. import javax.crypto.IllegalBlockSizeException;
  31. import javax.crypto.KeyGenerator;
  32. import javax.crypto.NoSuchPaddingException;
  33. import javax.crypto.spec.SecretKeySpec;
  34. import org.openjdk.jmh.annotations.Benchmark;
  35. import org.openjdk.jmh.annotations.Fork;
  36. import org.openjdk.jmh.annotations.Measurement;
  37. import org.openjdk.jmh.annotations.OutputTimeUnit;
  38. import org.openjdk.jmh.annotations.Param;
  39. import org.openjdk.jmh.annotations.Scope;
  40. import org.openjdk.jmh.annotations.Setup;
  41. import org.openjdk.jmh.annotations.State;
  42. import org.openjdk.jmh.annotations.Warmup;
  43. /**
  44. * Tests various encryption algorithms with the JCE framework. Sets Fork
  45. * parameters as these tests are rather allocation intensive. Reduced numbers of
  46. * forks and iteration as 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 Crypto {
  54. @Param({"64", "1024", "16384"})
  55. private int length;
  56. @Param({"AES", "Blowfish", "DES", "DESede"})
  57. private String cipherName;
  58. private SecretKeySpec secretKey;
  59. private Cipher encryptCipher;
  60. private Cipher decryptCipher;
  61. private byte[] plainBytes;
  62. private byte[] encryptedBytes;
  63. @Setup
  64. public void setupSubclass() throws NoSuchAlgorithmException, NoSuchPaddingException,
  65. InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
  66. // Setup ciphers for encrypt/decrypt
  67. byte[] encodedKey = KeyGenerator.getInstance(cipherName).generateKey().getEncoded();
  68. secretKey = new SecretKeySpec(encodedKey, cipherName);
  69. encryptCipher = Cipher.getInstance(cipherName);
  70. encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
  71. decryptCipher = Cipher.getInstance(cipherName);
  72. decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
  73. // Generate data to encrypt/decrypt
  74. plainBytes = new byte[length];
  75. new Random(1234567890).nextBytes(plainBytes);
  76. encryptedBytes = encryptCipher.doFinal(plainBytes);
  77. }
  78. /**
  79. * Encrypt byte array
  80. *
  81. * @return encrypted byte array
  82. * @throws javax.crypto.IllegalBlockSizeException
  83. * @throws javax.crypto.BadPaddingException
  84. */
  85. @Benchmark
  86. public byte[] encrypt() throws IllegalBlockSizeException, BadPaddingException {
  87. return encryptCipher.doFinal(plainBytes);
  88. }
  89. /**
  90. * Decrypt byte array
  91. *
  92. * @return decrypted byte array
  93. * @throws javax.crypto.IllegalBlockSizeException
  94. * @throws javax.crypto.BadPaddingException
  95. */
  96. @Benchmark
  97. public byte[] decrypt() throws IllegalBlockSizeException, BadPaddingException {
  98. return decryptCipher.doFinal(encryptedBytes);
  99. }
  100. }