Replace printKeyGenResults with more generic printBenchmarkResults
parent
fbc28b398d
commit
b24b4779d3
|
@ -61,7 +61,7 @@ public class CryptoBenchmark {
|
||||||
private static class TimingResult {
|
private static class TimingResult {
|
||||||
int operations;
|
int operations;
|
||||||
double elapsedTime;
|
double elapsedTime;
|
||||||
|
|
||||||
TimingResult(int operations, double elapsedTime) {
|
TimingResult(int operations, double elapsedTime) {
|
||||||
this.operations = operations;
|
this.operations = operations;
|
||||||
this.elapsedTime = elapsedTime;
|
this.elapsedTime = elapsedTime;
|
||||||
|
@ -73,13 +73,13 @@ public class CryptoBenchmark {
|
||||||
int ops = 0;
|
int ops = 0;
|
||||||
long startTime = System.nanoTime();
|
long startTime = System.nanoTime();
|
||||||
double elapsedTime = 0;
|
double elapsedTime = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
operation.run();
|
operation.run();
|
||||||
ops++;
|
ops++;
|
||||||
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
|
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
|
||||||
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
|
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
|
||||||
|
|
||||||
return new TimingResult(ops, elapsedTime);
|
return new TimingResult(ops, elapsedTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -527,7 +527,6 @@ public class CryptoBenchmark {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* This method has been replaced by printBenchmarkResults */
|
|
||||||
|
|
||||||
/* Run RSA benchmarks for specified provider and key size */
|
/* Run RSA benchmarks for specified provider and key size */
|
||||||
private static void runRSABenchmark(String providerName, int keySize) throws Exception {
|
private static void runRSABenchmark(String providerName, int keySize) throws Exception {
|
||||||
|
@ -608,13 +607,12 @@ public class CryptoBenchmark {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ECC keygen benchmark */
|
|
||||||
/* ECC keygen benchmark */
|
/* ECC keygen benchmark */
|
||||||
private static void runECCBenchmark(String providerName, String curveName) throws Exception {
|
private static void runECCBenchmark(String providerName, String curveName) throws Exception {
|
||||||
/* Initialize key generator */
|
/* Initialize key generator */
|
||||||
final String finalProviderName;
|
final String finalProviderName;
|
||||||
final KeyPairGenerator keyGen;
|
final KeyPairGenerator keyGen;
|
||||||
|
|
||||||
if (providerName.equals("SunJCE")) {
|
if (providerName.equals("SunJCE")) {
|
||||||
keyGen = KeyPairGenerator.getInstance("EC", "SunEC");
|
keyGen = KeyPairGenerator.getInstance("EC", "SunEC");
|
||||||
keyGen.initialize(new ECGenParameterSpec(curveName));
|
keyGen.initialize(new ECGenParameterSpec(curveName));
|
||||||
|
@ -774,7 +772,7 @@ public class CryptoBenchmark {
|
||||||
/* Key Agreement benchmark using helper */
|
/* Key Agreement benchmark using helper */
|
||||||
final KeyPair finalKeyPair1 = keyPair1;
|
final KeyPair finalKeyPair1 = keyPair1;
|
||||||
final KeyPair finalKeyPair2 = keyPair2;
|
final KeyPair finalKeyPair2 = keyPair2;
|
||||||
|
|
||||||
TimingResult agreementResult = runBenchmark(() -> {
|
TimingResult agreementResult = runBenchmark(() -> {
|
||||||
try {
|
try {
|
||||||
/* Create a new KeyAgreement instance for each operation */
|
/* Create a new KeyAgreement instance for each operation */
|
||||||
|
@ -1320,12 +1318,11 @@ public class CryptoBenchmark {
|
||||||
int keySize = getHmacKeySize(algorithm) * 8;
|
int keySize = getHmacKeySize(algorithm) * 8;
|
||||||
keyGen.init(keySize);
|
keyGen.init(keySize);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Use default key size
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
KeyGenerator tempKeyGen = KeyGenerator.getInstance(algorithm, providerName);
|
KeyGenerator tempKeyGen = KeyGenerator.getInstance(algorithm, providerName);
|
||||||
tempKeyGen.generateKey(); // Test if key generation works with default settings
|
tempKeyGen.generateKey();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalArgumentException("Unsupported algorithm or unable to determine key size: " + algorithm);
|
throw new IllegalArgumentException("Unsupported algorithm or unable to determine key size: " + algorithm);
|
||||||
}
|
}
|
||||||
|
@ -1384,7 +1381,6 @@ public class CryptoBenchmark {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Run ECDH benchmarks for specified provider and curve */
|
|
||||||
/* Run ECDH benchmarks for specified provider and curve */
|
/* Run ECDH benchmarks for specified provider and curve */
|
||||||
private static void runECDHBenchmark(String providerName, String curveName) throws Exception {
|
private static void runECDHBenchmark(String providerName, String curveName) throws Exception {
|
||||||
/* Variables for benchmark operations */
|
/* Variables for benchmark operations */
|
||||||
|
@ -1423,7 +1419,7 @@ public class CryptoBenchmark {
|
||||||
/* Key Agreement benchmark using helper */
|
/* Key Agreement benchmark using helper */
|
||||||
final KeyPair finalKeyPair1 = keyPair1;
|
final KeyPair finalKeyPair1 = keyPair1;
|
||||||
final KeyPair finalKeyPair2 = keyPair2;
|
final KeyPair finalKeyPair2 = keyPair2;
|
||||||
|
|
||||||
TimingResult agreementResult = runBenchmark(() -> {
|
TimingResult agreementResult = runBenchmark(() -> {
|
||||||
try {
|
try {
|
||||||
/* Create a new KeyAgreement instance for each operation to avoid "Object already has a key" error */
|
/* Create a new KeyAgreement instance for each operation to avoid "Object already has a key" error */
|
||||||
|
@ -1437,7 +1433,7 @@ public class CryptoBenchmark {
|
||||||
});
|
});
|
||||||
|
|
||||||
String agreementOp = String.format("ECDH %s agree", curveName);
|
String agreementOp = String.format("ECDH %s agree", curveName);
|
||||||
printBenchmarkResults(agreementResult.operations, agreementResult.elapsedTime, agreementOp, providerName, "ECDH");
|
printBenchmarkResults(agreementResult.operations, agreementResult.elapsedTime, agreementOp, finalProviderName, "ECDH");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
Loading…
Reference in New Issue