From 3a33bb126a6f619ba662c52cf8b892bafc1c1d1a Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 3 Sep 2022 14:24:37 +0200 Subject: [PATCH] Add RNGPerformanceTest to help diagnose performance bottlenecks Related to https://github.com/pgpainless/pgpainless/issues/309 --- .../investigations/RNGPerformanceTest.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 pgpainless-core/src/test/java/investigations/RNGPerformanceTest.java diff --git a/pgpainless-core/src/test/java/investigations/RNGPerformanceTest.java b/pgpainless-core/src/test/java/investigations/RNGPerformanceTest.java new file mode 100644 index 00000000..e583089d --- /dev/null +++ b/pgpainless-core/src/test/java/investigations/RNGPerformanceTest.java @@ -0,0 +1,88 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package investigations; + +import org.bouncycastle.crypto.digests.SHA1Digest; +import org.bouncycastle.crypto.digests.SHA256Digest; +import org.bouncycastle.crypto.prng.DigestRandomGenerator; +import org.junit.jupiter.api.Test; +import org.junit.platform.commons.logging.Logger; +import org.junit.platform.commons.logging.LoggerFactory; + +import java.security.SecureRandom; +import java.time.Duration; +import java.time.Instant; +import java.util.Random; + +/** + * Evaluate performance of random number generators. + */ +public class RNGPerformanceTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(RNGPerformanceTest.class); + private static final int bytesInMebiByte = 1024 * 1024; + + @Test + public void evaluateRandomPerformance() { + Random random = new Random(); + byte[] bytes = new byte[bytesInMebiByte]; + + Instant start = Instant.now(); + random.nextBytes(bytes); + Instant end = Instant.now(); + + Duration duration = Duration.between(start, end); + LOGGER.info(() -> String.format( + "Random.nextBytes() took %s milliseconds to generate 1 MiB of data", + duration.toMillis())); + } + + @Test + public void evaluateSecureRandomPerformance() { + SecureRandom random = new SecureRandom(); + byte[] bytes = new byte[bytesInMebiByte]; + + Instant start = Instant.now(); + random.nextBytes(bytes); + Instant end = Instant.now(); + + Duration duration = Duration.between(start, end); + LOGGER.info(() -> String.format( + "SecureRandom.nextBytes() took %s milliseconds to generate 1 MiB of data", + duration.toMillis())); + } + + @Test + public void evaluateSHA256BasedDigestRandomGeneratorPerformance() { + SHA256Digest digest = new SHA256Digest(); + DigestRandomGenerator random = new DigestRandomGenerator(digest); + byte[] bytes = new byte[bytesInMebiByte]; + + Instant start = Instant.now(); + random.nextBytes(bytes); + Instant end = Instant.now(); + + Duration duration = Duration.between(start, end); + LOGGER.info(() -> String.format( + "SHA256-based DigestRandomGenerator.nextBytes() took %s milliseconds to generate 1 MiB of data", + duration.toMillis())); + } + + @Test + public void evaluateSHA1BasedDigestRandomGeneratorPerformance() { + SHA1Digest digest = new SHA1Digest(); + DigestRandomGenerator random = new DigestRandomGenerator(digest); + byte[] bytes = new byte[bytesInMebiByte]; + + Instant start = Instant.now(); + random.nextBytes(bytes); + Instant end = Instant.now(); + + Duration duration = Duration.between(start, end); + LOGGER.info(() -> String.format( + "SHA1-based DigestRandomGenerator.nextBytes() took %s milliseconds to generate 1 MiB of data", + duration.toMillis())); + } +}