|
| 1 | +package com.link_intersystems.security; |
| 2 | + |
| 3 | +import org.openjdk.jmh.annotations.*; |
| 4 | + |
| 5 | +import java.security.*; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Some code analysis tools say that making fields final improves performance. |
| 11 | + * This test is made to show the impact of both variants so that you can make |
| 12 | + * a decision. |
| 13 | + */ |
| 14 | +@Fork(warmups = 1, value = 1) |
| 15 | +@Measurement(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) |
| 16 | +@Warmup(iterations = 1) |
| 17 | +@BenchmarkMode({Mode.Throughput}) |
| 18 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 19 | +public class SignBenchmark { |
| 20 | + |
| 21 | + @State(Scope.Benchmark) |
| 22 | + public static class SignState { |
| 23 | + |
| 24 | + protected byte[] content; |
| 25 | + protected PrivateKey privateKeyBytes; |
| 26 | + protected PublicKey publicKeyBytes; |
| 27 | + protected String keyAlgorithm = "RSA"; |
| 28 | + protected String signatureAlgorithm = "SHA256withRSA"; |
| 29 | + |
| 30 | + @Setup(Level.Invocation) |
| 31 | + public void setUp() throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { |
| 32 | + content = new byte[1024 * 1024]; |
| 33 | + new SecureRandom().nextBytes(content); |
| 34 | + |
| 35 | + KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgorithm); |
| 36 | + |
| 37 | + KeyPair keyPair = kpg.generateKeyPair(); |
| 38 | + |
| 39 | + privateKeyBytes = keyPair.getPrivate(); |
| 40 | + publicKeyBytes = keyPair.getPublic(); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + public byte[] sign() throws InvalidKeyException, NoSuchAlgorithmException, SignatureException { |
| 45 | + Signature signature = Signature.getInstance(signatureAlgorithm); |
| 46 | + signature.initSign(privateKeyBytes); |
| 47 | + signature.update(content); |
| 48 | + return signature.sign(); |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + @Benchmark |
| 55 | + public byte[] sign(SignState state) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException { |
| 56 | + return state.sign(); |
| 57 | + } |
| 58 | + |
| 59 | + @State(Scope.Benchmark) |
| 60 | + public static class VerifyState extends SignState { |
| 61 | + |
| 62 | + private byte[] signBytes; |
| 63 | + |
| 64 | + @Setup(Level.Invocation) |
| 65 | + public void setUp() throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { |
| 66 | + super.setUp(); |
| 67 | + |
| 68 | + signBytes = sign(); |
| 69 | + } |
| 70 | + |
| 71 | + public boolean verify() throws InvalidKeyException, NoSuchAlgorithmException, SignatureException { |
| 72 | + Signature signature = Signature.getInstance(signatureAlgorithm); |
| 73 | + signature.initVerify(publicKeyBytes); |
| 74 | + signature.update(content); |
| 75 | + return signature.verify(signBytes); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @Benchmark |
| 81 | + public boolean verify(VerifyState state) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException { |
| 82 | + return state.verify(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
0 commit comments