Home | History | Annotate | Download | only in conscrypt
      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.ServerSocketBenchmark.Config;
     20 import org.openjdk.jmh.annotations.AuxCounters;
     21 import org.openjdk.jmh.annotations.Benchmark;
     22 import org.openjdk.jmh.annotations.Fork;
     23 import org.openjdk.jmh.annotations.Level;
     24 import org.openjdk.jmh.annotations.Param;
     25 import org.openjdk.jmh.annotations.Scope;
     26 import org.openjdk.jmh.annotations.Setup;
     27 import org.openjdk.jmh.annotations.State;
     28 import org.openjdk.jmh.annotations.TearDown;
     29 import org.openjdk.jmh.annotations.Threads;
     30 
     31 /**
     32  * Benchmark for comparing performance of server socket implementations.
     33  */
     34 @State(Scope.Benchmark)
     35 @Fork(1)
     36 @Threads(1)
     37 public class JmhServerSocketBenchmark {
     38     /**
     39      * Use an AuxCounter so we can measure that bytes per second as they accumulate without
     40      * consuming CPU in the benchmark method.
     41      */
     42     @AuxCounters
     43     @State(Scope.Thread)
     44     public static class BytesPerSecondCounter {
     45         @Setup(Level.Iteration)
     46         public void clean() {
     47             ServerSocketBenchmark.reset();
     48         }
     49 
     50         @SuppressWarnings("unused")
     51         public long bytesPerSecond() {
     52             return ServerSocketBenchmark.bytesPerSecond();
     53         }
     54     }
     55 
     56     private final JmhConfig config = new JmhConfig();
     57 
     58     @Param
     59     public OpenJdkEndpointFactory socketType;
     60 
     61     @Param
     62     public ChannelType channelType;
     63 
     64     @Param({"64", "512", "4096"})
     65     public int messageSize;
     66 
     67     @Param({"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"})
     68     public String cipher;
     69 
     70     private ServerSocketBenchmark benchmark;
     71 
     72     @Setup(Level.Iteration)
     73     public void setup() throws Exception {
     74         benchmark = new ServerSocketBenchmark(config);
     75     }
     76 
     77     @TearDown(Level.Iteration)
     78     public void teardown() throws Exception {
     79         benchmark.close();
     80     }
     81 
     82     @Benchmark
     83     public final void bm(@SuppressWarnings("unused") BytesPerSecondCounter counter)
     84             throws Exception {
     85         benchmark.throughput();
     86     }
     87 
     88     private final class JmhConfig implements Config {
     89         @Override
     90         public EndpointFactory serverFactory() {
     91             return socketType;
     92         }
     93 
     94         @Override
     95         public EndpointFactory clientFactory() {
     96             // Use the same client for all benchmarks since we're benchmarks server perf.
     97             return OpenJdkEndpointFactory.CONSCRYPT_ENGINE;
     98         }
     99 
    100         @Override
    101         public int messageSize() {
    102             return messageSize;
    103         }
    104 
    105         @Override
    106         public String cipher() {
    107             return cipher;
    108         }
    109 
    110         @Override
    111         public ChannelType channelType() {
    112             return channelType;
    113         }
    114     }
    115 }
    116