LoadRunner參數和變量之間的轉換

這是用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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章