forked from I2P_Developers/i2p.i2p
Use I2PAppContext for obtaining instances of AES and SHA256
This gives a more realistic benchmark of how the rest of the I2P codebase experiences these calls.
This commit is contained in:
@@ -31,7 +31,6 @@ import net.i2p.data.SessionKey;
|
||||
public class AESBench {
|
||||
I2PAppContext ctx = I2PAppContext.getGlobalContext();
|
||||
SessionKey key;
|
||||
CryptixAESEngine aes;
|
||||
byte[] iv = new byte[16];
|
||||
byte[] origPT = new byte[1024];
|
||||
byte[] origCT = new byte[1024];
|
||||
@@ -46,18 +45,17 @@ public class AESBench {
|
||||
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);
|
||||
ctx.aes().encrypt(origPT, 0, origCT, 0, key, iv, len);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void encrypt() {
|
||||
aes.encrypt(origPT, 0, encrypted, 0, key, iv, len);
|
||||
ctx.aes().encrypt(origPT, 0, encrypted, 0, key, iv, len);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void decrypt() {
|
||||
aes.decrypt(origCT, 0, decrypted, 0, key, iv, len);
|
||||
ctx.aes().decrypt(origCT, 0, decrypted, 0, key, iv, len);
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws RunnerException {
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
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;
|
||||
@@ -21,6 +18,9 @@ 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.Hash;
|
||||
|
||||
/**
|
||||
* Test the JVM's implementation for speed
|
||||
*
|
||||
@@ -50,7 +50,7 @@ import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
@Fork(1)
|
||||
@State(Scope.Benchmark)
|
||||
public class SHA256Bench {
|
||||
MessageDigest md;
|
||||
I2PAppContext ctx = I2PAppContext.getGlobalContext();
|
||||
|
||||
@Param({"40", "387", "10240"})
|
||||
public int len;
|
||||
@@ -58,17 +58,14 @@ public class SHA256Bench {
|
||||
byte[] data;
|
||||
|
||||
@Setup
|
||||
public void prepare() throws NoSuchAlgorithmException {
|
||||
md = MessageDigest.getInstance("SHA-256");
|
||||
public void prepare() {
|
||||
data = new byte[len];
|
||||
Random r = new Random();
|
||||
r.nextBytes(data);
|
||||
ctx.random().nextBytes(data);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public byte[] digest() {
|
||||
md.reset();
|
||||
return md.digest(data);
|
||||
public Hash calculateHash() {
|
||||
return ctx.sha().calculateHash(data);
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws RunnerException {
|
||||
|
||||
Reference in New Issue
Block a user