1 /*
2  * Copyright 2017 The Android Open Source Project
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 org.conscrypt;
18 
19 import org.conscrypt.CipherEncryptBenchmark.Config;
20 import org.openjdk.jmh.annotations.Benchmark;
21 import org.openjdk.jmh.annotations.Fork;
22 import org.openjdk.jmh.annotations.Level;
23 import org.openjdk.jmh.annotations.Param;
24 import org.openjdk.jmh.annotations.Scope;
25 import org.openjdk.jmh.annotations.Setup;
26 import org.openjdk.jmh.annotations.State;
27 import org.openjdk.jmh.annotations.Threads;
28 import org.openjdk.jmh.infra.Blackhole;
29 
30 /**
31  * Benchmark comparing Cipher creation performance.
32  */
33 @State(Scope.Benchmark)
34 @Fork(1)
35 @Threads(1)
36 public class JmhCipherEncryptBenchmark {
37     private final JmhConfig config = new JmhConfig();
38 
39     @Param
40     public Transformation a_tx;
41 
42     @Param
43     public CipherEncryptBenchmark.BufferType b_bufferType;
44 
45     @Param
46     public OpenJdkCipherFactory c_provider;
47 
48     private CipherEncryptBenchmark benchmark;
49 
50     @Setup(Level.Iteration)
setup()51     public void setup() throws Exception {
52         benchmark = new CipherEncryptBenchmark(config);
53     }
54 
55     @Benchmark
encrypt(Blackhole bh)56     public void encrypt(Blackhole bh) throws Exception {
57         bh.consume(benchmark.encrypt());
58     }
59 
60     private final class JmhConfig implements Config {
61         @Override
bufferType()62         public CipherEncryptBenchmark.BufferType bufferType() {
63             return b_bufferType;
64         }
65 
66         @Override
cipherFactory()67         public CipherFactory cipherFactory() {
68             return c_provider;
69         }
70 
71         @Override
transformation()72         public Transformation transformation() {
73             return a_tx;
74         }
75     }
76 }
77