运用LOADRUNNER .NET ADD-IN 写的性能测试脚本
using System;
using System.Runtime.InteropServices;
using System.Data.OleDb;
using System.Data;
namespace LoadRunnerUser1
{
/// <summary>
/// Summary description for VuserClass.
/// </summary>
[ClassInterface(ClassInterfaceType.AutoDual)]
public class VuserClass
{
LoadRunner.LrApi lr;
public VuserClass()
{
// LoadRunner Standard API Interface :: DO NOT REMOVE!!!
lr = new LoadRunner.LrApi();
}
// ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
public int Initialize()
{
// TO DO: Add virtual user's initialization routines
lr.message("Initialize部分,我只执行一次哦!");
return lr.PASS;
}
// ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
public int Actions()
{
// TO DO: Add virtual user's business process actions
lr.message("Actions部分,我可以重复执行(在设置迭代情况下)!");
try
{
//设置连接字符串开始
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection+=@"Data Source=C:\\test.mdb";
//设置连接字符串结束
//插入一个集合点开始
lr.rendezvous("集合点");
//插入一个集合点结束
//事务开始
lr.start_transaction("SQL语句性能");
//建立OleDbConnection和OleDbCommand,并指定要运行的Sql语句开始
System.Data.OleDb.OleDbConnection conn=new
System.Data.OleDb.OleDbConnection(strConnection);
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from testdb";
//建立OleDbConnection和OleDbCommand,并指定要运行的Sql语句结束
//插入一个日志开始
lr.log_message("LOG: Sql语句开始执行了,Sql="+cmd.CommandText);
//插入一个日志结束
//将查询结果填充到DataTable开始
DataTable dt = new DataTable();
System.Data.OleDb.OleDbDataAdapter oleDA = new
System.Data.OleDb.OleDbDataAdapter();
oleDA.SelectCommand = cmd;
oleDA.Fill(dt);
//将查询结果填充到DataTable结束
//插入一个日志开始
lr.log_message("LOG: Sql语句执行完成,Sql="+cmd.CommandText);
//插入一个日志结束
//取得结果集的记录数
int iCountRec=Convert.ToInt32(dt.Rows.Count.ToString());
conn.Close();//关闭连接
//如果记录数大于0,完整这个事务,否则标识事务失败
if(iCountRec>0)
lr.end_transaction("SQL语句性能",lr.PASS);
else
lr.end_transaction("SQL语句性能",lr.FAIL);
//再来一个参数化的示例开始
lr.output_message("Welcome "+lr.eval_string("<username>")+"!");
//再来一个参数化的示例结束
//Thinktime 的应用,就是模拟手工操作的延时,在这里我们延时3秒钟
lr.think_time(3);
}
catch(Exception ex)
{
conn.Close();//关闭连接
string error = ex.Message;
}
return lr.PASS;
}
// ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
public int Terminate()
{
// TO DO: Add virtual user's termination routines
lr.message("Terminate部分,我只执行一次哦!");
return lr.PASS;
}
}
}