这是用LoadRunner自定义监控Tomcat的脚本为基础而写的脚本.阐述了参数相互之间以及参数与变量之间复制传递原理.下面的代码注释是按照自己的理解写的,正确性不一定保证.
Action() { //定义三个字符数组用于条件判断 char jFM[100]; char jTM[100]; char jMM[100]; //必须要预先声明数据转换函数,否则得到的监控结果不正确. double atof(const char *string); //以下三个web_reg_save_param负责从Tomcat中抓取监控数据 web_reg_save_param("JVM_Free_Memory", "LB=Free memory:", "RB=MB", "ORD=1", LAST); web_reg_save_param("JVM_Total_Memory", "LB=Total memory:", "RB=MB", "ORD=1", LAST); web_reg_save_param("JVM_Max_Memory", "LB=Max memory:", "RB=MB", "ORD=1", LAST); //设定监控事务 lr_start_transaction("Status"); //登陆Tomcat web_set_user("admin", "admin", "localhost:8080"); //Tomcat监控URL web_url("status", "URL=http://localhost:8080/manager/status", "Resource=0", "RecContentType=text/html", "Snapshot=t1.inf", "Mode=HTML", LAST); lr_end_transaction("Status", LR_PASS); //通过用户自定义监控Tomcat_JVM的使用情况 //lr_user_data_point("JVM Free Memory", atof(lr_eval_string("{JVM_Free_Memory}"))); //lr_user_data_point("JVM Total Memory", atof(lr_eval_string("{JVM_Total_Memory}"))); //lr_user_data_point("JVM Max Memory", atof(lr_eval_string("{JVM_Max_Memory}"))); lr_output_message("**********************************"); //打印监控值 lr_output_message(lr_eval_string("{JVM_Free_Memory}")); lr_output_message(lr_eval_string("{JVM_Total_Memory}")); lr_output_message(lr_eval_string("{JVM_Max_Memory}")); lr_output_message("**********************************"); //将参数的值保存在另外一个参数中(其实从运行原理上说,类似于C++中的引用) lr_save_string(lr_eval_string("{JVM_Free_Memory}"), "JFreeMem"); lr_save_string(lr_eval_string("{JVM_Total_Memory}"), "JTotalMem"); lr_save_string(lr_eval_string("{JVM_Max_Memory}"), "JMaxMem"); lr_output_message("**********************************"); //打印"引用"中的值 lr_output_message(lr_eval_string("{JFreeMem}")); lr_output_message(lr_eval_string("{JTotalMem}")); lr_output_message(lr_eval_string("{JMaxMem}")); lr_output_message("**********************************"); //将参数值赋给变量(字符串数组) strcpy(jFM, lr_eval_string("{JVM_Free_Memory}")); strcpy(jTM, lr_eval_string("{JVM_Total_Memory}")); strcpy(jMM, lr_eval_string("{JVM_Max_Memory}")); //进行逻辑判断 if (strcmp(jFM, "") == 0 && strcmp(jTM, "") == 0 && strcmp(jMM, "") == 0) { lr_output_message("%s", "无参数"); } else { lr_output_message("%s", "有参数"); } //输出实际值 lr_output_message("**********************************"); lr_output_message("%s", jFM); lr_output_message("%s", jTM); lr_output_message("%s", jMM); lr_output_message("**********************************"); //将变量值保存在另外一个参数中(其实从运行原理上说,类似于C++中的引用) lr_save_string(jFM, "JFreeMem"); lr_save_string(jTM, "JTotalMem"); lr_save_string(jMM, "JMaxMem"); lr_output_message("**********************************"); return 0; }