Skip to content

Commit f15e959

Browse files
BAEL-5641 How to Fix Slow SecureRandom Generator (#18897)
* BAEL-5641_add_sample_code * BAEL-5641 add JHM benchmark * BAEL-5641 parametrized version number in POM and formatting fix
1 parent 2d607de commit f15e959

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

core-java-modules/core-java-security-2/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,57 @@
2424
<artifactId>bcprov-jdk18on</artifactId>
2525
<version>${bouncycastle.version}</version>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.openjdk.jmh</groupId>
29+
<artifactId>jmh-core</artifactId>
30+
<version>${jmh-core.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.openjdk.jmh</groupId>
34+
<artifactId>jmh-generator-annprocess</artifactId>
35+
<version>${jmh-generator-annprocess.version}</version>
36+
</dependency>
2737
</dependencies>
2838

2939
<properties>
3040
<bouncycastle.version>1.76</bouncycastle.version>
3141
<commons-codec.version>1.16.0</commons-codec.version>
42+
<jmh-core.version>1.37</jmh-core.version>
43+
<jmh-generator-annprocess.version>1.37</jmh-generator-annprocess.version>
3244
</properties>
3345

46+
<build>
47+
<plugins>
48+
<plugin>
49+
<artifactId>maven-dependency-plugin</artifactId>
50+
<executions>
51+
<execution>
52+
<id>build-classpath</id>
53+
<goals>
54+
<goal>build-classpath</goal>
55+
</goals>
56+
<configuration>
57+
<includeScope>runtime</includeScope>
58+
<outputProperty>depClasspath</outputProperty>
59+
</configuration>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
<plugin>
64+
<groupId>org.codehaus.mojo</groupId>
65+
<artifactId>exec-maven-plugin</artifactId>
66+
<configuration>
67+
<mainClass>BenchmarkRunner</mainClass>
68+
<systemProperties>
69+
<systemProperty>
70+
<key>java.class.path</key>
71+
<value>
72+
${project.build.outputDirectory}${path.separator}${depClasspath}</value>
73+
</systemProperty>
74+
</systemProperties>
75+
</configuration>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
3480
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.securerandomtester;
2+
3+
import java.security.NoSuchAlgorithmException;
4+
import java.security.SecureRandom;
5+
6+
public class SecureRandomAvailableAlgorithms {
7+
8+
static String[] algorithmNames = { "NativePRNG", "NativePRNGBlocking", "NativePRNGNonBlocking", "PKCS11", "SHA1PRNG", "Windows-PRNG" };
9+
10+
public static void main(String[] args) {
11+
for (int i = 0; i < algorithmNames.length; i++) {
12+
String name = algorithmNames[i];
13+
Boolean isAvailable = true;
14+
try {
15+
SecureRandom random = SecureRandom.getInstance(name);
16+
} catch (NoSuchAlgorithmException e) {
17+
isAvailable = false;
18+
}
19+
20+
System.out.println("Algorithm " + name + (isAvailable ? " is" : " isn't") + " available");
21+
}
22+
}
23+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.securerandomtester;
2+
3+
import java.security.NoSuchAlgorithmException;
4+
import java.security.SecureRandom;
5+
import java.util.concurrent.TimeUnit;
6+
import org.openjdk.jmh.annotations.Benchmark;
7+
import org.openjdk.jmh.annotations.BenchmarkMode;
8+
import org.openjdk.jmh.annotations.Level;
9+
import org.openjdk.jmh.annotations.Mode;
10+
import org.openjdk.jmh.annotations.OutputTimeUnit;
11+
import org.openjdk.jmh.annotations.Scope;
12+
import org.openjdk.jmh.annotations.Setup;
13+
import org.openjdk.jmh.annotations.State;
14+
15+
@BenchmarkMode(Mode.AverageTime)
16+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
17+
@State(Scope.Thread)
18+
public class SecureRandomPerformanceTest {
19+
20+
SecureRandom randomNativePRNGBlocking;
21+
SecureRandom randomNativePRNGNonBlocking;
22+
23+
final int NBYTES = 256;
24+
final int NSAMPLES = 20_000;
25+
26+
@Setup(Level.Trial)
27+
public void setup() throws NoSuchAlgorithmException {
28+
randomNativePRNGBlocking = SecureRandom.getInstance("NativePRNGBlocking");
29+
randomNativePRNGNonBlocking = SecureRandom.getInstance("NativePRNGNonBlocking");
30+
}
31+
32+
@Benchmark
33+
public void measureTimePRNGBlocking() {
34+
byte[] randomBytes = new byte[NBYTES];
35+
for (int i = 0; i < NSAMPLES; i++) {
36+
randomNativePRNGBlocking.nextBytes(randomBytes);
37+
}
38+
}
39+
40+
@Benchmark
41+
public void measureTimePRNGNonBlocking() {
42+
byte[] randomBytes = new byte[NBYTES];
43+
for (int i = 0; i < NSAMPLES; i++) {
44+
randomNativePRNGNonBlocking.nextBytes(randomBytes);
45+
}
46+
}
47+
48+
public static void main(String[] args) throws Exception {
49+
org.openjdk.jmh.Main.main(args);
50+
}
51+
}

0 commit comments

Comments
 (0)