LR監控tomcat伺服器
阿新 • • 發佈:2018-12-16
採用編寫VuGen指令碼訪問Tomcat的Status頁面的方式獲取效能資料(利用了關聯和lr_user_data_point函式),本質上還是使用tomcat自帶的監控頁面,只是將監控結果加到LR的analysis中。具體步驟如下:
1)首先確保Tomcat中建立了必要許可權的使用者(編輯Tomcat的\conf目錄下的tomcat-users.xml檔案),然後在LoadRunner中編寫指令碼(根據不同版本的tomcat監控結果中的監控資料項會有些差別,根據情況更改指令碼):
double atof (const char *string); extern char* strtok(char*token, const char *delimiter); CollectMetrics() { int countP, countS, countF, countR, countK; int numValues; static int loggedVersionInfo = FALSE; lr_save_string("127.0.0.1:8080", "ServerName"); web_set_max_html_param_len("65536"); web_set_user("admin","admin", "{ServerName}"); lr_start_transaction("monitor tomcat"); web_reg_save_param("JVMFreeMemory", "LB=Free memory: ", "RB= MB", "Ord=1", LAST); web_reg_save_param("JVMTotalMemory", "LB=Total memory: ", "RB= MB","Ord=1", LAST); web_reg_save_param("JVMMaxMemory", "LB=Max memory: ", "RB= MB", "Ord=1", LAST); web_reg_save_param("HTTPMaxThreads", "LB=Max threads: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPMinSpareThreads", "LB=Min spare threads: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPMaxSpareThreads", "LB=Max spare threads: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPCurrentSpareThreads", "LB=Current thread count: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPCurrentThreadBusy", "LB=Current thread busy: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPMaxProcessingTime", "LB=Max processing time: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPRequestCount", "LB=Request count: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPErrorCount", "LB=Error count: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPBytesReceived", "LB=Bytes received: ", "RB= ", "Ord=1", LAST); web_reg_save_param("HTTPBytesSent", "LB=Bytes sent: ", "RB= ", "Ord=1", LAST); web_reg_save_param("JKMaxThreads", "LB=Max threads: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKMinSpareThreads", "LB=Min spare threads: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKMaxSpareThreads", "LB=Max spare threads: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKCurrentSpareThreads", "LB=Current thread count: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKCurrentThreadBusy", "LB=Current thread busy: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKMaxProcessingTime", "LB=Max processing time: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKRequestCount", "LB=Request count: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKErrorCount", "LB=Error count: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKBytesReceived", "LB=Bytes received: ", "RB= ", "Ord=2", LAST); web_reg_save_param("JKBytesSent", "LB=Bytes sent: ", "RB= ", "Ord=2", LAST); web_reg_find("Text=/manager", LAST); web_url("status", "URL=http://{ServerName}/manager/status", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t1.inf", "Mode=HTTP", LAST); lr_end_transaction("monitor tomcat", LR_AUTO); // 寫入Tomcat JVM 度量資料 lr_user_data_point("Tomcat JVM Free memory", atof(lr_eval_string("{JVMFreeMemory}"))); lr_user_data_point("Tomcat JVM Total memory", atof(lr_eval_string("{JVMTotalMemory}"))); lr_user_data_point("Tomcat JVM Max memory", atof(lr_eval_string("{JVMMaxMemory}"))); // 寫入Tomcat web server 度量資料 lr_user_data_point("Tomcat HTTP Max threads", atof(lr_eval_string("{HTTPMaxThreads}"))); lr_user_data_point("Tomcat HTTP Min spare threads", atof(lr_eval_string("{HTTPMinSpareThreads}"))); lr_user_data_point("Tomcat HTTP Max spare threads", atof(lr_eval_string("{HTTPMaxSpareThreads}"))); lr_user_data_point("Tomcat HTTP Current spare threads", atof(lr_eval_string("{HTTPCurrentSpareThreads}"))); lr_user_data_point("Tomcat HTTP Current thread busy", atof(lr_eval_string("{HTTPCurrentThreadBusy}"))); lr_user_data_point("Tomcat HTTP Max processing time", atof(lr_eval_string("{HTTPMaxProcessingTime}"))); lr_user_data_point("Tomcat HTTP Request count", atof(lr_eval_string("{HTTPRequestCount}"))); lr_user_data_point("Tomcat HTTP Error count", atof(lr_eval_string("{HTTPErrorCount}"))); lr_user_data_point("Tomcat HTTP Bytes received", atof(lr_eval_string("{HTTPBytesReceived}"))); lr_user_data_point("Tomcat HTTP Bytes sent", atof(lr_eval_string("{HTTPBytesSent}"))); // 寫入Tomcat servlet container 度量資料 lr_user_data_point("Tomcat JK Max threads", atof(lr_eval_string("{JKMaxThreads}"))); lr_user_data_point("Tomcat JK Min spare threads", atof(lr_eval_string("{JKMinSpareThreads}"))); lr_user_data_point("Tomcat JK Max spare threads", atof(lr_eval_string("{JKMaxSpareThreads}"))); lr_user_data_point("Tomcat JK Current spare threads", atof(lr_eval_string("{JKCurrentSpareThreads}"))); lr_user_data_point("Tomcat JK Current thread busy", atof(lr_eval_string("{JKCurrentThreadBusy}"))); lr_user_data_point("Tomcat JK Max processing time", atof(lr_eval_string("{JKMaxProcessingTime}"))); lr_user_data_point("Tomcat JK Request count", atof(lr_eval_string("{JKRequestCount}"))); lr_user_data_point("Tomcat JK Error count", atof(lr_eval_string("{JKErrorCount}"))); lr_user_data_point("Tomcat JK Bytes received", atof(lr_eval_string("{JKBytesReceived}"))); lr_user_data_point("Tomcat JK Bytes sent", atof(lr_eval_string("{JKBytesSent}"))); return 0; }
2)然後在Run-time中設定資料收集的間隔:
3)最後在Controller中設定指令碼執行,這樣場景執行完畢後,即可在Analysis中新增“User Defined Data Points”檢視收集到的Tomcat效能資料:
這種收集資料的弊端是需要通過網路請求Tomcat的頁面,會對Tomcat伺服器的效能造成一定的影響,如果Tomcat出現效能瓶頸,則收集資料時也可能碰到連線錯誤,因此,最好把收集資料點的間隔設大一點,例如5秒~10秒收集一個數據點。或儘量不採用LR監控。