LoadRunner參數和變量之間的轉換

這是用LoadRunner自定義監控Tomcat的腳本爲基礎而寫的腳本.闡述了參數相互之間以及參數與變量之間複製傳遞原理.下面的代碼註釋是按照本身的理解寫的,正確性不必定保證.html

 

[cpp]  view plain copy
 
  1. Action()  
  2. {  
  3.     //定義三個字符數組用於條件判斷  
  4.     char jFM[100];  
  5.     char jTM[100];  
  6.     char jMM[100];  
  7.   
  8.     //必需要預先聲明數據轉換函數,不然獲得的監控結果不正確.  
  9.     double atof(const char *string);  
  10.   
  11.     //如下三個web_reg_save_param負責從Tomcat中抓取監控數據  
  12.     web_reg_save_param("JVM_Free_Memory",  
  13.                 "LB=Free memory:",  
  14.                 "RB=MB",  
  15.                 "ORD=1",  
  16.                 LAST);  
  17.     web_reg_save_param("JVM_Total_Memory",  
  18.                 "LB=Total memory:",  
  19.                 "RB=MB",  
  20.                 "ORD=1",  
  21.                 LAST);  
  22.     web_reg_save_param("JVM_Max_Memory",  
  23.                 "LB=Max memory:",  
  24.                 "RB=MB",  
  25.                 "ORD=1",  
  26.                 LAST);  
  27.   
  28.     //設定監控事務  
  29.         lr_start_transaction("Status");  
  30.     //登錄Tomcat  
  31.     web_set_user("admin", "admin", "localhost:8080");  
  32.     //Tomcat監控URL  
  33.     web_url("status",  
  34.             "URL=http://localhost:8080/manager/status",  
  35.             "Resource=0",  
  36.             "RecContentType=text/html",  
  37.             "Snapshot=t1.inf",  
  38.             "Mode=HTML",  
  39.             LAST);  
  40.   
  41.         lr_end_transaction("Status", LR_PASS);  
  42.     //經過用戶自定義監控Tomcat_JVM的使用狀況  
  43.     //lr_user_data_point("JVM Free Memory", atof(lr_eval_string("{JVM_Free_Memory}")));  
  44.     //lr_user_data_point("JVM Total Memory", atof(lr_eval_string("{JVM_Total_Memory}")));  
  45.     //lr_user_data_point("JVM Max Memory", atof(lr_eval_string("{JVM_Max_Memory}")));  
  46.   
  47.     lr_output_message("**********************************");  
  48.   
  49.     //打印監控值  
  50.         lr_output_message(lr_eval_string("{JVM_Free_Memory}"));  
  51.         lr_output_message(lr_eval_string("{JVM_Total_Memory}"));  
  52.         lr_output_message(lr_eval_string("{JVM_Max_Memory}"));  
  53.   
  54.     lr_output_message("**********************************");  
  55.   
  56.     //將參數的值保存在另一個參數中(其實從運行原理上說,相似於C++中的引用)  
  57.     lr_save_string(lr_eval_string("{JVM_Free_Memory}"), "JFreeMem");  
  58.     lr_save_string(lr_eval_string("{JVM_Total_Memory}"), "JTotalMem");  
  59.     lr_save_string(lr_eval_string("{JVM_Max_Memory}"), "JMaxMem");  
  60.   
  61.     lr_output_message("**********************************");  
  62.   
  63.     //打印"引用"中的值  
  64.     lr_output_message(lr_eval_string("{JFreeMem}"));  
  65.     lr_output_message(lr_eval_string("{JTotalMem}"));  
  66.     lr_output_message(lr_eval_string("{JMaxMem}"));  
  67.   
  68.     lr_output_message("**********************************");  
  69.   
  70.     //將參數值賦給變量(字符串數組)  
  71.     strcpy(jFM, lr_eval_string("{JVM_Free_Memory}"));  
  72.     strcpy(jTM, lr_eval_string("{JVM_Total_Memory}"));  
  73.     strcpy(jMM, lr_eval_string("{JVM_Max_Memory}"));  
  74.   
  75.     //進行邏輯判斷  
  76.     if (strcmp(jFM, "") == 0 && strcmp(jTM, "") == 0 && strcmp(jMM, "") == 0) {  
  77.         lr_output_message("%s", "無參數");  
  78.     } else {  
  79.         lr_output_message("%s", "有參數");  
  80.     }  
  81.       
  82.     //輸出實際值  
  83.     lr_output_message("**********************************");  
  84.     lr_output_message("%s", jFM);  
  85.     lr_output_message("%s", jTM);  
  86.     lr_output_message("%s", jMM);  
  87.     lr_output_message("**********************************");  
  88.   
  89.     //將變量值保存在另一個參數中(其實從運行原理上說,相似於C++中的引用)  
  90.     lr_save_string(jFM, "JFreeMem");  
  91.     lr_save_string(jTM, "JTotalMem");  
  92.     lr_save_string(jMM, "JMaxMem");  
  93.     lr_output_message("**********************************");  
  94.   
  95.     return 0;  
  96. }  
相關文章
相關標籤/搜索