复现步骤
export LANG=C
export CC=clang
export CXX=clang++
export USE_CLANG=true
export COMPILER_WARNINGS_FATAL=false
export ARCH_DATA_MODEL=64
export LP64=1
export HOTSPOT_BUILD_JOBS=10
# 配置构建系统(macOS)
bash configure \
--with-boot-jdk=/opt/homebrew/opt/openjdk@17 \
--with-jtreg=./jtreg \
--with-jmh=./jmh \
--with-openssl=/opt/homebrew/opt/openssl@3 \
--disable-warnings-as-errors
make clean
# 构建 JDK
make images
# 验证构建
build/macosx-aarch64-server-release/jdk/bin/java --version
# 运行 jtreg:NativeSM2Test 测试
make test TEST="jtreg:test/jdk/sm/crypto/SM2/NativeSM2Test.java" \
JTREG="VM_OPTIONS=-Djdk.openssl.cryptoLibPath=/opt/homebrew/opt/openssl@3/lib/libcrypto.3.dylib"
# 构建 JMH 测试
make build-microbenchmark
# 基于 openssl 运行 JMH 测试
make test TEST="micro:org.openjdk.bench.java.security.SM2ComparisonBenchmark" \
MICRO="OPTIONS=-jvmArgs=-Djdk.openssl.cryptoLibPath=/opt/homebrew/opt/openssl@3/lib/libcrypto.3.dylib -prof gc"
# 基于 java 运行 JMH 测试
make test TEST="micro:org.openjdk.bench.java.security.SM2ComparisonBenchmark" \
MICRO="OPTIONS=-prof gc"
Jtreg 测试
CMD
make test TEST="jtreg:test/jdk/sm/crypto/SM2/NativeSM2Test.java" \
JTREG="VM_OPTIONS=-Djdk.openssl.cryptoLibPath=/opt/homebrew/opt/openssl@3/lib/libcrypto.3.dylib"
OUTPUT
Building target 'test' in configuration 'macosx-aarch64-server-release'
...
Running tests using JTREG control variable 'VM_OPTIONS=-Djdk.openssl.cryptoLibPath=/opt/homebrew/opt/openssl@3/lib/libcrypto.3.dylib'
Test selection 'jtreg:test/jdk/sm/crypto/SM2/NativeSM2Test.java', will run:
* jtreg:test/jdk/sm/crypto/SM2/NativeSM2Test.java
Running test 'jtreg:test/jdk/sm/crypto/SM2/NativeSM2Test.java'
Passed: sm/crypto/SM2/NativeSM2Test.java
Test results: passed: 1
Report written to /Users/hongpeng.lin/Project/TencentKona-17/build/macosx-aarch64-server-release/test-results/jtreg_test_jdk_sm_crypto_SM2_NativeSM2Test_java/html/report.html
Results written to /Users/hongpeng.lin/Project/TencentKona-17/build/macosx-aarch64-server-release/test-support/jtreg_test_jdk_sm_crypto_SM2_NativeSM2Test_java
Finished running test 'jtreg:test/jdk/sm/crypto/SM2/NativeSM2Test.java'
Test report is stored in build/macosx-aarch64-server-release/test-results/jtreg_test_jdk_sm_crypto_SM2_NativeSM2Test_java
==============================
Test summary
==============================
TEST TOTAL PASS FAIL ERROR
jtreg:test/jdk/sm/crypto/SM2/NativeSM2Test.java 1 1 0 0
==============================
TEST SUCCESS
Finished building target 'test' in configuration 'macosx-aarch64-server-release'
性能表现
仅展示部分数据(DecryptWorkFlow)
JNI + OPENSSL
| dataSize | Throughput (ops/s) | Alloc Rate (MB/s) | Alloc Rate Norm (B/op) | GC Count | GC Time (ms) |
|---|---|---|---|---|---|
| 32 | 10898.465 ± 83.053 | 13.389 ± 0.208 | 1288.272 ± 12.747 | 2 | 2 |
| 64 | 10774.131 ± 328.669 | 14.223 ± 0.491 | 1384.274 ± 12.747 | 2 | 3 |
| 128 | 10624.888 ± 315.785 | 15.971 ± 0.550 | 1576.278 ± 12.745 | 2 | 2 |
| 256 | 10477.170 ± 184.173 | 19.665 ± 0.346 | 1968.282 ± 0.007 | ≈0* | - |
| 512 | 9940.806 ± 208.225 | 25.787 ± 0.540 | 2720.296 ± 0.009 | ≈0* | - |
| 1024 | 8989.762 ± 181.791 | 36.625 ± 0.741 | 4272.328 ± 0.008 | ≈0* | - |
Java
| dataSize | Throughput (ops/s) | Alloc Rate (MB/s) | Alloc Rate Norm (B/op) | GC Count | GC Time (ms) |
|---|---|---|---|---|---|
| 32 | 1440.241 ± 32.575 | 85.402 ± 2.127 | 62182.046 ± 363.347 | 4 | 13 |
| 64 | 1449.436 ± 40.157 | 86.300 ± 2.379 | 62438.038 ± 82.881 | 4 | 13 |
| 128 | 1423.341 ± 48.897 | 85.244 ± 2.880 | 62806.073 ± 414.351 | 4 | 10 |
| 256 | 1421.411 ± 92.357 | 86.068 ± 5.764 | 63494.076 ± 172.019 | 4 | 9 |
| 512 | 1421.932 ± 49.481 | 88.463 ± 3.470 | 65238.073 ± 478.055 | 4 | 7 |
| 1024 | 1338.836 ± 103.820 | 87.834 ± 6.802 | 68798.209 ± 57.405 | 4 | 9 |
对比
比较相同参数下的两种实现,可得 native 实现的性能及 GC 表现显著优于 java 实现