复现步骤

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

dataSizeThroughput (ops/s)Alloc Rate (MB/s)Alloc Rate Norm (B/op)GC CountGC Time (ms)
3210898.465 ± 83.05313.389 ± 0.2081288.272 ± 12.74722
6410774.131 ± 328.66914.223 ± 0.4911384.274 ± 12.74723
12810624.888 ± 315.78515.971 ± 0.5501576.278 ± 12.74522
25610477.170 ± 184.17319.665 ± 0.3461968.282 ± 0.007≈0*-
5129940.806 ± 208.22525.787 ± 0.5402720.296 ± 0.009≈0*-
10248989.762 ± 181.79136.625 ± 0.7414272.328 ± 0.008≈0*-

Java

dataSizeThroughput (ops/s)Alloc Rate (MB/s)Alloc Rate Norm (B/op)GC CountGC Time (ms)
321440.241 ± 32.57585.402 ± 2.12762182.046 ± 363.347413
641449.436 ± 40.15786.300 ± 2.37962438.038 ± 82.881413
1281423.341 ± 48.89785.244 ± 2.88062806.073 ± 414.351410
2561421.411 ± 92.35786.068 ± 5.76463494.076 ± 172.01949
5121421.932 ± 49.48188.463 ± 3.47065238.073 ± 478.05547
10241338.836 ± 103.82087.834 ± 6.80268798.209 ± 57.40549

对比

比较相同参数下的两种实现,可得 native 实现的性能及 GC 表现显著优于 java 实现

Licensed under CC BY-NC-SA 4.0
Last updated on Oct 22, 2025 16:27 CST
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy