"LoadRunner脚本的组成部分包括vuser_init()、Action()和vuser_end(),这些函数在任何Vuser脚本中都是基础结构。vuser_init()用于虚拟用户的初始化,例如登录和分配内存,仅执行一次。Action()包含了主要的业务逻辑,可以设置迭代循环,重复执行。vuser_end()则是清理工作,如登出和释放内存,同样只执行一次。在脚本编辑框中,Action的内容是主要关注的部分,协议相关的函数(如web_url)会被调用执行。LoadRunner脚本可以使用三种类型的函数:LR通用函数、特定协议函数和语言相关函数。通用VU函数,如LR开头的函数,提供事务控制、命令行分析等功能,帮助构建和控制脚本的执行。"
在LoadRunner中,脚本编写是性能测试的核心环节。Vuser_init()、Action()和vuser_end()这三个框架函数构成了一个完整的Vuser脚本生命周期。
1. **vuser_init()**:这是每个脚本开始执行时首先调用的函数,通常用于进行环境设置和初始化操作。例如,模拟用户登录、分配必要的内存资源等。在这个阶段,Controller的Vuser状态会显示为“initialize”。因为只运行一次,所以初始化操作的效率和准确性至关重要。
2. **Action()**:这个函数包含实际的业务逻辑,模拟用户在应用中的操作。Action可以被设置为在循环中重复执行,以模拟多个用户并发执行相同或不同的任务。这允许测试人员模拟负载和压力场景,对系统进行性能评估。
3. **vuser_end()**:与vuser_init()相对应,vuser_end()负责清理工作,如登出用户、释放内存等。确保在脚本执行完毕后,资源得到正确释放,避免内存泄漏。
除了这三个基础框架函数,LoadRunner还提供了丰富的内置函数供测试人员使用:
- **通用VU函数**:这些函数是基于C语言的,适用于所有类型的Vuser脚本,如LR_start_transaction用于开始一个事务,LR_end_transaction用于结束事务,以及其他如日志记录、系统信息获取等功能。
- **协议相关函数**:根据使用的协议(如WebHTTP),有特定的函数如web_url用于发送HTTP请求,web_link用于点击链接,以及web_list等其他功能。
- **语言相关函数**:由于LoadRunner脚本可以使用C语言编写,因此可以利用C标准库中的函数,或者通过DLL动态加载其他功能。
理解并熟练运用这些函数是创建高效LoadRunner脚本的关键。通过合理地组织和调用这些函数,测试人员能够精确模拟用户行为,评估系统在高负载下的性能表现,发现潜在的问题,并优化应用程序的性能。