1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-19 02:42:05 +01:00

Add RNGPerformanceTest to help diagnose performance bottlenecks

Related to https://github.com/pgpainless/pgpainless/issues/309
This commit is contained in:
Paul Schaub 2022-09-03 14:24:37 +02:00
parent 3cd5a95d89
commit 3a33bb126a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -0,0 +1,88 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// 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()));
}
}