package security; import component.ComponentException; import component.Identifier; import component.TTL; import encoding.VlInt; import minsecurity.Common; import minsecurity.certificate.cert.CertUtils; import minsecurity.certificate.cert.Certificate; import minsecurity.crypto.sm2.SM2KeyPair; import minsecurity.identity.Identity; import minsecurity.identity.KeyParam; //import org.checkerframework.checker.units.qual.K; 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 packet.CPacket; import packet.Data; 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) // 进行五次微基准测试 public class KeychainBenchmark { /** * 随机生成身份数据 * @return Identity */ private Identity createRandomIdentity() throws Exception{ // 测试PersistIdentity SM2KeyPair pair = SM2KeyPair.generateKeyPair(); Identity identity = new Identity(); identity.setName("/wzq"+Math.random()); KeyParam keyParam = new KeyParam(); keyParam.PublicKeyAlgorithm = 0; keyParam.SignatureAlgorithm = 0; identity.setKeyParam(keyParam); identity.setPrikey(pair.getSm2PrivateKey()); identity.setPubkey(pair.getSm2PublicKey()); identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99"); // identity.lock("0123456789abcdef", Common.SM4ECB); Certificate cert = new Certificate(); cert.setVersion(1); cert.setSerialNumber(1); cert.setPublicKey(pair.getSm2PublicKey()); cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3? cert.setPublicKeyAlgorithm(Common.SM2); cert.setIssueTo("root"); cert.setIssuer("root"); long timestamp = System.currentTimeMillis() / 1000; cert.setTimestamp(timestamp); // 10bit timestamp cert.setNotAfter(timestamp); // 10bit timestamp cert.setNotBefore(timestamp + 1000); // 10bit timestamp cert.setKeyUsage(Common.CertSign); cert.setCA(true); CertUtils.signCert(cert, pair.getSm2PrivateKey()); identity.setCert(cert); return identity; } private static KeyChain chain; private static Interest interest, interest2, interest3, interest4, interest5, interest6; // 用于签名 private static Data data, data2, data3, data4, data5, data6; // 用于签名 private static CPacket cPacket, cPacket2, cPacket3, cPacket4, cPacket5, cPacket6; // 用于签名 private static byte[] createPayload(int mode){ byte[] res = null; switch (mode){ case 0: return new byte[]{1}; case 1: res = new byte[1024]; for (int i = 0; i < 1024 ; i++) { res[i] = (byte) (i % 128); } return res; case 8: res = new byte[8192]; for (int i = 0; i < 8192 ; i++) { res[i] = (byte) (i % 128); } return res; default: return res; } } private static Interest createInterest(byte[] payload) throws ComponentException { // 身份允许签名与验证签名 Identifier id1 = new Identifier("/min/pku/sz"); //////////////////// 构建兴趣包 //////////////////////// Interest interest = new Interest(); interest.minPacket.packetType = new VlInt(3); interest.setName(id1); interest.canBePrefix.setCanBePrefix(true); interest.mustBeRefresh.setMustBeRefresh(true); interest.nonce.setNonce(1234); interest.hopLimit.setHopLimit(1234); interest.congestionMark.setCongestionLevel(1234); interest.ttl.setTtl(1234); interest.payload.setValue(payload); return interest; } private static Data createData(byte[] payload) throws ComponentException { Data data = new Data(); data.freshnessPeriod.setFreshnessPeriod(02346345465453L); data.payload.setValue(payload); data.congestionMark.setCongestionLevel(1234L); Identifier id2 = new Identifier("/wzq"); data.setName(id2); data.minPacket.packetType = new VlInt(3); return data; } private static CPacket createCPacket(byte[] payload) throws ComponentException { Identifier id1 = new Identifier("/min/pku/sz"); Identifier id2 = new Identifier("/install"); CPacket cPacket = new CPacket(); cPacket.minPacket.packetType = new VlInt(3); cPacket.setSrcIdentifier(id1); cPacket.setDstIdentifier(id2); cPacket.setTtl(new TTL(16546418374324163L)); cPacket.payload.setValue(payload); return cPacket; } static { try { chain = new KeyChain(); byte[] payLoad = createPayload(0); byte[] payLoad1K = createPayload(1); byte[] payLoad8K = createPayload(8); // 1byte 兴趣包准备 interest = createInterest(payLoad); interest2 = createInterest(payLoad); chain.signInterest(interest2); // 1K 兴趣包准备 interest3 = createInterest(payLoad1K); interest4 = createInterest(payLoad1K); chain.signInterest(interest4); // 8K 兴趣包准备 interest5 = createInterest(payLoad8K); interest6 = createInterest(payLoad8K); chain.signInterest(interest6); // 1byte 数据包准备 data = createData(payLoad); data2 = createData(payLoad); chain.signData(data2); // 1K 数据包准备 data3 = createData(payLoad1K); data4 = createData(payLoad1K); chain.signData(data4); // 8K 数据包准备 data5 = createData(payLoad8K); data6 = createData(payLoad8K); chain.signData(data6); // 1byte 普通包准备 cPacket = createCPacket(payLoad); cPacket2 = createCPacket(payLoad); chain.signCPacket(cPacket2); // 1K 普通包准备 cPacket3 = createCPacket(payLoad1K); cPacket4 = createCPacket(payLoad1K); chain.signCPacket(cPacket4); // 8K 普通包准备 cPacket5 = createCPacket(payLoad8K); cPacket6 = createCPacket(payLoad8K); chain.signCPacket(cPacket6); } catch (Exception e) { e.printStackTrace(); } } // @Benchmark // public void testCreateKeyChain() throws Exception { // KeyChain keyChain = new KeyChain(); // } // // @Benchmark // public void testInitialKeyChain() throws Exception { // // 测试设置新身份为当前身份 // KeyChain keyChain = new KeyChain(); // // Identity id = createRandomIdentity(); // // keyChain.getIdentifyManager().setDefaultIdentity(id, true); // // 输入密码用于解锁身份 // // keyChain.setCurrentIdentity(keyChain.getIdentifyManager().getDefaultIdentity(), "0123456789abcdef"); // } @Benchmark public void testSignData() throws Exception { // 正常测试签名与验签 chain.signData(data); } @Benchmark public void testSignData1K() throws Exception { // 正常测试签名与验签 chain.signData(data3); } @Benchmark public void testSignData8K() throws Exception { // 正常测试签名与验签 chain.signData(data5); } @Benchmark public void testVerifyData() throws Exception { // 正常测试签名与验签 chain.verifyData(data2); } @Benchmark public void testVerifyData1K() throws Exception { // 正常测试签名与验签 chain.verifyData(data4); } @Benchmark public void testVerifyData8K() throws Exception { // 正常测试签名与验签 chain.verifyData(data6); } @Benchmark public void testSignCPacket() throws Exception { // 正常测试签名与验签 chain.signCPacket(cPacket); } @Benchmark public void testSignCPacket1K() throws Exception { // 正常测试签名与验签 chain.signCPacket(cPacket3); } @Benchmark public void testSignCPacket8K() throws Exception { // 正常测试签名与验签 chain.signCPacket(cPacket5); } @Benchmark public void testVerifyCPacket() throws Exception { // 正常测试签名与验签 chain.verifyCPacket(cPacket2); } @Benchmark public void testVerifyCPacket1K() throws Exception { // 正常测试签名与验签 chain.verifyCPacket(cPacket4); } @Benchmark public void testVerifyCPacket8K() throws Exception { // 正常测试签名与验签 chain.verifyCPacket(cPacket6); } @Benchmark public void testSignInterest() throws Exception { // 正常测试签名与验签 chain.signInterest(interest); } @Benchmark public void testSignInterest1K() throws Exception { // 正常测试签名与验签 chain.signInterest(interest3); } @Benchmark public void testSignInterest8K() throws Exception { // 正常测试签名与验签 chain.signInterest(interest5); } @Benchmark public void testVerifyInterest() throws Exception { // 正常测试签名与验签 chain.verifyInterest(interest2); } @Benchmark public void testVerifyInterest1K() throws Exception { // 正常测试签名与验签 chain.verifyInterest(interest4); } @Benchmark public void testVerifyInterest8K() throws Exception { // 正常测试签名与验签 chain.verifyInterest(interest6); } public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(KeychainBenchmark.class.getSimpleName()) .build(); new Runner(opt).run(); } }