測試目的
測試Java下的AES的加密速度。
測試環境
-
機器1:
- 硬件:i5 9600K + 16 GB DDR4 2666 + 512GB Intel 760P NVME
- 軟件:Window 10 + Java 1.8.0_251
-
機器2:
- 硬件:i7 6500K + 8 GB DDR4 2666 + 1T機械硬盤
- 軟件:Window 10 + Java 11.0.7
測試結果
根據以下測試結果,在第1次虛擬機熱身後,機器1的加密速度可以達到 280MB/s 左右,機器2可以達到130 MB/s - 170 MB/s。
機器1測試結果
-------------- test 0 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 206.19 MB/s.
-------------- test 1 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 273.22 MB/s.
-------------- test 2 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 269.54 MB/s.
-------------- test 3 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 273.22 MB/s.
-------------- test 4 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 271.00 MB/s.
-------------- test 5 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 280.11 MB/s.
-------------- test 6 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 278.55 MB/s.
-------------- test 7 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 280.11 MB/s.
-------------- test 8 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 284.90 MB/s.
-------------- test 9 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 280.11 MB/s.
機器2測試結果
-------------- test 0 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 76.92 MB/s.
-------------- test 1 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 97.09 MB/s.
-------------- test 2 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 141.04 MB/s.
-------------- test 3 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 175.75 MB/s.
-------------- test 4 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 169.20 MB/s.
-------------- test 5 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 133.69 MB/s.
-------------- test 6 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 136.61 MB/s.
-------------- test 7 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 133.87 MB/s.
-------------- test 8 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 138.12 MB/s.
-------------- test 9 --------------
Data.Length: 1.00 MB.
Running Times: 100.
Output.Length: 133.34 MB.
Encrypt Speed: 132.98 MB/s.
測試代碼
package hao.tests;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class EncryptPerformanceTest {
public static void main(String[] args) throws Exception {
for(int test = 0; test < 10; test++) {
System.out.printf("-------------- test %d --------------\n", test);
encryptTest();
}
}
static void encryptTest() throws Exception {
// generate 1 MB data to be encrypted.
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 1024 * 64; i++)
sb.append("Hello:0123456789");
String data = sb.toString();
int times = 100; // running times.
int t = 0; // total length of output
long t1 = System.currentTimeMillis();
for(int i = 0; i < times; i++)
t += encrypt(data, "0123456789ABCDEF").length();
long t2 = System.currentTimeMillis();
// print results.
System.out.printf("Data.Length: %.2f MB.\nRunning Times: %d.\nOutput.Length: %.2f MB.\nEncrypt Speed: %.2f MB/s.\n",
data.length()/1024.0/1024, times, t/1024.0/1024, 100.0 * 1000 / (t2-t1));
}
public static String encrypt(String content, String key) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec("1234567812345678".getBytes("utf-8")));
byte[] bytes = cipher.doFinal(content.getBytes());
String base64 = java.util.Base64.getEncoder().encodeToString(bytes);
return base64;
}
}