Skip to content

Commit

Permalink
Removed re-encryption instead reuses encryptTestData
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Tjaden committed Jan 2, 2025
1 parent ba751c9 commit 562b298
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions examples/provider/CryptoBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ private static void runBenchmark(String algorithm, String mode) throws Exception
/* Using specific type instead of Object */
AlgorithmParameterSpec params;
/* Test data variables */
byte[] encryptTestData;
byte[] decryptTestData;
byte[] encryptedData;
byte[] encrypted;
double encryptDataSizeMiB;
double decryptDataSizeMiB;
byte[] testData;
byte[] encryptedData = null;
double dataSizeMiB;

/* Cipher variables */
Cipher cipher;
Expand Down Expand Up @@ -70,34 +67,30 @@ private static void runBenchmark(String algorithm, String mode) throws Exception
}

/* Generate test data filled with zeros */
encryptTestData = generateTestData(ENCRYPT_SIZE);
decryptTestData = encryptTestData; /* Reuse the same data for decryption test */
testData = generateTestData(ENCRYPT_SIZE);

/* Initialize cipher */
cipher = Cipher.getInstance(algorithm);

/* Warm up phase */
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
cipher.init(Cipher.ENCRYPT_MODE, key, params);
encrypted = cipher.doFinal(encryptTestData);
encryptedData = cipher.doFinal(testData);

cipher.init(Cipher.DECRYPT_MODE, key, params);
cipher.doFinal(encrypted);
cipher.doFinal(encryptedData);
}

/* Benchmark encryption */
startTime = System.nanoTime();
for (int i = 0; i < TEST_ITERATIONS; i++) {
cipher.init(Cipher.ENCRYPT_MODE, key, params);
cipher.doFinal(encryptTestData);
encryptedData = cipher.doFinal(testData); // Save the last encrypted result
}
endTime = System.nanoTime();
encryptTime = (endTime - startTime) / TEST_ITERATIONS;

/* Benchmark decryption */
cipher.init(Cipher.ENCRYPT_MODE, key, params);
encryptedData = cipher.doFinal(decryptTestData);

/* Benchmark decryption using the encrypted data from encryption benchmark */
startTime = System.nanoTime();
for (int i = 0; i < TEST_ITERATIONS; i++) {
cipher.init(Cipher.DECRYPT_MODE, key, params);
Expand All @@ -106,9 +99,8 @@ private static void runBenchmark(String algorithm, String mode) throws Exception
endTime = System.nanoTime();
decryptTime = (endTime - startTime) / TEST_ITERATIONS;

/* Calculate data sizes in MiB */
encryptDataSizeMiB = ENCRYPT_SIZE / (1024.0 * 1024.0);
decryptDataSizeMiB = DECRYPT_SIZE / (1024.0 * 1024.0);
/* Calculate data size in MiB */
dataSizeMiB = ENCRYPT_SIZE / (1024.0 * 1024.0);

/* Calculate time in milliseconds */
encryptTimeMS = encryptTime / 1000000.0;
Expand All @@ -121,9 +113,9 @@ private static void runBenchmark(String algorithm, String mode) throws Exception
/* Print results */
String testName = "AES-256-" + mode;
System.out.printf("%s-enc %4.2f MiB took %1.3f ms, %8.3f MiB/s%n",
testName, encryptDataSizeMiB, encryptTimeMS, encryptThroughput);
testName, dataSizeMiB, encryptTimeMS, encryptThroughput);
System.out.printf("%s-dec %4.2f MiB took %1.3f ms, %8.3f MiB/s%n",
testName, decryptDataSizeMiB, decryptTimeMS, decryptThroughput);
testName, dataSizeMiB, decryptTimeMS, decryptThroughput);
}

public static void main(String[] args) {
Expand Down

0 comments on commit 562b298

Please sign in to comment.