Repository: commons-crypto Updated Branches: refs/heads/master c267b98d7 -> 45de8b8d3
Split off JNA benchmark Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/45de8b8d Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/45de8b8d Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/45de8b8d Branch: refs/heads/master Commit: 45de8b8d33d13c351475a8a6e81dd1a0fdb93bef Parents: c267b98 Author: Sebb <[email protected]> Authored: Fri Jul 1 11:08:32 2016 +0100 Committer: Sebb <[email protected]> Committed: Fri Jul 1 11:08:32 2016 +0100 ---------------------------------------------------------------------- .../commons/crypto/AbstractBenchmark.java | 66 ++++++++++++++++ .../apache/commons/crypto/CryptoBenchmark.java | 80 +------------------- .../commons/crypto/jna/CryptoJnaBenchmark.java | 69 +++++++++++++++++ 3 files changed, 136 insertions(+), 79 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/45de8b8d/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java b/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java new file mode 100644 index 0000000..19512ee --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java @@ -0,0 +1,66 @@ +package org.apache.commons.crypto; + +import java.nio.ByteBuffer; +import java.util.Properties; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +import org.apache.commons.crypto.cipher.CryptoCipher; +import org.apache.commons.crypto.cipher.CryptoCipherFactory; +import org.apache.commons.crypto.conf.ConfigurationKeys; +import org.apache.commons.crypto.random.CryptoRandom; +import org.apache.commons.crypto.random.CryptoRandomFactory; +import org.junit.Assert; + +public abstract class AbstractBenchmark { + + private static final byte[] KEY = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; + private static final byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; + private static final SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES"); + private static final IvParameterSpec ivSpec = new IvParameterSpec(IV); + private static final byte[] BUFFER = new byte[1000]; + + public AbstractBenchmark() { + super(); + } + + protected void random(String cipherClass) throws Exception { + CryptoRandom random = getRandom(cipherClass); + random.nextBytes(new byte[1000]); + random.nextBytes(new byte[1000]); + random.close(); + } + + protected void encipher(String cipherClass) throws Exception { + CryptoCipher enCipher = getCipher(cipherClass); + enCipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); + final int bufferSize = 1024; + ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize); + ByteBuffer outBuffer = ByteBuffer.allocateDirect(bufferSize); + inBuffer.put(BUFFER); + inBuffer.flip(); + enCipher.doFinal(inBuffer, outBuffer); + enCipher.close(); + } + + protected CryptoRandom getRandom(String className) throws Exception { + Properties props = new Properties(); + props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, className); + final CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(props); + Assert.assertEquals(className, cryptoRandom.getClass().getCanonicalName()); + return cryptoRandom; + } + + protected CryptoCipher getCipher(String className) throws Exception { + Properties properties = new Properties(); + properties.setProperty(ConfigurationKeys.CIPHER_CLASSES_KEY, className); + CryptoCipher cipher = CryptoCipherFactory.getCryptoCipher("AES/CBC/PKCS5Padding", properties); + Assert.assertEquals(className, cipher.getClass().getCanonicalName()); + return cipher; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/45de8b8d/src/test/java/org/apache/commons/crypto/CryptoBenchmark.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/CryptoBenchmark.java b/src/test/java/org/apache/commons/crypto/CryptoBenchmark.java index 0b2d639..3ec4e9d 100644 --- a/src/test/java/org/apache/commons/crypto/CryptoBenchmark.java +++ b/src/test/java/org/apache/commons/crypto/CryptoBenchmark.java @@ -17,20 +17,10 @@ package org.apache.commons.crypto; -import java.nio.ByteBuffer; -import java.util.Properties; import java.util.concurrent.TimeUnit; -import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; - -import org.apache.commons.crypto.cipher.CryptoCipher; import org.apache.commons.crypto.cipher.CryptoCipherFactory; -import org.apache.commons.crypto.conf.ConfigurationKeys; -import org.apache.commons.crypto.random.CryptoRandom; import org.apache.commons.crypto.random.CryptoRandomFactory; -import org.junit.Assert; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; @@ -50,27 +40,15 @@ import org.openjdk.jmh.annotations.Warmup; @Warmup(iterations = 10) @Measurement(iterations = 20) @OutputTimeUnit(TimeUnit.MILLISECONDS) -public class CryptoBenchmark { +public class CryptoBenchmark extends AbstractBenchmark { - // TODO replace these with the appropriate public fields/methods when these have been set up - private static final String RANDOM_OPENSSL_JNA = "org.apache.commons.crypto.jna.OpenSslJnaCryptoRandom"; private static final String RANDOM_JAVA = CryptoRandomFactory.RandomProvider.JAVA.getClassName(); private static final String RANDOM_OS = CryptoRandomFactory.RandomProvider.OS.getClassName(); private static final String RANDOM_OPENSSL = CryptoRandomFactory.RandomProvider.OPENSSL.getClassName(); - private static final String CIPHER_OPENSSL_JNA = "org.apache.commons.crypto.jna.OpenSslJnaCipher"; private static final String CIPHER_OPENSSL = CryptoCipherFactory.CipherProvider.OPENSSL.getClassName(); private static final String CIPHER_JCE = CryptoCipherFactory.CipherProvider.JCE.getClassName(); - private static final byte[] KEY = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; - private static final byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - private static final SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES"); - private static final IvParameterSpec ivSpec = new IvParameterSpec(IV); - - private static final byte[] BUFFER = new byte[1000]; - @Benchmark public void RandomCreateOS() throws Exception { getRandom(RANDOM_OS); @@ -87,11 +65,6 @@ public class CryptoBenchmark { } @Benchmark - public void RandomTestOpensslJNA() throws Exception { - random(RANDOM_OPENSSL_JNA); - } - - @Benchmark public void RandomTestOS() throws Exception { random(RANDOM_OS); } @@ -106,12 +79,6 @@ public class CryptoBenchmark { random(RANDOM_OPENSSL); } - @Benchmark - public void RandomCreateOpensslJNA() throws Exception { - getRandom(RANDOM_OPENSSL_JNA); - } - - @Benchmark public void CipherCreateJce() throws Exception { @@ -133,49 +100,4 @@ public class CryptoBenchmark { encipher(CIPHER_OPENSSL); } - @Benchmark - public void CipherCreateOpensslJna() throws Exception { - getCipher(CIPHER_OPENSSL_JNA); - } - - @Benchmark - public void CipherTestOpensslJna() throws Exception { - encipher(CIPHER_OPENSSL_JNA); - } - - private void random(String cipherClass) throws Exception { - CryptoRandom random = getRandom(cipherClass); - random.nextBytes(new byte[1000]); - random.nextBytes(new byte[1000]); - random.close(); - } - - private void encipher(String cipherClass) throws Exception { - CryptoCipher enCipher = getCipher(cipherClass); - enCipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); - final int bufferSize = 1024; - ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize); - ByteBuffer outBuffer = ByteBuffer.allocateDirect(bufferSize); - inBuffer.put(BUFFER); - inBuffer.flip(); - enCipher.doFinal(inBuffer, outBuffer); - enCipher.close(); - } - - private CryptoRandom getRandom(String className) throws Exception { - Properties props = new Properties(); - props.setProperty(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, className); - final CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(props); - Assert.assertEquals(className, cryptoRandom.getClass().getCanonicalName()); - return cryptoRandom; - } - - private CryptoCipher getCipher(String className) throws Exception { - Properties properties = new Properties(); - properties.setProperty(ConfigurationKeys.CIPHER_CLASSES_KEY, className); - CryptoCipher cipher = CryptoCipherFactory.getCryptoCipher("AES/CBC/PKCS5Padding", properties); - Assert.assertEquals(className, cipher.getClass().getCanonicalName()); - return cipher; - } - } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/45de8b8d/src/test/java/org/apache/commons/crypto/jna/CryptoJnaBenchmark.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/jna/CryptoJnaBenchmark.java b/src/test/java/org/apache/commons/crypto/jna/CryptoJnaBenchmark.java new file mode 100644 index 0000000..a79227a --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/jna/CryptoJnaBenchmark.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.crypto.jna; + +import java.util.concurrent.TimeUnit; + +import org.apache.commons.crypto.AbstractBenchmark; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.annotations.Warmup; + +/** + * Basic Benchmark to compare creation and runtimes for the different implementations. + * Needs work to improve how well the tests mirror real-world use. + */ +@BenchmarkMode(Mode.AverageTime) +@Fork(value = 1, jvmArgs = "-server") +@Threads(1) +@Warmup(iterations = 10) +@Measurement(iterations = 20) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +public class CryptoJnaBenchmark extends AbstractBenchmark { + + private static final String RANDOM_OPENSSL_JNA = OpenSslJna.getRandomClass().getName(); + + private static final String CIPHER_OPENSSL_JNA = OpenSslJna.getCipherClass().getName(); + + + @Benchmark + public void RandomTestOpensslJNA() throws Exception { + random(RANDOM_OPENSSL_JNA); + } + + @Benchmark + public void RandomCreateOpensslJNA() throws Exception { + getRandom(RANDOM_OPENSSL_JNA); + } + + @Benchmark + public void CipherCreateOpensslJna() throws Exception { + getCipher(CIPHER_OPENSSL_JNA); + } + + @Benchmark + public void CipherTestOpensslJna() throws Exception { + encipher(CIPHER_OPENSSL_JNA); + } + +}
