|
| 1 | +package org.dcache.oncrpc4j.benchmarks; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.math.BigInteger; |
| 5 | +import java.security.GeneralSecurityException; |
| 6 | +import java.security.KeyPair; |
| 7 | +import java.security.KeyPairGenerator; |
| 8 | +import java.security.KeyStore; |
| 9 | +import java.security.SecureRandom; |
| 10 | +import java.security.cert.Certificate; |
| 11 | +import java.util.Date; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | +import javax.net.ssl.KeyManagerFactory; |
| 14 | +import javax.net.ssl.SSLContext; |
| 15 | +import javax.net.ssl.TrustManagerFactory; |
| 16 | +import org.bouncycastle.asn1.x500.X500Name; |
| 17 | +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; |
| 18 | +import org.bouncycastle.cert.X509CertificateHolder; |
| 19 | +import org.bouncycastle.cert.X509v3CertificateBuilder; |
| 20 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; |
| 21 | +import org.bouncycastle.operator.ContentSigner; |
| 22 | +import org.bouncycastle.operator.OperatorCreationException; |
| 23 | +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
| 24 | +import org.dcache.oncrpc4j.rpc.OncRpcProgram; |
| 25 | +import org.dcache.oncrpc4j.rpc.OncRpcSvc; |
| 26 | +import org.dcache.oncrpc4j.rpc.OncRpcSvcBuilder; |
| 27 | +import org.dcache.oncrpc4j.rpc.RpcAuthTypeNone; |
| 28 | +import org.dcache.oncrpc4j.rpc.RpcCall; |
| 29 | +import org.dcache.oncrpc4j.rpc.RpcDispatchable; |
| 30 | +import org.dcache.oncrpc4j.rpc.RpcTransport; |
| 31 | +import org.dcache.oncrpc4j.rpc.net.IpProtocolType; |
| 32 | +import org.dcache.oncrpc4j.xdr.XdrAble; |
| 33 | +import org.dcache.oncrpc4j.xdr.XdrVoid; |
| 34 | +import org.openjdk.jmh.annotations.Benchmark; |
| 35 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 36 | +import org.openjdk.jmh.annotations.Mode; |
| 37 | +import org.openjdk.jmh.annotations.Param; |
| 38 | +import org.openjdk.jmh.annotations.Scope; |
| 39 | +import org.openjdk.jmh.annotations.Setup; |
| 40 | +import org.openjdk.jmh.annotations.State; |
| 41 | +import org.openjdk.jmh.annotations.TearDown; |
| 42 | + |
| 43 | +/** |
| 44 | + * |
| 45 | + */ |
| 46 | +@State(Scope.Thread) |
| 47 | +@BenchmarkMode(Mode.Throughput) |
| 48 | +public class TlsOverhead { |
| 49 | + |
| 50 | + private static final int PROGNUM = 100017; |
| 51 | + private static final int PROGVER = 1; |
| 52 | + |
| 53 | + @Param({"true", "false"}) |
| 54 | + private String withTLS; |
| 55 | + |
| 56 | + private OncRpcSvc svc; |
| 57 | + private OncRpcSvc clnt; |
| 58 | + private RpcCall clntCall; |
| 59 | + private SSLContext sslContext; |
| 60 | + |
| 61 | + private final RpcDispatchable NULL = (RpcCall call) -> call.reply(XdrVoid.XDR_VOID); |
| 62 | + |
| 63 | + @Setup |
| 64 | + public void setUp() throws IOException, Exception { |
| 65 | + |
| 66 | + if (Boolean.getBoolean(withTLS)) { |
| 67 | + sslContext = createSslContext(); |
| 68 | + } |
| 69 | + |
| 70 | + svc = new OncRpcSvcBuilder() |
| 71 | + .withoutAutoPublish() |
| 72 | + .withTCP() |
| 73 | + .withWorkerThreadIoStrategy() |
| 74 | + .withBindAddress("127.0.0.1") |
| 75 | + .withSelectorThreadPoolSize(1) |
| 76 | + .withWorkerThreadPoolSize(1) |
| 77 | + .withRpcService(new OncRpcProgram(PROGNUM, PROGVER), NULL) |
| 78 | + .withSSLContext(sslContext) |
| 79 | + .withServiceName("svc") |
| 80 | + .build(); |
| 81 | + svc.start(); |
| 82 | + |
| 83 | + clnt = new OncRpcSvcBuilder() |
| 84 | + .withoutAutoPublish() |
| 85 | + .withTCP() |
| 86 | + .withClientMode() |
| 87 | + .withWorkerThreadIoStrategy() |
| 88 | + .withSelectorThreadPoolSize(1) |
| 89 | + .withWorkerThreadPoolSize(1) |
| 90 | + .withSSLContext(sslContext) |
| 91 | + .withServiceName("clnt") |
| 92 | + .build(); |
| 93 | + clnt.start(); |
| 94 | + |
| 95 | + RpcTransport t = clnt.connect(svc.getInetSocketAddress(IpProtocolType.TCP)); |
| 96 | + clntCall = new RpcCall(PROGNUM, PROGVER, new RpcAuthTypeNone(), t); |
| 97 | + } |
| 98 | + |
| 99 | + @TearDown |
| 100 | + public void shutdown() throws IOException { |
| 101 | + clnt.stop(); |
| 102 | + svc.stop(); |
| 103 | + } |
| 104 | + |
| 105 | + @Benchmark |
| 106 | + public XdrAble callOpNull() throws IOException { |
| 107 | + |
| 108 | + XdrVoid reply = new XdrVoid(); |
| 109 | + clntCall.call(0, XdrVoid.XDR_VOID, reply); |
| 110 | + return reply; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + public static SSLContext createSslContext() throws Exception { |
| 115 | + |
| 116 | + char[] password = "password".toCharArray(); |
| 117 | + |
| 118 | + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); |
| 119 | + keyPairGenerator.initialize(2048, new SecureRandom()); |
| 120 | + KeyPair keyPair = keyPairGenerator.generateKeyPair(); |
| 121 | + |
| 122 | + Certificate certificate = generateSelfSignedCert(keyPair); |
| 123 | + Certificate[] certificateChain = {certificate}; |
| 124 | + |
| 125 | + // create emtpy keystore and put certificates into it |
| 126 | + KeyStore keyStore = createEmptyKeystore(); |
| 127 | + keyStore.setKeyEntry("private", keyPair.getPrivate(), password, certificateChain); |
| 128 | + keyStore.setCertificateEntry("cert", certificate); |
| 129 | + |
| 130 | + KeyManagerFactory keyManagerFactory |
| 131 | + = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 132 | + keyManagerFactory.init(keyStore, password); |
| 133 | + |
| 134 | + TrustManagerFactory trustManagerFactory |
| 135 | + = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 136 | + trustManagerFactory.init(keyStore); |
| 137 | + |
| 138 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 139 | + sslContext.init(keyManagerFactory.getKeyManagers(), |
| 140 | + trustManagerFactory.getTrustManagers(), |
| 141 | + new SecureRandom()); |
| 142 | + |
| 143 | + return sslContext; |
| 144 | + } |
| 145 | + |
| 146 | + private static Certificate generateSelfSignedCert(KeyPair keyPair) throws GeneralSecurityException, OperatorCreationException { |
| 147 | + |
| 148 | + long notBefore = System.currentTimeMillis(); |
| 149 | + long notAfter = notBefore + TimeUnit.DAYS.toMillis(1); |
| 150 | + |
| 151 | + X500Name subjectDN = new X500Name("CN=localhost, O=dCache.org"); |
| 152 | + X500Name issuerDN = subjectDN; |
| 153 | + |
| 154 | + SubjectPublicKeyInfo subjectPublicKeyInfo |
| 155 | + = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()); |
| 156 | + |
| 157 | + X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder(issuerDN, |
| 158 | + BigInteger.ONE, |
| 159 | + new Date(notBefore), |
| 160 | + new Date(notAfter), subjectDN, |
| 161 | + subjectPublicKeyInfo); |
| 162 | + |
| 163 | + String signatureAlgorithm = "SHA256WithRSA"; |
| 164 | + |
| 165 | + // sign with own key |
| 166 | + ContentSigner contentSigner = new JcaContentSignerBuilder(signatureAlgorithm) |
| 167 | + .build(keyPair.getPrivate()); |
| 168 | + |
| 169 | + X509CertificateHolder certificateHolder = certificateBuilder.build(contentSigner); |
| 170 | + return new JcaX509CertificateConverter().getCertificate(certificateHolder); |
| 171 | + } |
| 172 | + |
| 173 | + private static KeyStore createEmptyKeystore() throws GeneralSecurityException { |
| 174 | + try { |
| 175 | + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 176 | + keyStore.load(null, null); |
| 177 | + return keyStore; |
| 178 | + } catch (IOException e) { |
| 179 | + throw new AssertionError(e); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | +} |
0 commit comments