Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ jobs:
publishJUnitResults: true
testResultsFiles: '**/TEST-*.xml'
tasks: 'build'

- job: benchmark
dependsOn:
- ut
steps:
- script:
gradle jmh

- job: releaseJar
dependsOn:
- ut
Expand Down
13 changes: 11 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'checkstyle'
id 'maven-publish'
id "me.champeau.gradle.jmh" version "0.5.2"
}

group 'twgc'
Expand All @@ -15,7 +16,7 @@ allprojects {
maven {
url 'https://maven.aliyun.com/repository/public/'
}
mavenLocal()
//mavenLocal()
mavenCentral()
}
}
Expand All @@ -28,6 +29,10 @@ tasks.withType(Test) {
systemProperty "file.encoding", "UTF-8"
}

jmh {
zip64 = false
}

checkstyle {
toolVersion '6.11.1'
showViolations true
Expand Down Expand Up @@ -68,4 +73,8 @@ dependencies {
implementation group: 'org.bouncycastle', name: 'bcpkix-jdk15on', version: '1.67'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
}
jmh 'org.apache.commons:commons-lang3:3.11'
jmh 'org.openjdk.jmh:jmh-core:1.25'
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.25'
jmh group: 'org.bouncycastle', name: 'bcpkix-jdk15on', version: '1.67'
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
93 changes: 93 additions & 0 deletions src/jmh/java/SM2UtilBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package javagm;

import java.security.*;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.RandomStringUtils;
import org.bouncycastle.crypto.InvalidCipherTextException;
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;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread) //Thread: 该状态为每个线程独享。
public class SM2UtilBenchmark {

@State(Scope.Thread)
public static class BenchmarkState {
static int randomData = 128;
static byte[] message = RandomStringUtils.random(randomData).getBytes();
SM2Util instance;
KeyPair keyPair;
PublicKey pubKey;
PrivateKey privKey;
byte[] signbyte;
byte[] encrypted;
{
try {
instance = new SM2Util();
keyPair = instance.generatekeyPair();
pubKey = keyPair.getPublic();
privKey = keyPair.getPrivate();
signbyte = instance.sign(this.privKey, message);
encrypted = instance.encrypt(this.pubKey, message);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@Benchmark
public void sign(BenchmarkState state) {
try {
state.instance.sign(state.privKey, state.message);
} catch (SignatureException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}

@Benchmark
public void verify(BenchmarkState state) {
try {
state.instance.verify(state.pubKey, state.message, state.signbyte);
} catch (SignatureException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}

@Benchmark
public void encrypt(BenchmarkState state) {
try {
state.instance.encrypt(state.pubKey, state.message);
} catch (InvalidCipherTextException e) {
e.printStackTrace();
}
}

@Benchmark
public void decrypt(BenchmarkState state) {
try {
state.instance.decrypt(state.privKey, state.encrypted);
} catch (InvalidCipherTextException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(SM2UtilBenchmark.class.getSimpleName()) //benchmark 所在的类的名字,注意这里是使用正则表达式对所有类进行匹配的
.forks(1) //进行 fork 的次数。如果 fork 数是2的话,则 JMH 会 fork 出两个进程来进行测试
.warmupIterations(3) //预热的迭代次数
.measurementIterations(5) //实际测量的迭代次数
.build();

new Runner(opt).run();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package javagm;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package javagm;

import java.security.Security;
import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package javagm;

import java.security.*;
import java.util.EnumMap;
import javax.crypto.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package javagm;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package javagm;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Assert;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package javagm;

import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.junit.Assert;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package javagm;

import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
Expand Down