View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.crypto;
19  
20  import java.util.concurrent.TimeUnit;
21  
22  import org.apache.commons.crypto.cipher.CryptoCipherFactory;
23  import org.apache.commons.crypto.random.CryptoRandomFactory;
24  import org.openjdk.jmh.annotations.Benchmark;
25  import org.openjdk.jmh.annotations.BenchmarkMode;
26  import org.openjdk.jmh.annotations.Fork;
27  import org.openjdk.jmh.annotations.Measurement;
28  import org.openjdk.jmh.annotations.Mode;
29  import org.openjdk.jmh.annotations.OutputTimeUnit;
30  import org.openjdk.jmh.annotations.Threads;
31  import org.openjdk.jmh.annotations.Warmup;
32  
33  /**
34   * Basic Benchmark to compare creation and runtimes for the different implementations.
35   * Needs work to improve how well the tests mirror real-world use.
36   */
37  @BenchmarkMode(Mode.AverageTime)
38  @Fork(value = 1, jvmArgs = "-server")
39  @Threads(1)
40  @Warmup(iterations = 10)
41  @Measurement(iterations = 20)
42  @OutputTimeUnit(TimeUnit.MILLISECONDS)
43  public class CryptoBenchmark extends AbstractBenchmark {
44  
45      private static final String RANDOM_JAVA        = CryptoRandomFactory.RandomProvider.JAVA.getClassName();
46      private static final String RANDOM_OS          = CryptoRandomFactory.RandomProvider.OS.getClassName();
47      private static final String RANDOM_OPENSSL     = CryptoRandomFactory.RandomProvider.OPENSSL.getClassName();
48  
49      private static final String CIPHER_OPENSSL     = CryptoCipherFactory.CipherProvider.OPENSSL.getClassName();
50      private static final String CIPHER_JCE         = CryptoCipherFactory.CipherProvider.JCE.getClassName();
51  
52      @Benchmark
53      public void RandomCreateOS() throws Exception {
54          getRandom(RANDOM_OS);
55      }
56  
57      @Benchmark
58      public void RandomCreateJava() throws Exception {
59          getRandom(RANDOM_JAVA);
60      }
61  
62      @Benchmark
63      public void RandomCreateOpenssl() throws Exception {
64          getRandom(RANDOM_OPENSSL);
65      }
66  
67      @Benchmark
68      public void RandomTestOS() throws Exception {
69          random(RANDOM_OS);
70      }
71  
72      @Benchmark
73      public void RandomTestJava() throws Exception {
74          random(RANDOM_JAVA);
75      }
76  
77      @Benchmark
78      public void RandomTestOpenssl() throws Exception {
79          random(RANDOM_OPENSSL);
80      }
81  
82      
83      @Benchmark
84      public void CipherCreateJce() throws Exception {
85          getCipher(CIPHER_JCE);
86      }
87  
88      @Benchmark
89      public void CipherTestJce() throws Exception {
90          encipher(CIPHER_JCE);
91      }
92  
93      @Benchmark
94      public void CipherCreateOpenssl() throws Exception {
95          getCipher(CIPHER_OPENSSL);
96      }
97  
98      @Benchmark
99      public void CipherTestOpenssl() throws Exception {
100         encipher(CIPHER_OPENSSL);
101     }
102 
103 }