loadrnner添加C语言代码的几种方式

今天有人在群里问,想直接把自己的C语言代码让lr调用,该怎么搞。

这东西说来简单,只是对Loadrunner这工具不熟悉可能才会有这种问题吧。个人理解,一般有三种方法吧,废话不多,直接干货。

1.直接引用

1)写一个c代码,直接保存为c文件,代码如下。文件为sum.c

int sum(int num1,int num2)
{
     retun num1+num2;   
}

 2)打开Loadrunner 新建一个空白http协议脚本,保存为Lr_Call_cCode

3)打开该脚本的文件夹,找到“globals.h”文件,在全局变量中添加以下内容,保存。

#ifndef _GLOBALS_H
#define _GLOBALS_H
//--------------------------------------------------------------------
// Include Files
#include "lrun.h"
#include "web_api.h"
#include "lrw_custom_body.h"
#include "sum.c"  //此行为添加内容
//--------------------------------------------------------------------
// Global Variables
#endif // _GLOBALS_H

 

 4)将文件"sum.c"拷贝到脚本“Lr_Call_cCode”根目录中

5)编辑action脚本如下:

Action() 
{ 
  int *i, *j; 
  int a=50, b=50; 
  i = &a; 
  j = &b; 
  lr_message("i+j=%d",sum(i,j)); 
 return 0; 
}

 

 

 6)保存,调试脚本,通过。

Virtual User Script started at : 2016-03-28 09:13:52
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0.0 for Windows 2008 R2; build 9238 (Feb  1 2011 02:48:36)      [MsgId: MMSG-27143]
Run Mode: HTML      [MsgId: MMSG-26000]
Run-Time Settings file: "C:\test\study\Lr_Call_cCode\\default.cfg"      [MsgId: MMSG-27141]
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Action.
i+j=100
Ending action Action.
Ending iteration 1.
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.

 =============华丽的分割线==========================================================

2.第二种方法,更直接

直接在action中写代码

Action()
{
 int *i, *j;
 int a=50, b=50; 
 i = &a;
 j = &b; 
lr_message("i+j=%d",sum(i,j));
 return 0; 
}
/*直接在action函数外部写c代码*/
int sum(int num1,int num2)
{
     retun num1+num2;   
}
保存脚本,回放,结果同1.

3.通过VC++使用标准C语言编写sum.dll库

通过Loadrunner调用,官方文档说明:

int lr_load_dll( const char *library_name ); 
note:library_name  The name of a DLL (Windows) or shared object (UNIX).  
The lr_load_dll function loads a DLL (Windows) or shared object (UNIX) allowing you to call an external function when replaying using the
C interpreter. Once you load the DLL, you can call any function defined in the DLL, without having to declare it. You can specify a full path for the DLL. On Windows platforms, if you do not specify a path, lr_load_library searches for the DLL using the standard sequence used by the C++ function, LoadLibrary . On UNIX platforms, you can set the LD_LIBRARY_PATH environment variable (or the platform equivalent). The lr_load_dll function uses the same search
rules as dlopen. For more information, see the man pages for dlopen or its equivalent.

dll动态库编写使用标准c++函数,加载标准的WindowsLoadLibrary.Linux平台类似,不过大部分人用估计都是在windows平台吧。

想看案例,给个链接。http://www.cnblogs.com/coderzh/archive/2008/04/02/1135118.html

 

posted @ 2016-03-28 18:30  $椰子$  阅读(1328)  评论(0编辑  收藏  举报