51Testing软件测试论坛

标题: jmeter BeanShell PreProcessor 性能问题 [打印本页]

作者: 测试积点老人    时间: 2021-11-26 10:20
标题: jmeter BeanShell PreProcessor 性能问题
在 jmeter 中使用 beanshell 实现签名算法,如下图,在使用过程中发现,直接在 BeanShell PreProcessor 实现加密算法在执行过程中耗时较久,达到 200ms+,影响性能测试
[attach]135445[/attach]
[attach]135446[/attach]
之后将签名算法打包成 jar 包,发现性能提升许多
[attach]135448[/attach]
有哪位大佬可以告诉我这中间的区别吗?以及原因吗?
具体代码
  1. import javax.crypto.Cipher;
  2. import javax.crypto.SecretKey;
  3. import javax.crypto.spec.IvParameterSpec;
  4. import javax.crypto.spec.SecretKeySpec;

  5. import org.apache.commons.codec.binary.Base64;
  6. import java.security.*;
  7. import java.security.spec.PKCS8EncodedKeySpec;

  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.text.DecimalFormat;

  11. public class App {
  12.     public static byte[] TripleDesEncrypt(byte[] content, byte[] key) throws Exception {
  13.         byte[] icv = new byte[8];
  14.         System.arraycopy(key, 0, icv, 0, 8);
  15.         return TripleDesEncrypt(content, key, icv);
  16.     }

  17.     protected static byte[] TripleDesEncrypt(byte[] content, byte[] key, byte[] icv) throws Exception {
  18.         final SecretKey secretKey = new SecretKeySpec(key, "DESede");
  19.         final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
  20.         final IvParameterSpec iv = new IvParameterSpec(icv);
  21.         cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
  22.         return cipher.doFinal(content);
  23.     }

  24.     public static byte[] TripleDesDecrypt(byte[] content, byte[] key) throws Exception {
  25.         byte[] icv = new byte[8];
  26.         System.arraycopy(key, 0, icv, 0, 8);
  27.         return TripleDesDecrypt(content, key, icv);
  28.     }

  29.     protected static byte[] TripleDesDecrypt(byte[] content, byte[] key, byte[] icv) throws Exception {
  30.         final SecretKey secretKey = new SecretKeySpec(key, "DESede");
  31.         final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
  32.         final IvParameterSpec iv = new IvParameterSpec(icv);
  33.         cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
  34.         return cipher.doFinal(content);
  35.     }
  36.     public static String sign(String content, String privateKeyPem,long time) {
  37.         try {
  38.             long startTime = System.currentTimeMillis();         
  39.         String privateKeyPem_String = privateKeyPem.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
  40.         byte[] encodedKey = org.bouncycastle.util.encoders.Base64.decode(privateKeyPem_String.getBytes("UTF-8"));
  41.         long spendtime2 = System.currentTimeMillis() - startTime;
  42.         log.info("=====sign Base64 :"+ spendtime2);

  43.         long startTime3 = System.currentTimeMillis();
  44.         PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
  45.         Signature signature = Signature.getInstance("SHA256WithRSA");

  46.         long spendtime4 = System.currentTimeMillis() - startTime3;
  47.         log.info("=====sign signature :"+ spendtime4);
  48.         long startTime1 = System.currentTimeMillis();
  49.         signature.initSign(privateKey);
  50.         long spendtime3 = System.currentTimeMillis() - startTime1;
  51.         log.info("=====sign initSign :"+ spendtime3);
  52.         signature.update(content.getBytes("utf-8"));
  53.         byte[] signed = signature.sign();
  54.         long spendtime5 = System.currentTimeMillis() - startTime1;
  55.         log.info("=====sign update :"+ spendtime5);
  56.         return new String(org.bouncycastle.util.encoders.Base64.encode(signed));
  57.         } catch (Exception var6) {
  58.             String errorMessage = "签名遭遇异常,content=" + content + " privateKeySize=" + privateKeyPem.length() + " reason=" + var6.getMessage();
  59.             // log.error("====error: "+errorMessage);
  60.             throw new RuntimeException(errorMessage, var6);
  61.         }
  62.     }

  63. }
  64. // ==============begin================
  65. String timestamp = String.valueOf(System.currentTimeMillis());
  66. vars.put("timestamp",timestamp);
  67. long startTime = System.currentTimeMillis();
  68. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  69. Random rand = new Random();
  70. Integer randomNum = rand.nextInt(5000000) + 1000000;
  71. String randomNumStr = String.valueOf(randomNum);
  72. String order_id = sdf.format(new Date()) + randomNumStr;
  73. vars.put("order_id",order_id);

  74. double a=Math.random()*100 + 1.00;
  75. DecimalFormat df = new DecimalFormat( "0.00" );
  76. String pay_str=df.format(a);

  77. Random rand = new Random();
  78. Integer randomNum = rand.nextInt(5000000) + 1000000;
  79. String randomNumStr = String.valueOf(randomNum);
  80. String id_card = "430" + randomNumStr + "10104219";

  81. String private_key = vars.get("key");
  82. String dealer_id = "00243810";

  83. String data = "{\"order_id\":\""+order_id+"\",\"dealer_id\":\"\",\"broker_id\":\"\",\"real_name\":\"\",\"card_no\":\"\",\"phone_no\":\"\",\"id_card\":\"\", \"pay\":\""+pay_str+"\"}";
  84. byte[] des3key = "0857b75Fmev7mbTFuhUNa6Rt".getBytes("utf-8");
  85. byte[] enc = App.TripleDesEncrypt(data.getBytes("utf-8"), des3key);

  86. byte[] enc64 = Base64.encodeBase64(enc);
  87. String data64 = new String(enc64);
  88. vars.put("data",data64);

  89. // 进行sign
  90. String sign_data = "data=" + data64 + "&mess=test×tamp="+ timestamp + "&key=0aYxoR0s474JZns9kS09oXnA2e7Rz0fj";
  91. String sign = App.sign(sign_data, private_key, startTime);
  92. vars.put("sign",sign);
复制代码



作者: 海海豚    时间: 2021-11-29 09:30
https://blog.csdn.net/weixin_30920513/article/details/95674732   参考下吧
作者: qqq911    时间: 2021-11-29 16:00
可能是代码太复杂,每次都要解析一遍导致的?




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2