Home | History | Annotate | Download | only in regression
      1 /*
      2  * Copyright (C) 2012 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package benchmarks.regression;
     18 
     19 import com.google.caliper.Param;
     20 import com.google.caliper.SimpleBenchmark;
     21 import java.security.KeyPair;
     22 import java.security.PrivateKey;
     23 import java.security.PublicKey;
     24 import java.security.KeyPairGenerator;
     25 import java.security.SecureRandom;
     26 import java.util.HashMap;
     27 import java.util.Map;
     28 
     29 public class KeyPairGeneratorBenchmark extends SimpleBenchmark {
     30     @Param private Algorithm algorithm;
     31 
     32     public enum Algorithm {
     33         RSA,
     34         DSA,
     35     };
     36 
     37     @Param private Implementation implementation;
     38 
     39     public enum Implementation { OpenSSL, BouncyCastle };
     40 
     41     private String generatorAlgorithm;
     42     private KeyPairGenerator generator;
     43     private SecureRandom random;
     44 
     45     @Override protected void setUp() throws Exception {
     46         this.generatorAlgorithm = algorithm.toString();
     47 
     48         final String provider;
     49         if (implementation == Implementation.BouncyCastle) {
     50             provider = "BC";
     51         } else {
     52             provider = "AndroidOpenSSL";
     53         }
     54 
     55         this.generator = KeyPairGenerator.getInstance(generatorAlgorithm, provider);
     56         this.random = SecureRandom.getInstance("SHA1PRNG");
     57         this.generator.initialize(1024);
     58     }
     59 
     60     public void time(int reps) throws Exception {
     61         for (int i = 0; i < reps; ++i) {
     62             KeyPair keyPair = generator.generateKeyPair();
     63         }
     64     }
     65 }
     66