使用loadrunner进行压力测试主要分两步,第一步是编写脚本(比较重点),第二步执行测试(配置都是在界面上点点就行了,当然我只的是比较简单的,能满足日常需要的),第三步分析结果(这一步比较高深,但是对于我来说能看懂简单的性能指标就OK了)。所以这里就说一下脚本的编写,给未来再用到做参考。 1.HTTP的GET请求 这里以访问百度为例,地址http://www.baidu.com/s?wd=mobile,表示在百度上搜索mobile。具体脚本如下(有注释)
Lr script代码
Action()
{
int status;
lr_start_transaction("send");
web_reg_find("Search=Body",//这里说明在Body的范围内查找
"SaveCount=ret_Count",//这里表示把返回值的个数放在变量ret_Count里
"Text=mobile",//这里表示查找的内容是“mobile”
LAST);
status=web_url("Baidu_Search",
"URL= http://www.baidu.com/s?wd=mobile",
"TargetFrame=Main",
"Resource=0",
"RecContentType=text/html",
"Mode=http",
LAST );
lr_output_message("Request Status:%d",status);
lr_output_message("查找到的返回值个数:%d",atoi(lr_eval_string("{ret_Count}")));
if (atoi(lr_eval_string("{ret_Count}")) > 0){//这里判断检查到的个数
lr_output_message("Rec successful.");
lr_end_transaction("send", LR_PASS);
}
else{
lr_error_message("Rec failed");
lr_end_transaction("send", LR_FAIL);
}
return 0;
}
Lr script代码
Action()
{
int status;
lr_start_transaction("send");
web_reg_find("Search=Body",//这里说明在Body的范围内查找
"SaveCount=ret_Count",//这里表示把返回值的个数放在变量ret_Count里
"Text=t",//这里表示查找的内容是“t”
LAST);
status=web_submit_data("trs",
"Action=http://192.168.77.185:9002/recognizeText",//地址
"Method=POST",//POST请求
"RecContentType=text/html",
"Mode=HTML",
ITEMDATA,
"Name=usercontent","Value=gprs",ENDITEM,//这一行表示传入一个参数usercontent,值为gprs
"Name=Accept","Value=text/plain",ENDITEM,
LAST);
lr_output_message("Request Status:%d",status);
if (atoi(lr_eval_string("{ret_Count}")) > 0){//这里判断检查到的个数
lr_output_message("Rec successful.");
lr_end_transaction("send", LR_PASS);
}
else{
lr_error_message("Rec failed");
lr_end_transaction("send", LR_FAIL);
}
return 0;
}
Lr script代码
Action()
{
int status;
lr_start_transaction("send");
status=web_service_call( "StepName=getSupportCity_102",
"SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getSupportCity",//这里是我已经引用了webservice的名称及调用方法
"ResponseParam=response",
"Service=WeatherWebService",
"ExpectedResponse=SoapResult",
"Snapshot=t1353067092.inf",
BEGIN_ARGUMENTS,
"byProvinceName=安徽",//这里是入参,参数名称:byProvinceName,值:安徽。入参和返回值的名称都可以再引用里看见
END_ARGUMENTS,
BEGIN_RESULT,
"getSupportCityResult=result",//这里是返回值,名称:getSupportCityResult,把它放到变量result中
END_RESULT,
LAST);
lr_output_message("Request Status:%d",status);
lr_output_message("Result:%s",lr_eval_string("{result}"));//这里把返回值输出,调试webservice的时候用
if(strstr(lr_eval_string("{result}"),"合肥")>0){//这里是判断返回值中是否包含“合肥”
lr_end_transaction("send",LR_PASS);
}else{
lr_end_transaction("send",LR_AUTO);
}
return 0;
}
Lr script代码
#include "lrs.h"
Action()
{
char *recvbuf;
int recvlen=0;
int rc;
lrs_startup(257);
lr_start_transaction("Trans_1");
lr_start_transaction("Conn_1");
rc=lrs_create_socket("socket0", "TCP", "RemoteHost=192.168.1.101:8888", LrsLastArg);//创建Socket连接
if (rc != 0 ) {
lr_end_transaction("Conn_1", LR_FAIL);
lr_end_transaction ("Trans_1", LR_FAIL);
return 0;
}
lr_end_transaction("Conn_1", LR_PASS); //判断socket是否链接成功的事务,0表示创建成功
lrs_send("socket0", "buf0", LrsLastArg); //发送buf0,buf0为在data.ws中定义的发送变量
lrs_receive("socket0", "buf1", LrsLastArg); //接收消息,存放在buf1中,buf1是在data.ws中定义的接收数组,注意数组长度一定要大于等于实际接收长度
lrs_get_last_received_buffer("socket0",&recvbuf,&recvlen);//把Socket最后接收的字节数组,长度放在recvlen中,内容放在recvbuf中
lr_output_message("Received:%s",lr_eval_string(recvbuf));
if(recvlen>3)
lr_end_transaction("Trans_1", LR_PASS);
else
lr_end_transaction ("Trans_1", LR_FAIL);
lrs_disable_socket("socket0", DISABLE_SEND_RECV);
lrs_close_socket("socket0");
return 0;
}
Lr script代码
//data.ws
;WSRData 2 1
send buf0 32
"hehehehe"
recv buf1 50
-1
原链接:http://blog.csdn.net/zljjava/article/details/8401351