im 试图让我们 jbenchx 对不同的加密方法进行基准测试,但我无法让 @ForEachInt 命令正常工作
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jbenchx.annotations.Bench;
import org.jbenchx.annotations.ForEachInt;
public class keyGen {
public KeyPair generateKeyPair(@ForEachInt({112,196,256}) int size) throws GeneralSecurityException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGen.initialize(size);
return keyPairGen.generateKeyPair();
}
@Bench
public Object createkey() throws GeneralSecurityException {
Security.addProvider(new BouncyCastleProvider());
KeyPair RSA = generateKeyPair();
return RSA;
}
}
}
我尝试仅使用keyPairGen.initialize运行单独的方法,但它也不起作用。有人可以告诉我我的错误吗
@ForEachInt
-命令只是对每个int
-value 执行测试,例如代码:
import org.jbenchx.annotations.*;
public class Main {
@Bench
public int[] doSomething(@ForEachInt({ 20, 10, 30, 10 }) int size) {
return new int[size];
}
}
给出以下输出:
Initializing Benchmarking Framework...
Running on Windows 7 6.1
Max heap = 3784310784 System Benchmark = 0,83ns
Performing 4 benchmarking tasks..
[0] Main.doSomething(20)****!********** 16,3ns
[1] Main.doSomething(10)********** 16,6ns
[2] Main.doSomething(30)****!************ 17,1ns
[3] Main.doSomething(10).*.***.*** 16,5ns
Success.
如您所见,@ForEachInt
语句有四个int
值,因此执行了四个测试。http://iquadrat.github.io/jbenchx/中显示了类似的例子。
代码中有两个缺陷:
- 代码无法编译,因为在
createkey()
-方法 中调用的generateKeyPair()
-方法未定义(定义的generateKeyPair(int)
-方法 具有int
参数,因此具有不同的签名(。 @ForEachInt
-命令必须在标有@Bench
-annotatioan 的方法中使用,即createKey()
.
更改代码,如下所示:
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jbenchx.annotations.Bench;
import org.jbenchx.annotations.ForEachInt;
public class keyGen {
private KeyPair generateKeyPair(int size) throws GeneralSecurityException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGen.initialize(size);
return keyPairGen.generateKeyPair();
}
@Bench
public Object createkey(@ForEachInt({ 112, 196, 256 }) int size) throws GeneralSecurityException {
Security.addProvider(new BouncyCastleProvider());
KeyPair RSA = generateKeyPair(size);
return RSA;
}
}
然后输出变为:
Initializing Benchmarking Framework...
Running on Windows 7 6.1
Max heap = 3784310784 System Benchmark = 0,84ns
Performing 3 benchmarking tasks..
[0] keyGen.createkey(112)!!!!!!!!!!.!.!!!!!!!!!!!!.!!.!!!..!!!*!!..*..*...* 952us
[1] keyGen.createkey(196)!!!!!..*....*...* 1.94ms
[2] keyGen.createkey(256)!!!!!!!!*.*!!*.*.*!.*.*!*.**.****. 2.88ms
Success.