loadrunner 编写socket脚本实例(附服务端实现)
Posted wenchunl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了loadrunner 编写socket脚本实例(附服务端实现)相关的知识,希望对你有一定的参考价值。
一、socket背景知识
这个咱就不废话了,网上一搜一大堆
二、本实例实现的功能
服务端接收客户端发送的字符串,并返回"5678succ"共8个字符
三、服务端实现(java代码)
①MySocketServer.java
package serverSocketMultiThreadVer;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MySocketServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
//创建一个服务器端socket,指定绑定的端口,并监听此端口
//ServerSocket serverSocket=new ServerSocket(8820);
@SuppressWarnings("resource")
ServerSocket serverSocket=new ServerSocket(8820, 100, InetAddress.getByName("9.111.42.204"));
Socket socket=null;
int count=0;
System.out.println("***服务器即将启动,等待客户端的链接***");
//循环监听等待客户端的链接
while(true)
{
//调用accept()方法开始监听
socket=serverSocket.accept();
//创建一个新的线程
ServerThread serverThread=new ServerThread(socket);
//启动线程
serverThread.start();
count++;//统计客户端的数量
System.out.println("客户端第:"+count+"次访问");
InetAddress address=socket.getInetAddress();
System.out.println("当前客户端的IP:"+address.getHostAddress());
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
②ServerThread.java
package serverSocketMultiThreadVer;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread extends Thread {
// 和本线程相关的Socket
Socket socket = null;
public ServerThread(Socket socket) {
this.socket = socket;
}
//线程执行的操作,响应客户端的请求
public void run(){
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os=null;
PrintWriter pw=null;
DataOutputStream out1=null;
byte[] mybyte=new byte[8];
try {
//获取输入流,并读取客户端信息
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
out1=new DataOutputStream(socket.getOutputStream());
String info=null;
while((info=br.readLine())!=null){//循环读取客户端的信息
System.out.println("来自客户端的消息:"+info);
}
socket.shutdownInput();
//out1.writeUTF("1234"); 这种方法返回长度是实际字符的2倍。会增加客户端的处理难度
mybyte[0]=53;
mybyte[1]=54;
mybyte[2]=55;
mybyte[3]=56;
mybyte[4]=‘s‘;
mybyte[5]=‘u‘;
mybyte[6]=‘c‘;
mybyte[7]=‘c‘;
//out1.writeChars("5678");
out1.write(mybyte);//最好的方式是使用字节数组,否则调用该socket服务端的客户端程序有可能不能正常解析
out1.flush();
socket.shutdownOutput();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭资源
try {
if(out1!=null)
out1.close();
if(pw!=null)
pw.close();
if(os!=null)
os.close();
if(br!=null)
br.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、客户端实现(loadrunner脚本)
①Action.c
#include "lrs.h"
Action()
{
char *Data;
int Size=0;
int rc=0;
//返回报文是否成功,判断值
int msgOk=-1;
char *position="";
//返回报文是否成功标识
char *passMsg="succ"; //需和服务端进行确认是否一定返回该字符串
int receive_code;
memset(&Data,0,8);
//建立socket
rc=lrs_create_socket("socket0","TCP","RemoteHost=9.111.42.204:8820",LrsLastArg);
if (0==rc) {
lr_output_message("Socket was successfully created ");
}
else
{
lr_output_message("An error occurred while creating the socket, Error Code: %d",rc);
}
//开始事务
lr_start_transaction("socket_trans");
//发送数据
lrs_send("socket0","buf0",LrsLastArg);
//结束发送
lrs_disable_socket("socket0", DISABLE_SEND);
//接收服务端发回的数据
receive_code=lrs_receive("socket0","buf1",LrsLastArg);
//获取最后返回的缓冲区的数据及其长度
lrs_get_last_received_buffer("socket0",&Data,&Size);
//获取passMsg在返回数据的位置
position=(char *)strstr(Data,passMsg);
msgOk=(int)(position-Data+1); //数组下标从0开始,因此加1以符合使用习惯
//保存参数
lrs_save_param_ex("socket0","user",Data,0,8,"ascii","new_parameter");
lrs_free_buffer(Data);
lr_output_message("The result‘s size is: %d.",Size);
lr_output_message("The receive code is: %d.",receive_code);
lr_output_message("The data socket server give is: %s",lr_eval_string("<new_parameter>"));
//根据msgOk的值,判断事务是否成功
if (msgOk>0) {
lr_end_transaction("socket_trans",LR_PASS);
}
else
{
lr_end_transaction("socket_trans",LR_FAIL);
}
//关闭打开的socket
lrs_close_socket("socket0");
return 0;
}
②data.ws
;WSRData 2 1
send buf0 10
"<NewParam>"
recv buf1 8
-1
以上是关于loadrunner 编写socket脚本实例(附服务端实现)的主要内容,如果未能解决你的问题,请参考以下文章
用loadrunner做性能测试,通过socket需要把一个xml报文,原文直接发送出去,脚本怎么编写