Fan Question: Convert postman code to beanshell snippet

"Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-in Event, click to view the details of the event ."

story cause

At station C, fans bought the column of the topic owner, and the topic owner promised to solve any problems with their hearts. No, fans often come to them. At the beginning, a picture says: How do we convert the code that we pre-processed in postman into jmeter Know the code? The subject looked at the screenshot, and the inner OS: Isn't this very simple? But the first sentence of the fans was: the first sentence of code was completely stunned! So the result is that the fans don't care about the bitterness of the topic owner at all: please read all the column articles of the topic owner one by one, maybe there is something to gain? The foundation is not firmly shaken, this is not an alarmist!

image

start experiment

Disclaimer: JMeter can support writing code is the beanshell component, yes, any of its components.

image

Process analysis

The reason why fans say it is difficult is because there is no java code base and the number of jmeter components used; if it is about the functions that JMeter does not support, the first thing that comes to mind is to wrap the handwritten code into a plug-in or beanshell programming, the subject recommends the first one method, because it is more general.

  • The first sentence of the postman screenshot means: get a 13-digit timestamp;
  • The second sentence and the frequently-occurring var that follows are keywords for defining variables;
  • console.log is similar to JMeter's log.info () is log output;
  • For methods: Math, SHA256, etc. are all method references. If JMeter does not support it, handwritten code is required;
  • Coincidentally, JMeter provides the corresponding function: function assistant to understand

image

  • The latter so many sets are to set global variables.
# 关于时间函数
${__time(yyyy-mm-dd hh:mm:ss,)} 
${__time(yyyy-mm-dd,)}
# 13位时间戳
${__time(,)}
# 10位时间戳
${__time(/1000,)}  

# 规则字符串
${__UUID}

# 加密算法,它支持MD5、SHA-256;具体参见JMETER功能函数助手介绍
${__digest(SHA-256,string,,true,)}
复制代码

Test plan implementation

To practice by creating a test plan through JMeter, the idea is: the parameters are processed by the beanshell preprocessor code, and then assigned to the HTTP sampler, which can also be used directly in the next interface request;

  • beanshell preprocessing interface input parameters
    image
  • Encrypted parameters using function references
    image
  • test function
    image
  • Verify test results
    image

code segment

// 时间戳字符串
String timeStamp = "${__time(,)}";
// 控制台输出
log.info(timeStamp);
// 定义或者获取token,或获取变量
String token = "aslfjlajlskfslf";
String key = "123456";
// 赋值,在下一个请求:${key}引用
vars.put("key","value_abc");

// 这里关于随机字符串的算法,可能需要自己定义;Jmeter提供了uuid;结构与你的案例符合
String randStr = "${__UUID}";

String sigStr = timeStamp + token + key + timeStamp;
vars.put("sigStr",sigStr);

// 签名算法,在jmeter支持md5,sha256可能需要自己写好;恰恰又有函数支持;如果在同一个脚本中可能有问题,但是在外面作为入参或者引用是没问题的
// String signature = "${__digest(SHA-256,vars.get("sigStr"),,true,)}";
// log.info(signature);
复制代码

summary

After the above experiments, the code process of postman has been implemented in JMeter, so the use of JMeter tools must be mastered. When the functional requirements cannot be met, secondary development should be considered, such as encryption algorithms. If it is not supported, write it by hand. In the above quote, such as the {__digest()} function, the string that needs to be encrypted is alive and will change with each request, so how to make it dynamic is the key. It is recommended to understand: {__V() }, {__eval()}, {__beanshell()} and other functions.

Guess you like

Origin juejin.im/post/7079792531411730463