commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject commons-crypto git commit: Basic Benchmark tests
Date Tue, 28 Jun 2016 17:39:27 GMT
Repository: commons-crypto
Updated Branches:
  refs/heads/master 623b4dd66 -> e5ddc2fed


Basic Benchmark tests

Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/e5ddc2fe
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/e5ddc2fe
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/e5ddc2fe

Branch: refs/heads/master
Commit: e5ddc2fedc547bac763af50d057b793e56e569c3
Parents: 623b4dd
Author: Sebb <sebb@apache.org>
Authored: Tue Jun 28 18:39:23 2016 +0100
Committer: Sebb <sebb@apache.org>
Committed: Tue Jun 28 18:39:23 2016 +0100

----------------------------------------------------------------------
 pom.xml                                         |  81 ++++++++
 .../apache/commons/crypto/CryptoBenchmark.java  | 183 +++++++++++++++++++
 2 files changed, 264 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/e5ddc2fe/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index a549564..4fd6e5f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -308,6 +308,79 @@ The following provides more details on the included cryptographic software:
         </plugins>
       </reporting>
     </profile>
+    <!-- Profile to build and run the benchmarks. Use 'mvn test -Pbenchmark', and add
'-Dbenchmark=foo' to run only the foo benchmark -->
+    <profile>
+      <id>benchmark</id>
+
+      <properties>
+        <skipTests>true</skipTests>
+        <benchmark>org.apache</benchmark>
+        <jmh.version>1.12</jmh.version>
+      </properties>
+
+      <dependencies>
+        <dependency>
+          <groupId>org.openjdk.jmh</groupId>
+          <artifactId>jmh-core</artifactId>
+          <version>${jmh.version}</version>
+          <scope>test</scope>
+        </dependency>
+
+        <dependency>
+          <groupId>org.openjdk.jmh</groupId>
+          <artifactId>jmh-generator-annprocess</artifactId>
+          <version>${jmh.version}</version>
+          <scope>test</scope>
+        </dependency>
+
+      </dependencies>
+
+      <build>
+        <plugins>
+          <!-- Enable the compilation of the benchmarks -->
+          <plugin>
+            <artifactId>maven-compiler-plugin</artifactId>
+            <version>${commons.compiler.version}</version>
+            <configuration combine.self="override">
+              <testIncludes>
+                <testInclude>**/*</testInclude>
+              </testIncludes>
+            </configuration>
+          </plugin>
+
+          <!-- Hook the benchmarks to the test phase -->
+          <plugin>
+            <groupId>org.codehaus.mojo</groupId>
+            <artifactId>exec-maven-plugin</artifactId>
+            <version>1.4.0</version>
+            <executions>
+              <execution>
+                <id>benchmark</id>
+                <phase>test</phase>
+                <goals>
+                  <goal>exec</goal>
+                </goals>
+                <configuration>
+                  <classpathScope>test</classpathScope>
+                  <executable>java</executable>
+                  <arguments>
+                    <argument>-classpath</argument>
+                    <classpath />
+                    <argument>org.openjdk.jmh.Main</argument>
+                    <argument>-rf</argument>
+                    <argument>json</argument>
+                    <argument>-rff</argument>
+                    <argument>target/jmh-result.json</argument>
+                    <argument>${benchmark}</argument>
+                  </arguments>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+
   </profiles>
   <licenses>
     <license>
@@ -419,6 +492,14 @@ The following provides more details on the included cryptographic software:
         </configuration>
       </plugin>
       <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <testExcludes>
+            <testExclude>**/*Benchmark*</testExclude>
+          </testExcludes>
+        </configuration>
+      </plugin>
+      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-scm-publish-plugin</artifactId>
         <version>1.1</version>

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/e5ddc2fe/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
new file mode 100644
index 0000000..b1bd753
--- /dev/null
+++ b/src/test/java/org/apache/commons/crypto/CryptoBenchmark.java
@@ -0,0 +1,183 @@
+/*
+ * 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;
+
+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;
+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 CryptoBenchmark {
+
+    // 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        = "org.apache.commons.crypto.random.JavaCryptoRandom";
+    private static final String RANDOM_OS          = "org.apache.commons.crypto.random.OsCryptoRandom";
+    private static final String RANDOM_OPENSSL     = "org.apache.commons.crypto.random.OpensslCryptoRandom";
+
+    private static final String CIPHER_OPENSSL_JNA = "org.apache.commons.crypto.jna.OpensslJnaCipher";
+    private static final String CIPHER_OPENSSL     = "org.apache.commons.crypto.cipher.OpensslCipher";
+    private static final String CIPHER_JCE         = "org.apache.commons.crypto.cipher.JceCipher";
+
+    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);
+    }
+
+    @Benchmark
+    public void RandomCreateJava() throws Exception {
+        getRandom(RANDOM_JAVA);
+    }
+
+    @Benchmark
+    public void RandomCreateOpenssl() throws Exception {
+        getRandom(RANDOM_OPENSSL);
+    }
+
+    @Benchmark
+    public void RandomTestOpensslJNA() throws Exception {
+        random(RANDOM_OPENSSL_JNA);
+    }
+
+    @Benchmark
+    public void RandomTestOS() throws Exception {
+        random(RANDOM_OS);
+    }
+
+    @Benchmark
+    public void RandomTestJava() throws Exception {
+        random(RANDOM_JAVA);
+    }
+
+    @Benchmark
+    public void RandomTestOpenssl() throws Exception {
+        random(RANDOM_OPENSSL);
+    }
+
+    @Benchmark
+    public void RandomCreateOpensslJNA() throws Exception {
+        getRandom(RANDOM_OPENSSL_JNA);
+    }
+
+
+    
+    @Benchmark
+    public void CipherCreateJce() throws Exception {
+        getCipher(CIPHER_JCE);
+    }
+
+    @Benchmark
+    public void CipherTestJce() throws Exception {
+        encipher(CIPHER_JCE);
+    }
+
+    @Benchmark
+    public void CipherCreateOpenssl() throws Exception {
+        getCipher(CIPHER_OPENSSL);
+    }
+
+    @Benchmark
+    public void CipherTestOpenssl() throws Exception {
+        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);
+        properties.setProperty(ConfigurationKeys.ENABLE_FALLBACK_ON_NATIVE_FAILED_KEY, "false");
+
+        CryptoCipher cipher = CryptoCipherFactory.getInstance("AES/CBC/PKCS5Padding", properties);
+        Assert.assertEquals(className, cipher.getClass().getCanonicalName());
+        return cipher;
+    }
+
+}


Mime
View raw message