mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 02:30:25 +08:00
add:添加sm234的jmh版性能测试,与力宏师兄的版本性能进行对比
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
@@ -119,7 +120,13 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.6.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,91 @@
|
||||
package minsecurity.crypto;
|
||||
|
||||
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import minsecurity.identity.IdentityBenchmark;
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@State(Scope.Thread)
|
||||
@Fork(1)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Warmup(iterations = 3)
|
||||
@Measurement(iterations = 5)
|
||||
public class SM2Benchmark {
|
||||
private static String content = "7b227462734365727469666963617465223a7b2276657273696f6e223a312c2273657269616c4e756d626572223a312c227075626c69634b6579223a22424a304d7047714e5958706b695879575a4d6e5838776d6b694a51326f576d37336257615932525473706b695058342f37705443666332764e7272596e766c6a644d3448647850537531753272616363506c74485245593d222c227369676e6174757265416c676f726974686d223a302c227075626c69634b6579416c676f726974686d223a302c226973737565546f223a226973737565546f222c22697373756572223a22697373756572222c226e6f744265666f7265223a313631353032303130393339352c226e6f744166746572223a313631353032303130393539352c226b65795573616765223a322c2274696d657374616d70223a313631353032303130393339352c226361223a747275657d2c227369676e6174757265416c676f726974686d223a302c227369676e617475726556616c7565223a6e756c6c7d";
|
||||
private static SM2PrivateKey sm2PrivateKey;
|
||||
|
||||
private static SM2PublicKey sm2PublicKey;
|
||||
private static byte[] digest;
|
||||
private static byte[] encBytes;
|
||||
private static String content2;
|
||||
static {
|
||||
try {
|
||||
init(); // 初始化类成员公私钥
|
||||
digest = sm2PrivateKey.sign(ByteUtils.fromHexString(content)); // 初始化类成员摘要
|
||||
content2 = "hello world";
|
||||
encBytes = sm2PublicKey.encrypt(content2.getBytes());
|
||||
} catch (CryptoException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void init(){
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
sm2PrivateKey = new SM2PrivateKey(d);
|
||||
sm2PublicKey = new SM2PublicKey(x,y);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testSign() throws CryptoException {
|
||||
SM2Benchmark.sm2PrivateKey.sign(ByteUtils.fromHexString(content));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testVerify() throws CryptoException {
|
||||
SM2Benchmark.sm2PublicKey.verify(ByteUtils.fromHexString(content), digest);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testGenKeyPair(){
|
||||
SM2Base.generateKeyPairParameter();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testEncrypt() throws InvalidCipherTextException {
|
||||
sm2PublicKey.encrypt(content2.getBytes());
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testDecrypt() throws InvalidCipherTextException {
|
||||
sm2PrivateKey.decrypt(encBytes);
|
||||
}
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(SM2Benchmark.class.getSimpleName())
|
||||
.build();
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package minsecurity.crypto;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@State(Scope.Thread)
|
||||
@Fork(1)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Warmup(iterations = 3)
|
||||
@Measurement(iterations = 5)
|
||||
public class SM3Benchmark {
|
||||
private static HashAlgo hashAlgo;
|
||||
private static String srcString = "123456";
|
||||
private static byte[] srcData = srcString.getBytes();
|
||||
static {
|
||||
hashAlgo = new HashAlgo();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testSM3(){
|
||||
hashAlgo.sm3(srcData);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testSM3ByString(){
|
||||
hashAlgo.sm3(srcString);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(SM3Benchmark.class.getSimpleName())
|
||||
.build();
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package minsecurity.crypto;
|
||||
|
||||
import minsecurity.identity.IdentityBenchmark;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static minsecurity.crypto.TestSM4.SRC_DATA;
|
||||
import static minsecurity.crypto.TestSM4.SRC_DATA_16B;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@State(Scope.Thread)
|
||||
@Fork(4)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Warmup(iterations = 3)
|
||||
@Measurement(iterations = 5)
|
||||
public class SM4Benchmark {
|
||||
private static byte[] key;
|
||||
private static byte[] iv;
|
||||
private static byte[] cipherText;
|
||||
private static byte[] cipherText2;
|
||||
|
||||
static {
|
||||
try {
|
||||
key = SM4.generateKey();
|
||||
iv = SM4.generateKey();
|
||||
cipherText = SM4.encrypt_ECB_Padding(key, SRC_DATA_16B);
|
||||
cipherText2 = SM4.encrypt_CBC_Padding(key, iv, SRC_DATA);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testECBEncrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
||||
SM4.encrypt_ECB_Padding(key, SRC_DATA_16B);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testECBDecrypt() throws BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
|
||||
SM4.decrypt_ECB_Padding(key, cipherText);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testCBCEncrypt() throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
||||
SM4.encrypt_CBC_Padding(key, iv, SRC_DATA);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testCBCDecrypt() throws BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
|
||||
SM4.decrypt_CBC_Padding(key, iv, cipherText2);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder()
|
||||
.include(SM4Benchmark.class.getSimpleName())
|
||||
.build();
|
||||
new Runner(opt).run();
|
||||
}
|
||||
}
|
||||
@@ -20,12 +20,12 @@ import packet.Interest;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@State(Scope.Thread)
|
||||
@Fork(1)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Warmup(iterations = 3)
|
||||
@Measurement(iterations = 5)
|
||||
@BenchmarkMode(Mode.AverageTime) // 统计平均响应时间
|
||||
@State(Scope.Thread) // 每个进行基准测试的线程都会独享一个对象示例
|
||||
@Fork(1) // 开启一个线程进行测试
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS) // 输出的时间单位
|
||||
@Warmup(iterations = 3) // 微基准测试前进行三次预热执行
|
||||
@Measurement(iterations = 5) // 进行五次微基准测试
|
||||
public class KeychainBenchmark {
|
||||
/**
|
||||
* 随机生成身份数据
|
||||
|
||||
Reference in New Issue
Block a user