loadRunner_windows Sockers 关联
1、LSP就是lrs_save_param()函数与lrs_save_searched_string()函数
以下我们用一个例子去说明他们的使用.
C/S机制和B/S不一样,特别是有一个data.ws里面的数据就是一些向服务端发的数据包和服务端返回的数据包以下就是我录制完以后的data.ws内容;
;WSRData 2 1
send buf0 55
"4##SELECT agent_name FROM AGENTS ORDER BY agent_name###"
recv buf1 55
"0##Alex#Amanda#Debby#Julia#Mary#Robert#Sharon#Suzan###"
"\x00"
send buf2 68
"2##1## SELECT DISTINCT departure FROM Flights ORDER BY departure ###"
recv buf3 56
"0##Denver#Los Angeles#Portland#San Francisco#Seattle###"
"\x00"
send buf4 298
"2##0##SELECT departure, flight_number, departure_initials, day_of_week, ar"
"rival_initials, arrival, departure_time, arrival_time, airlines, seats_ava"
"ilable, ticket_price, mileage FROM Flights WHERE arrival = 'Portland' A"
"ND departure = 'Denver' AND day_of_week = 'Friday'ORDER BY flight_number #"
"##"
recv buf5 128
"0##5787;250;5587;03:11 PM;DEN;Friday;POR;04:52 PM;NW;164;Denver#6242;250;6"
"042;08:00 AM;DEN;Friday;POR;11:00 AM;AA;177;Denver###"
"\x00"
send buf6 82
"11##UPDATE Counters SET counter_value=counter_value+1 WHERE table_name='OR"
"DERS'###"
recv buf7 8
"0##1###"
"\x00"
send buf8 67
"12##SELECT counter_value FROM Counters WHERE table_name='ORDERS'###"
recv buf9 10
"0##101###"
"\x00"
send buf10 72
"12##SELECT customer_no FROM Customers WHERE customer_name='<Customer>'###"
recv buf11 9
"0##-1###"
"\x00"
send buf12 85
"11##UPDATE Counters SET counter_value=counter_value+1 WHERE table_name='CU"
"STOMERS'###"
recv buf13 8
"0##1###"
"\x00"
send buf14 70
"12##SELECT counter_value FROM Counters WHERE table_name='CUSTOMERS'###"
recv buf15 9
"0##31###"
"\x00"
send buf16 81
"11##INSERT INTO Customers (customer_name,customer_no) VALUES ('<Customer>',"
" <userid>)###"
recv buf17 8
"0##1###"
"\x00"
send buf18 58
"12##SELECT agent_no FROM Agents WHERE agent_name='<agent>'###"
recv buf19 8
"0##4###"
"\x00"
send buf20 195
"11##INSERT INTO Orders (order_number,agent_no,customer_no,flight_number,de"
"parture_date,tickets_ordered,class,send_signature_with_order) VALUES (<orderno>,"
" <agentid>, <userid>, 6242, {d '2011-11-11'}, 1, '3', 'N')###"
recv buf21 8
"0##1###"
"\x00"
send buf22 13
"11##COMMIT###"
recv buf23 8
"0##0###"
"\x00"
-1
//下面就是我们的ACTION里面的脚本了
#include "lrs.h"
Action()
{
int random;
lrs_create_socket("socket0", "TCP", "RemoteHost=172.16.2.9:3456", LrsLastArg);
lrs_send("socket0", "buf0", LrsLastArg); //返回agent name
lrs_receive("socket0", "buf1", LrsLastArg);
srand(time(NULL));
random=rand() % 8 + 2;
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
lr_think_time(12);
lrs_send("socket0", "buf2", LrsLastArg); //返回航班起始地和目的地
lrs_receive("socket0", "buf3", LrsLastArg);
lr_think_time(13);
lrs_send("socket0", "buf4", LrsLastArg); //查询航班信息
lrs_receive("socket0", "buf5", LrsLastArg);
lr_think_time(18);
lrs_send("socket0", "buf6", LrsLastArg); //生成订单号
lrs_receive("socket0", "buf7", LrsLastArg);
lrs_send("socket0", "buf8", LrsLastArg); //返回订单号码
lrs_receive("socket0", "buf9", LrsLastArg);
lrs_save_param("socket0", NULL, "orderno", 3, 3);//利用关联返回订单号--------
lrs_send("socket0", "buf10", LrsLastArg); //查找是否是新用户,新用户返回-1,否则返回客户编号
lrs_receive("socket0", "buf11", LrsLastArg);
lrs_save_param("socket0", NULL, "userid", 3, 2); ///======
if (strcmp(lr_eval_string("<userid>"),"-1")==0)
{
lrs_send("socket0", "buf12", LrsLastArg);//生成客户编号
lrs_receive("socket0", "buf13", LrsLastArg);
lrs_send("socket0", "buf14", LrsLastArg);//返回客户编号
lrs_receive("socket0", "buf15", LrsLastArg);
lrs_save_param("socket0", NULL, "userid", 3, 2);
lrs_send("socket0", "buf16", LrsLastArg);//插入客户记录
lrs_receive("socket0", "buf17", LrsLastArg);
};
lrs_send("socket0", "buf18", LrsLastArg); //返回agent id
lrs_receive("socket0", "buf19", LrsLastArg);
lrs_save_param("socket0", NULL, "agentid", 3, 1);
lrs_send("socket0", "buf20", LrsLastArg); //插入订单记录
lrs_receive("socket0", "buf21", LrsLastArg);
lrs_send("socket0", "buf22", LrsLastArg); //提交
lrs_receive("socket0", "buf23", LrsLastArg);
lr_think_time(7);
return 0;
}
srand(time(NULL));
random=rand() % 8 + 2;
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
//我们从这段代码开始分析。
srand(time(NULL));
random=rand() % 9 + 2;
//这个随机数的作用是为了
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
//函数从
recv buf1 55
"0##Alex#Amanda#Debby#Julia#Mary#Robert#Sharon#Suzan###"
"\x00"
//中随机获得一个关联数据,为什么是数据说的范围是2-9呢,细看一下
##Alex#Amanda#Debby#Julia#Mary#Robert#Sharon#Suzan###
就知道以左边界和右边界为#的是有11对,不过只有第二开始到第九个才是对我们有用的。
所以我们这样取值。从上面我们也可以看出WINSOCKET协议里面使用的关联和HTTP/HTML协议里面的关联函数是有区别的,区别是http的协议里面关联函数的位置一定要在打开的页面的前面,在Winsocket里面是在后面。
所以执行完以下脚本
srand(time(NULL));
random=rand() % 8 + 2;
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
agent就会随机得到Alex、Amanda、Debby、Julia、Mary、Robert、Sharon、Suzan其中的一个。
我再来分析
lrs_save_param("socket0", NULL, "orderno", 3, 3);
这段代码是data.ws包里面提取所以要的数据
recv buf9 10
"0##101###"
"\x00"
从以下的范例
lrs_save_param( char *s_desc,char *buf_desc, char *param_name, intoffset, intparam_len);
可以看出
lrs_save_param("socket0", NULL, "orderno", 3, 3);
是从"0##101###"的第3位开始取值,取3位,所以
Orderon=101
再来说一说这两个函数的一个大家不太了解的地方,然后就结束本章。
lrs_save_searched_string("socket0", NULL, "agent", "LB=#", "RB=#", random,0,-1);
lrs_save_param("socket0", NULL, "orderno", 3, 3);
其中我们都发现有一个NULL,NULL的作用就是在刚从服务端里面的数据中取值,
lrs_send("socket0", "buf8", LrsLastArg); //返回订单号码
lrs_receive("socket0", "buf9", LrsLastArg);
lrs_save_param("socket0", NULL, "orderno", 3, 3);
如果写NULL,我们要从buf9中取值的话,关联函数就得在
lrs_receive("socket0", "buf9", LrsLastArg);
的后面
如果
lrs_receive("socket0", "buf10", LrsLastArg);
lrs_save_param("socket0", NULL, "orderno", 3, 3);
就是在duf10中取值。
还有一种情况如果在
lrs_receive("socket0", "buf10", LrsLastArg);
后面取duf9的值就必须这么写
lrs_save_param("socket0","buf9", "orderno", 3, 3);
例如:
Action()
{
//存放通信返回报文
char * ActualBuffer=NULL;
//存放返回报文长度,切记附初值
int numberOfResponse = -1;
//链接是否创建成功,判断值
int rc = 0;
//返回报文是否成功,判断值
int msgOk=-1;
//存放返回报文
char * position="";
//返回报文是否成功标识
char * passMsg="<RetCode>000000</RetCode>";
lr_start_transaction("连接");
//--------------创建连接-----------------
rc = lrs_create_socket("socket0", "TCP", "RemoteHost=IP地址", LrsLastArg);
if(rc==0)
{
lr_output_message("Socket was successfully created");
}
else
{
lr_output_message("An error occurred while creating the socket,Error code:%d",rc);
}
//--------------创建连接-----------------
lr_end_transaction("连接", LR_AUTO);
lr_start_transaction("农行发送数据成功");
lrs_send("socket0", "buf0", LrsLastArg);
//往“socket0”发送"buf0"
lrs_receive("socket0", "buf1", LrsLastArg);
//获取套接字上接收到的最后的缓冲区及其大小
lrs_get_last_received_buffer("socket0",&ActualBuffer,&numberOfResponse);
if(strstr(ActualBuffer, passMsg)!=NULL)
{
// <SerialNo>11111339660000054316</SerialNo>\r\n"
// <PolicyNo>00010682927108088</PolicyNo>\r\n"
lrs_save_searched_string("socket0", NULL, "serialNumber", "LB=<SerialNo>", "RB=</SerialNo>",1,0,-1);
lr_output_message( "We are on iteration=======成功流水号======= #%s", lr_eval_string( "{serialNumber}" ) );
lrs_save_searched_string("socket0", NULL, "PolicyNumber", "LB=<PolicyNo>", "RB=</PolicyNo>",1,0,-1);
lr_output_message("保单号码是:%s",lr_eval_string("{PolicyNumber}"));
lr_end_transaction("农行发送数据成功", LR_PASS);
lr_output_message("交易号码是:11111339660000%s", lr_eval_string( "{HHMMSS}" ));
lr_output_message("发送数据:%s",ActualBuffer);
}
else
{
lrs_save_searched_string("socket0", NULL, "serialNumber", "LB=<InsuSerial>", "RB=</InsuSerial>",1,0,-1);
lr_output_message( "We are on iteration=======失败流水号======= #%s", lr_eval_string( "{serialNumber}" ) );
lrs_save_searched_string("socket0", NULL, "PolicyNumber", "LB=<PolicyNo>", "RB=</PolicyNo>",1,0,-1);
lr_output_message("保单号码是:%s",lr_eval_string("{PolicyNumber}"));
lr_end_transaction("农行发送数据成功", LR_FAIL);
lr_output_message("交易号码是:11111339660000%s", lr_eval_string( "{HHMMSS}" ));
lr_error_message("发送数据: %s",ActualBuffer);
}
//关闭连接
//--------------断开socket--------------
lrs_disable_socket("socket0", DISABLE_SEND_RECV);
//--------------关闭socket--------------
lrs_close_socket("socket0");
return 0;
}
经过调试,这样关联是可以输出结果;