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 /*
     18  * Copyright 2017 The Netty Project
     19  *
     20  * The Netty Project licenses this file to you under the Apache License,
     21  * version 2.0 (the "License"); you may not use this file except in compliance
     22  * with the License. You may obtain a copy of the License at:
     23  *
     24  *   http://www.apache.org/licenses/LICENSE-2.0
     25  *
     26  * Unless required by applicable law or agreed to in writing, software
     27  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     28  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     29  * License for the specific language governing permissions and limitations
     30  * under the License.
     31  */
     32 
     33 package org.conscrypt;
     34 
     35 import static org.conscrypt.TestUtils.doEngineHandshake;
     36 
     37 import java.nio.ByteBuffer;
     38 import javax.net.ssl.SSLEngine;
     39 import javax.net.ssl.SSLException;
     40 
     41 /**
     42  * Benchmark comparing handshake performance of various engine implementations to conscrypt.
     43  */
     44 public final class EngineHandshakeBenchmark {
     45     /**
     46      * Provider for the benchmark configuration
     47      */
     48     interface Config {
     49         BufferType bufferType();
     50         EngineFactory engineFactory();
     51         String cipher();
     52         boolean useAlpn();
     53     }
     54 
     55     private final EngineFactory engineFactory;
     56     private final String cipher;
     57     private final boolean useAlpn;
     58 
     59     private final ByteBuffer clientApplicationBuffer;
     60     private final ByteBuffer clientPacketBuffer;
     61     private final ByteBuffer serverApplicationBuffer;
     62     private final ByteBuffer serverPacketBuffer;
     63 
     64     EngineHandshakeBenchmark(Config config) throws Exception {
     65         engineFactory = config.engineFactory();
     66         cipher = config.cipher();
     67         useAlpn = config.useAlpn();
     68         BufferType bufferType = config.bufferType();
     69 
     70         SSLEngine clientEngine = engineFactory.newClientEngine(cipher, useAlpn);
     71         SSLEngine serverEngine = engineFactory.newServerEngine(cipher, useAlpn);
     72 
     73         // Create the application and packet buffers for both endpoints.
     74         clientApplicationBuffer = bufferType.newApplicationBuffer(clientEngine);
     75         serverApplicationBuffer = bufferType.newApplicationBuffer(serverEngine);
     76         clientPacketBuffer = bufferType.newPacketBuffer(clientEngine);
     77         serverPacketBuffer = bufferType.newPacketBuffer(serverEngine);
     78 
     79         engineFactory.dispose(clientEngine);
     80         engineFactory.dispose(serverEngine);
     81     }
     82 
     83     void handshake() throws SSLException {
     84         SSLEngine client = engineFactory.newClientEngine(cipher, useAlpn);
     85         SSLEngine server = engineFactory.newServerEngine(cipher, useAlpn);
     86         clientApplicationBuffer.clear();
     87         clientPacketBuffer.clear();
     88         serverApplicationBuffer.clear();
     89         serverPacketBuffer.clear();
     90 
     91         doEngineHandshake(client, server, clientApplicationBuffer, clientPacketBuffer,
     92                 serverApplicationBuffer, serverPacketBuffer, true);
     93         engineFactory.dispose(client);
     94         engineFactory.dispose(server);
     95     }
     96 }
     97