首页 > 代码库 > loadrunner使用socket协议来实现客户端对服务器产生压力实例。(通过发送心跳包,达到连接多个客户端的目的)
loadrunner使用socket协议来实现客户端对服务器产生压力实例。(通过发送心跳包,达到连接多个客户端的目的)
#include "lrs.h"
vuser_init()
{
char *ip;
int handler;
//编写获取LR分配的Vuser IP函数,将IP保存在ip变量中。
ip=lr_get_vuser_ip();
if(ip)
lr_vuser_status_message("the ip address is %s:",ip);
else
lr_vuser_status_message("IP spooler disabled");
//输出获取到lr分配的ip值。
lr_output_message("the current assign ip is :%s",ip);
//将ip变量保存到一个参数中,并输出该参数的值。
lr_save_string(ip,"VirtualIP");
lr_output_message( lr_eval_string("{VirtualIP}"));
//在lrs_create_socket函数中localhost使用<>:<>形式来参数化
lrs_create_socket("socket0","TCP","LocalHost=<VirtualIP>:<RandomPort>","RemoteHost=5.5.5.5:8888",LrsLastArg);
//输出socket句柄号
handler=lrs_get_socket_handler("socket0");
lr_message("The current handler of sokcet0 is: %d",handler);
lrs_startup(257);
return 0;
}
########################################################################
#include "lrs.h"
Action()
{
char *ReturnValue_Remote;
char *ReturnPort_Remote;
char *ReturnValue_Local;
char *ReturnPort_Local;
char *AcutalBuffer_Send;
int BufferSize_Send;
char *AcutalBuffer_Revc;
int BufferSize_Revc;
int flag_send;
int flag_revc;
//int match_length;
//###########获取远程主机及本地主机IP及端口号################
ReturnValue_Remote=lrs_get_socket_attrib("socket0",REMOTE_ADDRESS);
ReturnPort_Remote=lrs_get_socket_attrib("socket0",REMOTE_PORT);
ReturnValue_Local=lrs_get_socket_attrib("socket0", LOCAL_ADDRESS);
ReturnPort_Local=lrs_get_socket_attrib("socket0",LOCAL_PORT);
lr_start_transaction("获取远程主机地址及端口");
lr_output_message("this RemoteHost address is %s :", ReturnValue_Remote);
lr_output_message("this RemoteHost Port is %s : ",ReturnPort_Remote);
lr_end_transaction("获取远程主机地址及端口", LR_AUTO);
lr_start_transaction("获取本机地址及端口");
lr_output_message("this LocalHost address is %s :", ReturnValue_Local);
lr_output_message("this LocalHost Port is %s : ",ReturnPort_Local);
lr_end_transaction("获取本机地址及端口", LR_AUTO);
//##############发送心跳包##########################
lr_think_time(3);
lr_start_transaction("发送数据包");
lrs_set_send_timeout(3,0);
flag_send=lrs_send("socket0","buf0",LrsLastArg);
lr_end_transaction("发送数据包", LR_AUTO);
if (flag_send==0)
{
lr_output_message("buf0缓冲数据发送成功!");
}
else
lr_output_message("buf0缓冲数据发送超时!");
//##############获取发送BUFF数据及大小###################
lr_start_transaction("获取BUFF数据大小及内容");
lrs_get_buffer_by_name("buf0",&AcutalBuffer_Send,&BufferSize_Send);
lr_output_message("buf0 size is %d :",BufferSize_Send);
lrs_save_param_ex("socket0","user",AcutalBuffer_Send,0,BufferSize_Send,"ascii","param_send");
lr_output_message("the contect buf0 is %s :",lr_eval_string("<param_send>"));
lr_end_transaction("获取BUFF数据大小及内容", LR_AUTO);
lrs_free_buffer("AcutalBuffer_Send");
//#####################接收数据包###########################
// lrs_set_recv_timeout(15,0);
lr_start_transaction("接收数据包");
lrs_set_recv_timeout2(15,0);
flag_revc=lrs_receive("socket0","buf1",LrsLastArg);
lr_end_transaction("接收数据包", LR_AUTO);
if (flag_revc==0)
{
lr_output_message("接收数据成功!");
}
else
lr_output_message("接收数据失败!");
//#################获取接收BUFF大小及内容#######################
lr_start_transaction("获取接收数据大小及内容");
lrs_get_last_received_buffer("socket0",&AcutalBuffer_Revc,&BufferSize_Revc);
lr_output_message("the size of last buffer is %d :",BufferSize_Revc);
lrs_save_param_ex("socket0","user",AcutalBuffer_Revc,0,BufferSize_Revc,"ascii","param_revc");
lr_output_message("the contect of last received buffer is %s :",lr_eval_string("<param_revc>"));
lr_end_transaction("获取接收数据大小及内容", LR_AUTO);
if (BufferSize_Revc==11) {
lr_output_message("接收到的数据长度为心跳包长度11个字节!");
}
else
lr_output_message("心跳包长度不相等");
//lrs_free_buffer("AcutalBuffer_Revc");
if (BufferSize_Revc>1024) {
lr_message("接收过来数据过大,不为心跳包数据,故关闭SOCKET");
lrs_free_buffer("AcutalBuffer_Revc");
//lrs_close_socket("socket0");
}
lr_output_message("当前第%s次迭代已结束",lr_eval_string("<lteration>"));
lr_message("当前第%s迭代已结束,系统输出时间为:%s",lr_eval_string("<lteration>"),lr_eval_string("<localtime_now>"));
return 0;
}
##############################################
#include "lrs.h"
vuser_end()
{
lrs_disable_socket("socket0",DISABLE_SEND);
lrs_close_socket("socket0");
lrs_cleanup();
return 0;
}
##################################################
;WSRData 2 1
send buf0 11
"\x41\x45\x48\x50\x0B\x00\x00\x00\x01\xFF\xFF"
recv buf1 11
-1