From 32968af39c11e7e4e4ba47644548d90a61720af8 Mon Sep 17 00:00:00 2001 From: str4d <str4d@mail.i2p> Date: Tue, 25 Jul 2017 03:43:04 +0000 Subject: [PATCH] Add benchmarks for AES and SHA-256 --- .../src/net/i2p/crypto/AESBenchmarks.java | 70 ++++++++++++++++ .../src/net/i2p/crypto/SHA256Benchmarks.java | 81 +++++++++++++++++++ .../src/net/i2p/crypto/SHA256Generator.java | 59 -------------- 3 files changed, 151 insertions(+), 59 deletions(-) create mode 100644 benchmarks/java/src/net/i2p/crypto/AESBenchmarks.java create mode 100644 benchmarks/java/src/net/i2p/crypto/SHA256Benchmarks.java diff --git a/benchmarks/java/src/net/i2p/crypto/AESBenchmarks.java b/benchmarks/java/src/net/i2p/crypto/AESBenchmarks.java new file mode 100644 index 0000000000..160628fca5 --- /dev/null +++ b/benchmarks/java/src/net/i2p/crypto/AESBenchmarks.java @@ -0,0 +1,70 @@ +package net.i2p.crypto; + +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +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.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; + +import net.i2p.I2PAppContext; +import net.i2p.data.DataHelper; +import net.i2p.data.SessionKey; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +@Fork(1) +@State(Scope.Benchmark) +public class AESBenchmarks { + I2PAppContext ctx = I2PAppContext.getGlobalContext(); + SessionKey key; + CryptixAESEngine aes; + byte[] iv = new byte[16]; + byte[] origPT = new byte[1024]; + byte[] origCT = new byte[1024]; + byte[] encrypted = new byte[1024]; + byte[] decrypted = new byte[1024]; + + @Param({"512", "768", "1024"}) + public int len; + + @Setup + public void prepare() { + key = ctx.keyGenerator().generateSessionKey(); + ctx.random().nextBytes(iv); + ctx.random().nextBytes(origPT); + aes = new CryptixAESEngine(ctx); + aes.encrypt(origPT, 0, origCT, 0, key, iv, len); + } + + @Benchmark + public void encrypt() { + aes.encrypt(origPT, 0, encrypted, 0, key, iv, len); + } + + @Benchmark + public void decrypt() { + aes.decrypt(origCT, 0, decrypted, 0, key, iv, len); + } + + public static void main(String args[]) throws RunnerException { + Options opt = new OptionsBuilder() + .include(AESBenchmarks.class.getSimpleName()) + .build(); + + new Runner(opt).run(); + } +} diff --git a/benchmarks/java/src/net/i2p/crypto/SHA256Benchmarks.java b/benchmarks/java/src/net/i2p/crypto/SHA256Benchmarks.java new file mode 100644 index 0000000000..6bc2084fe1 --- /dev/null +++ b/benchmarks/java/src/net/i2p/crypto/SHA256Benchmarks.java @@ -0,0 +1,81 @@ +package net.i2p.crypto; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +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.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; + +/** + * Test the GNU and the JVM's implementations for speed + * + * Old results (2011-05): + * <ul> + * <li>eeepc Atom + * <li>100,000 runs + * <li>MessageDigest.getInstance time was included + * <li>One println included + * <li>Also shows GNU impl time (removed in 0.9.28) + * </ul><pre> + * JVM strlen GNU ms JVM ms + * Oracle 387 3861 3565 + * Oracle 40 825 635 + * Harmony 387 8082 5158 + * Harmony 40 4137 1753 + * JamVM 387 36301 34100 + * JamVM 40 7022 6016 + * gij 387 125833 4342 + * gij 40 22417 988 + * </pre> + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +@Fork(1) +@State(Scope.Benchmark) +public class SHA256Benchmarks { + MessageDigest md; + + @Param({"40", "387", "10240"}) + public int len; + + byte[] data; + + @Setup + public void prepare() throws NoSuchAlgorithmException { + md = MessageDigest.getInstance("SHA-256"); + data = new byte[len]; + Random r = new Random(); + r.nextBytes(data); + } + + @Benchmark + public byte[] digest() { + md.reset(); + return md.digest(data); + } + + public static void main(String args[]) throws RunnerException { + Options opt = new OptionsBuilder() + .include(SHA256Benchmarks.class.getSimpleName()) + .build(); + + new Runner(opt).run(); + } +} diff --git a/core/java/src/net/i2p/crypto/SHA256Generator.java b/core/java/src/net/i2p/crypto/SHA256Generator.java index a03878c2f0..2a96b71817 100644 --- a/core/java/src/net/i2p/crypto/SHA256Generator.java +++ b/core/java/src/net/i2p/crypto/SHA256Generator.java @@ -92,63 +92,4 @@ public final class SHA256Generator { throw new RuntimeException(e); } } - - //private static final int RUNS = 100000; - - /** - * Test the GNU and the JVM's implementations for speed - * - * Results: 2011-05 eeepc Atom - * <pre> - * JVM strlen GNU ms JVM ms - * Oracle 387 3861 3565 - * Oracle 40 825 635 - * Harmony 387 8082 5158 - * Harmony 40 4137 1753 - * JamVM 387 36301 34100 - * JamVM 40 7022 6016 - * gij 387 125833 4342 - * gij 40 22417 988 - * </pre> - */ -/**** - public static void main(String args[]) { - if (args.length <= 0) { - System.err.println("Usage: SHA256Generator string"); - return; - } - - byte[] data = args[0].getBytes(); - Sha256Standalone gnu = new Sha256Standalone(); - long start = System.currentTimeMillis(); - for (int i = 0; i < RUNS; i++) { - gnu.update(data, 0, data.length); - byte[] sha = gnu.digest(); - if (i == 0) - System.out.println("SHA256 [" + args[0] + "] = [" + Base64.encode(sha) + "]"); - gnu.reset(); - } - long time = System.currentTimeMillis() - start; - System.out.println("Time for " + RUNS + " SHA-256 computations:"); - System.out.println("GNU time (ms): " + time); - - start = System.currentTimeMillis(); - MessageDigest md; - try { - md = MessageDigest.getInstance("SHA-256"); - } catch (NoSuchAlgorithmException e) { - System.err.println("Fatal: " + e); - return; - } - for (int i = 0; i < RUNS; i++) { - md.reset(); - byte[] sha = md.digest(data); - if (i == 0) - System.out.println("SHA256 [" + args[0] + "] = [" + Base64.encode(sha) + "]"); - } - time = System.currentTimeMillis() - start; - - System.out.println("JVM time (ms): " + time); - } -****/ } -- GitLab