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