笔者在进行 Loadrunner 压测时,编写 http接口测试代码时,对于post 请求 常用到2个函数:

• web_submit_data()

• web_custom_request()

现将性能代码分享如下:

一、 关于 web_submit_data() 函数

web_reg_find("Text=\"success\":true",

        "SaveCount=spz_Count", 

        LAST );

lr_start_transaction("spz");

web_submit_data("spz.htm", 

"Action={url}/api/flow/FlowListFrom?ticket={ticket}", 

"Method=POST", 

"Mode=HTML", 

ITEMDATA, 

"Name=pageSize", "Value=10", ENDITEM, 

"Name=pageNumber", "Value=1",ENDITEM,  

LAST ); 

if (atoi(lr_eval_string("{spz_Count}")) > 0){

     lr_output_message("spz successful."); 

     lr_end_transaction("spz", LR_PASS);

     } 

  else{ 

     lr_end_transaction("spz", LR_FAIL);

  } 

二、关于 web_custom_request() 函数

 web_add_header("User-Agent","Mozilla/6.0 (Macintosh; Intel Mac OS X 12_19_9) AppleWebKit/63.86 (KHTML, like Gecko) Chrome/62.100 Safari/637.3");

  web_reg_find("Text=\"success\":true", 

        "SaveCount=code_Count", 

        LAST );

lr_start_transaction("code");

  web_custom_request("code",
  "Method=POST",
  "URL={url}/api/getItemByCode?ticket={ticket}",
  "RecContentType=application/json", 
  "EncType=application/json", 
  "Mode=HTML",
  "body={\"scope\": \"native\",\"itemCode\":\"TxtSet0\"}",
  LAST);

  if (atoi(lr_eval_string("{code_Count}")) > 0){ 

     lr_output_message("code successful."); 

     lr_end_transaction("code", LR_PASS);

     } 

  else{ 

     lr_end_transaction("code", LR_FAIL);

  }