友好连接

2008年10月9日星期四

Code Snippets for LoadRunner

1. How to choose the option randomly?
Description:

If one option needs to be chosen randomly in below figure, what can we do in LR?
图略


Solution:

1. Use web_reg_save_param with proper LB, RB and Ord=All to get all options’ value into an array.

2. Get a random value from array.

3. In next web_sumbit_form or web_url use the value.

Code:

Action()

{

//declare variables

int flightCount;

char Flight[100];

//replace with your web_reg_save_param

web_reg_save_param("flightList",

"LB=outboundFlight value=",

"RB=>",

"Ord=All", //Make sure Ord's value is All.

LAST);

//replace with your web submit or web url

web_submit_form("reservations.pl",

"Snapshot=t4.inf",ITEMDATA,

"Name=depart", "Value=Shanghai", ENDITEM,

"Name=arrive", "Value=Beijing", ENDITEM,

LAST);

//get the count of flightList array.

flightCount=atoi( lr_eval_string("{flightList_count}") );

/*save random flight to Flight string.

1 + rand() % flightCount: Generate a Random Number

{flightList_1}: get the first value in flightList

*/

sprintf (Flight,"{flightList_%d}",1 + rand() % flightCount );

//save Flight to String RandFlight

lr_save_string( lr_eval_string (Flight),"RandFlight" );

lr_message( lr_eval_string("{RandFlight}") );

//Use RandFlight in next step

web_submit_form("reservations.pl_2",

"Snapshot=t5.inf",ITEMDATA,

"Name=outboundFlight", "Value={RandFlight}", ENDITEM,

LAST);

return 0;

}

2. How to generate a unique input?
Description:

In the below figure we need to input unique values for the Username. How can we make sure that every username is unique during thousand iterations run?

Solution:

Controller will assign a unique VuserId to every Vuser when we start to run a Vuser. The following is two solutions we can use.

1. VuserId + iteration No

2. VuserId + Current Time (If name will be created twice in one iteration)

Code:

Action()

{

//declare variables

char testname[100];

char *VuserId;

//create a VuserId type parameter named RunningVuserId in Parameter list

VuserId=lr_eval_string("{RunningVuserId}");

//The value saved in ParamTimeStamp is the number of milliseconds since midnight January // 1st, 1970.

web_save_timestamp_param("ParamTimeStamp", LAST);

//Create testname as format: VuserId_ ParamTimeStamp.

strcpy(testname,VuserId);

strcat(testname,"_");

strcat(testname,lr_eval_string("{ParamTimeStamp}"));



lr_save_string(testname,"UniqueTestname");

lr_output_message("the value is %s", lr_eval_string("{UniqueTestname}"));

return 0;

}

3. How to check for download function?
Description:

Sometimes we do stress test that download a file from server. When we record this step, our script will generate web_url when we click download link or button. But when you replay it, this web_url always will pass and no file will actually be downloaded in the controller’s location. We can use web_reg_find to check whether we load page properly. How can we check here?

Solution:

The file won't be downloaded (Physically) to the folder but you can determine whether the request was successful or not by determining the size returned by the request. You can use the function "web_get_int_property" and "HTTP_INFO_DOWNLOAD_SIZE” option in that function to determine the size.

Code:

int flen;

//your web url that download file

// get the download file's size.

flen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);

if(flen > 0)

{

lr_end_transaction("S1_D_download", LR_AUTO);

}

else

{

lr_end_transaction("S1_D_download", LR_FAIL);

}



4. How to save file when you download in LR?
Description:

In case 3, if you also want to save the file to a location, what can be done?

Solution:

We can use web_reg_save_param to save all bytes downloaded from server and then these bytes could be written to a location.

Code:

int flen;

long filedes;

int i;



web_reg_save_param("FILED","LB=","RB=","Search=Body",LAST);

//your web url that download file



// get the download file's size.

flen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);

if(flen > 0)

{

//create a new file. If you run the script in controller, you can use unique //name in Case 2 to generate a unique file.

if((filedes = fopen("c:\\test.xls", "wb")) == NULL)

{

lr_output_message("Open File Failed!");

lr_end_transaction("file_download", LR_FAIL);

}

//write the data which are got from server to the file created in your

// location.

i = fwrite(lr_eval_string("{FILED}"), flen, 1, filedes);

lr_output_message("Successfully wrote %d", i );

lr_end_transaction("file_download", LR_AUTO);

fclose(filedes);

}

else

{

lr_end_transaction("S1_D_download", LR_FAIL);

}

5. How to get a substring in LR?
Description:

If the requirement demands us to parse a string, we can use LR_SAVE_VAR.

Solution: See code

Code:

Action()

{

char *mystr = "I come from Shanghai";

lr_save_var(mystr + 12, 8, 0, "Mycity");

lr_output_message("My city is %s",lr_eval_string("{Mycity}"));

return 0;

}



6. How to reformat date?
Description:

When we get some date, but the format is not our expectation, what can we do?

Solution: See code

Code:



char *ReqDate;

lr_save_string("10/11/2006","MyDate");

lr_save_var(lr_eval_string("{MyDate}"),2,0,"Date");

lr_save_var(lr_eval_string("{MyDate}")+3,2,0,"Month");

lr_save_var(lr_eval_string("{MyDate}")+6,4,0,"Year");

ReqDate = lr_eval_string("{Month}/{Date}/{Year}");

lr_output_message("date is %s",ReqDate);

你必须学会的几个常用网络测试命令

如果你是一个网络维护人员,那么肯定要经常处理网络故障,了解和掌握下面几个命令将会有助于您更快地检测到网络故障所在,从而节省时间,提高效率。

  Ping

  Ping是测试网络联接状况以及信息包发送和接收状况非常有用的工具,是网络测试最

  常用的命令。Ping向目标主机(地址)发送一个回送请求数据包,要求目标主机收到请求后给予答复,从而判断网络的响应时间和本机是否与目标主机(地址)联通。

  如果执行Ping不成功,则可以预测故障出现在以下几个方面:网线故障,网络适配器配置不正确,IP地址不正确。如果执行Ping成功而网络仍无法使用,那么问题很可能出在网络系统的软件配置方面,Ping成功只能保证本机与目标主机间存在一条连通的物理路径。

  命令格式:

  ping IP地址或主机名 [-t] [-a] [-n count] [-l size]

  参数含义:

  -t不停地向目标主机发送数据;

  -a 以IP地址格式来显示目标主机的网络地址 ;

  -n count 指定要Ping多少次,具体次数由count来指定 ;

  -l size 指定发送到目标主机的数据包的大小。

  例如当您的机器不能访问Internet,首先您想确认是否是本地局域网的故障。假定局域网的代理服务器IP地址为202.168.0.1,您可以使用Ping避免202.168.0.1命令查看本机是否和代理服务器联通。又如,测试本机的网卡是否正确安装的常用命令是ping 127.0.0.1。

  Tracert

  Tracert命令用来显示数据包到达目标主机所经过的路径,并显示到达每个节点的时间。命令功能同Ping类似,但它所获得的信息要比Ping命令详细得多,它把数据包所走的全部路径、节点的IP以及花费的时间都显示出来。该命令比较适用于大型网络。

  命令格式:

  tracert IP地址或主机名 [-d][-h maximumhops][-j host_list] [-w timeout]

  参数含义:

  -d 不解析目标主机的名字;

  -h maximum_hops 指定搜索到目标地址的最大跳跃数;

  -j host_list 按照主机列表中的地址释放源路由;

  -w timeout 指定超时时间间隔,程序默认的时间单位是毫秒。

  例如大家想要了解自己的计算机与目标主机www.cce.com.cn之间详细的传输路径信息,可以在MS-DOS方式输入tracertwww.cce.com.cn。

  如果我们在Tracert命令后面加上一些参数,还可以检测到其他更详细的信息,例如使用参数-d,可以指定程序在跟踪主机的路径信息时,同时也解析目标主机的域名。

  Netstat

  Netstat命令可以帮助网络管理员了解网络的整体使用情况。它可以显示当前正在活动的网络连接的详细信息,例如显示网络连接、路由表和网络接口信息,可以统计目前总共有哪些网络连接正在运行。

  利用命令参数,命令可以显示所有协议的使用状态,这些协议包括TCP协议、UDP协议以及IP协议等,另外还可以选择特定的协议并查看其具体信息,还能显示所有主机的端口号以及当前主机的详细路由信息。

  命令格式:

  netstat [-r] [-s] [-n] [-a]

  参数含义:

  -r 显示本机路由表的内容;

  -s 显示每个协议的使用状态(包括TCP协议、UDP协议、IP协议);

  -n 以数字表格形式显示地址和端口;

  -a 显示所有主机的端口号。

  ipconfig

  ipconfig命令以窗口的形式显示IP协议的具体配置信息,命令可以显示网络适配器的物理地址、主机的IP地址、子网掩码以及默认网关等,还可以查看主机名、DNS服务器、节点类型等相关信息。其中网络适配器的物理地址在检测网络错误时非常有用。

  命令格式:

  ipconfig[/?] [/all]

  参数含义:

  /all 显示所有的有关IP地址的配置信息;

  /batch [file] 将命令结果写入指定文件;

  /renew_ all 重试所有网络适配器;

  /release_all 释放所有网络适配器;

  /renew N 复位网络适配器 N;

  /release N 释放网络适配器 N。

LoadRunner如何获得参数化中每个关键字的搜索响应时间

在测试搜索引擎时我们一般采用大量的搜索关键字,有时有必要了解在并发访问的情况下每个关键字的响应时间,一般如果不对脚本进行处理的话你可以获得每个关键字的响应时间但是这些时间没有跟相应的关键字对应,所以对程序的调优没有什么大的帮助,这里介绍一种方法,可以让你知道在并发访问下每个关键字的响应时间。
解决办法是可以把搜索搜索的操作定义为一个事务,事务名也用参数值代替,这样就可以从测试结果中知道该关键字的响应时间了。举例如下:
#include "web_api.h"
Action()
{
lr_start_transaction(lr_eval_string("{SearchParam}"));
web_url("s",
"URL=http://www.baidu.com/s?wd={SearchParam}",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTTP",
LAST);
lr_end_transaction(lr_eval_string("{SearchParam}"), LR_AUTO);
return 0;
}
这样就可以在场景运行时通过事务监视图中就可以看到每个关键字的所用的时间了。
这种方法的缺点是当关键字比较多时,监视器中行数会增多,可能导致Controller显示不下。
还有一种方法,示例如下:
#include "web_api.h"
Action(){
float trans_time;
lr_start_transaction("sousuo");
web_url("s",
"URL=http://www.baidu.com/s?wd={SearchParam}",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTTP",
LAST);
trans_time = lr_get_transaction_duration("sousuo") * 1000;
lr_output_message("SearchParam:%s,duration:%fms",lr_eval_string("{SearchParam}"),trans_time);
lr_end_transaction("sousuo", LR_AUTO);
return 0;
}
一般使用这种方法比较好,可以把lr_output_message函数换为自定义的函数,把结果写到一个结果文件中去,注意写入的文件最好以vuser id来命名,这样可以保证同时只有一个进程在读写这个文件(这样一来不同的用户读写不同的文件,否则如果所有用户写入同一文件在用户数多的情况下并发可能造成对同一文件的访问共享违例,获得vuser id的函数为lr_whoami)。另外需要说明的是这里获得的事务时间可能比通过lr_start_transaction和lr_end_transaction获得的事务的时间要小一些,这是由于测试工具自身的原因造成的,可以不必理会。
同时,如果想知道查询过程中哪个关键字搜索结果错误,也可以加上验证点,同时可以再加一些代码,把搜索超过一定时间的关键字过滤出来。

使用LoadRunner监控Apache的步骤

Apache上的设置

打开\conf\httpd.conf,进行如下修改:

1、 设置允许查看Apache运行状态的主机

#

# Allow server status reports, with the URL of http://servername/server-status

# Change the ".your-domain.com" to match your domain to enable.

#

#取消一下代码前面的注释符号“#”,并且设置Order(顺序)为允许优先





SetHandler server-status



Order allow,deny



Deny from nothing



Allow from all





这样改变以后重新启动Apache在浏览器中输入http://servername/server-status就可以看到Apache运行时的信息,而输入http://servername/server-status?auto就会看到如下信息:

Total Accesses: 124





Total kBytes: 444





CPULoad: 3.32432





Uptime: 37





ReqPerSec: 3.35135





BytesPerSec: 12288





BytesPerReq: 3666.58





BusyWorkers: 1





IdleWorkers: 7





Scoreboard: ____W___.........................



看到这样的信息就表示修改成功,这样就可以使用LoadRunner监视Apache了。

以下两步跟使用LoadRunner监视Apache无关,可以跳过不看。

2、 改变Apache的设置,打开详细状态开关;

#

# ExtendedStatus controls whether Apache will generate "full" status

# information (ExtendedStatus On) or just basic information (ExtendedStatus

# Off) when the "server-status" handler is called. The default is Off.

#

#取消了下面一行前面的注释符号“#”



ExtendedStatus On



3、 有用的设置,查看各模块信息

#

# Allow remote server configuration reports, with the URL of

# http://servername/server-info (requires that mod_info.c be loaded).

# Change the ".example.com" to match your domain to enable.

#

#取消一下代码前面的注释符号“#”,并且设置Order(顺序)为允许优先







SetHandler server-info



Order allow,deny



Deny from nothing



Allow from all







二、LoadRunner上的设置

经过以上第一项设置以后就可以使用LoadRunner监控Apache的运行情况了,在LoadRunner可用的监视器中双击Web Server Resource Graphs下的Apache节点,然后在右边对应的窗口中添加Apache所在主机的IP地址,并且加入计数器后单击OK,这样就可以在LoadRunner中实时显示Apache的运行状态信息了。

注意:您可能收到如下消息【Monitor name :Apache. Parsing error, cannot find token: BusyServers. Measurement: BusyServers|192.168.0.186. Hints: 1) Such a measurement does not exist, or the html page may be different from the supported one. 2) Try to replace the Apache.cfg with appropriate Apache_.cfg file in \dat\monitors and rerun the application (entry point: CApacheMeasurement::NewData). [MsgId: MMSG-47479]】,这是由于要监视Apache的版本提供的计数器与LoadRunner默认的计数器不一致导致的。此时建议先关闭Controller,打开\dat\monitors下的apache.cfg文件(其它文件名类似Apache_.cfg的是Apache监视配置的备份,只有apache.cfg是生效的):

1、 修改Counter0=IdleServers为Counter0=IdleWorkers,同时修改注释信息Label0=#Idle Servers (Apache)为Label0=#Idle Workers (Apache),描述信息也建议修改;

2、 修改Counter4=BusyServers为Counter4=BusyWorkers,同时修改注释信息Label4=#Busy Servers (Apache)为Label4=#Busy Workers (Apache) ,描述信息也建议修改。

然后保存并关闭该文件,重新打开Controller并添加计数器,这样监视就正常了。

loadrunner 8.1破解

网上已经有很多这样的文章,在这里只做备忘.
步骤:
1.安装loadrunner8.1
2.将lm70.dll、mlr5lprg.dll拷到loadrunner8.1安装目录的BIN文件夹下,如C:\Program Files\Mercury\LoadRunner\bin
3.打开loadrunner,在Configuration-Loadrunner 输入7.8、8.0通用的license有:golba-100: AEAMAUIK-YAFEKEKJJKEEA-BCJGI web-10000: AEABEXFR-YTIEKEKJJMFKEKEKWBRAUNQJU-KBYGB
重新打开loadrunner,就不会再出现试用信息了.