Skip to content

Commit

Permalink
Issue #5053 - adding jmh to test SecureRandom algorithms
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Jul 17, 2020
1 parent 8e7bfa0 commit b0f68a7
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.eclipse.jetty.util.jmh;

import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(Scope.Benchmark)
@Threads(4000)
@Warmup(iterations = 4, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 4, time = 1, timeUnit = TimeUnit.SECONDS)
public class SecureRandomBenchmark
{
@Param({"NativePRNG", "NativePRNGNonBlocking", "SHA1PRNG"})
String algorithm;

SecureRandom secureRandom;

@Setup(Level.Trial)
public void setupTrial() throws Exception
{
secureRandom = SecureRandom.getInstance(algorithm);
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
public void testWebSocketMask() throws Exception
{
byte[] mask = new byte[4];
secureRandom.nextBytes(mask);
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
public void testClientNonce() throws Exception
{
byte[] mask = new byte[8];
secureRandom.nextBytes(mask);
}

public static void main(String[] args) throws RunnerException
{
Options opt = new OptionsBuilder()
.include(SecureRandomBenchmark.class.getSimpleName())
.forks(1)
.build();

new Runner(opt).run();
}
}

2 comments on commit b0f68a7

@joakime
Copy link
Contributor Author

@joakime joakime commented on b0f68a7 Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The results of this version on my machine ...

# JMH version: 1.21
# VM version: JDK 1.8.0_252, OpenJDK 64-Bit Server VM, 25.252-b09
# VM invoker: /home/joakim/java/jvm/jdk8u252-b09/jre/bin/java

4 Threads

Benchmark                                          (algorithm)   Mode  Cnt        Score         Error  Units
SecureRandomBenchmark.testClientNonce               NativePRNG  thrpt    4  1487891.328 ± 1669124.225  ops/s
SecureRandomBenchmark.testClientNonce    NativePRNGNonBlocking  thrpt    4  1030324.984 ±  510927.770  ops/s
SecureRandomBenchmark.testClientNonce                 SHA1PRNG  thrpt    4  2608502.149 ±  501642.860  ops/s
SecureRandomBenchmark.testWebSocketMask             NativePRNG  thrpt    4  1900152.728 ±  597393.360  ops/s
SecureRandomBenchmark.testWebSocketMask  NativePRNGNonBlocking  thrpt    4  1444574.436 ± 2331556.925  ops/s
SecureRandomBenchmark.testWebSocketMask               SHA1PRNG  thrpt    4  3404118.755 ±  167059.633  ops/s

40 Threads

Benchmark                                          (algorithm)   Mode  Cnt        Score        Error  Units
SecureRandomBenchmark.testClientNonce               NativePRNG  thrpt    4  1197862.434 ±  77357.263  ops/s
SecureRandomBenchmark.testClientNonce    NativePRNGNonBlocking  thrpt    4  1180633.199 ± 102016.735  ops/s
SecureRandomBenchmark.testClientNonce                 SHA1PRNG  thrpt    4  2178820.001 ± 646700.854  ops/s
SecureRandomBenchmark.testWebSocketMask             NativePRNG  thrpt    4  1510913.477 ± 127748.076  ops/s
SecureRandomBenchmark.testWebSocketMask  NativePRNGNonBlocking  thrpt    4  1560143.887 ±  87115.486  ops/s
SecureRandomBenchmark.testWebSocketMask               SHA1PRNG  thrpt    4  2874198.053 ± 152444.665  ops/s

400 Threads

Benchmark                                          (algorithm)   Mode  Cnt        Score        Error  Units
SecureRandomBenchmark.testClientNonce               NativePRNG  thrpt    4  1142028.565 ±  48538.208  ops/s
SecureRandomBenchmark.testClientNonce    NativePRNGNonBlocking  thrpt    4  1202012.066 ± 144410.431  ops/s
SecureRandomBenchmark.testClientNonce                 SHA1PRNG  thrpt    4  2091480.838 ± 548846.497  ops/s
SecureRandomBenchmark.testWebSocketMask             NativePRNG  thrpt    4  1486393.675 ±  66817.076  ops/s
SecureRandomBenchmark.testWebSocketMask  NativePRNGNonBlocking  thrpt    4  1642895.997 ± 628121.037  ops/s
SecureRandomBenchmark.testWebSocketMask               SHA1PRNG  thrpt    4  2760503.406 ±  87115.224  ops/s

4000 Threads

Benchmark                                          (algorithm)   Mode  Cnt        Score         Error  Units
SecureRandomBenchmark.testClientNonce               NativePRNG  thrpt    4  1185598.989 ±   95936.551  ops/s
SecureRandomBenchmark.testClientNonce    NativePRNGNonBlocking  thrpt    4  1206202.343 ±  265637.853  ops/s
SecureRandomBenchmark.testClientNonce                 SHA1PRNG  thrpt    4  2233527.360 ± 1130616.495  ops/s
SecureRandomBenchmark.testWebSocketMask             NativePRNG  thrpt    4  1572421.998 ±  560165.654  ops/s
SecureRandomBenchmark.testWebSocketMask  NativePRNGNonBlocking  thrpt    4  1672957.962 ±  536088.948  ops/s
SecureRandomBenchmark.testWebSocketMask               SHA1PRNG  thrpt    4  3088041.628 ±  352118.032  ops/s

@joakime
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for JDK 11 ...

4000 Threads

# JMH version: 1.21
# VM version: JDK 11.0.7, OpenJDK 64-Bit Server VM, 11.0.7+10
# VM invoker: /home/joakim/java/jvm/jdk-11.0.7+10/bin/java

Benchmark                                          (algorithm)   Mode  Cnt        Score        Error  Units
SecureRandomBenchmark.testClientNonce               NativePRNG  thrpt    4  1173281.338 ± 321226.045  ops/s
SecureRandomBenchmark.testClientNonce    NativePRNGNonBlocking  thrpt    4  1177515.593 ± 314828.034  ops/s
SecureRandomBenchmark.testClientNonce                 SHA1PRNG  thrpt    4  3087939.483 ± 107121.989  ops/s
SecureRandomBenchmark.testWebSocketMask             NativePRNG  thrpt    4  1409144.796 ± 309665.994  ops/s
SecureRandomBenchmark.testWebSocketMask  NativePRNGNonBlocking  thrpt    4  1425588.865 ± 356602.994  ops/s
SecureRandomBenchmark.testWebSocketMask               SHA1PRNG  thrpt    4  3651617.324 ± 728183.146  ops/s

Please sign in to comment.