• LoadRunner脚本开发:操作数据库(六)


    一. 步骤

    1. 下载MySQL loadrunner libraries

    http://files.cnblogs.com/files/xiaoxitest/MySQL_LoadRunner_libraries.zip

    2. 解压zip包,把其中bin、include文件夹下的文件拷贝到loadrunner的安装路径对应的文件夹中

    C:Program Files(x86)HPLoadRunnerin

    C:Program Files(x86)HPLoadRunnerinclude

    3. 准备mysql数据库信息

    新建数据库demo,字符集必须是utf8mb4,排序规则可以随意填

    使用sql语句建表 lr_user

    DROP TABLE IF EXISTS `lr_user`;
    
    CREATE TABLE `lr_user` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `mobile` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
        `pwd` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
        `dtime` datetime DEFAULT NULL,
        PRIMARY KEY(`id`)
    ) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
        

    4. 启动loadrunner,选择web-http/html协议,创建脚本

    5. 编写vuser_init的脚本

    引入头文件:#include "Ptt_Mysql.h",当然也可以在global.h文件中引入

    定义宏(也就是全局变量):#define 大写的变量名 变量值,当然也可以在global.h文件中定义。注意不要以分号结束,要放在action外

    #include "Ptt_Mysql.h"   //引用,也可以写在global.h中
    #define MYSQLSERVER "192.168.0.105"   //宏定义,定义一个全局变量
    #define MYSQLPORT "3306"
    #define MYSQLUSERNAME "root"
    #define MYSQLPASSWORD "pertest"
    #define MYSQLDB "demo"
    
    
    
    MYSQL *Mconn;    //定义数据库接收类型
    
    
    vuser_init()
    {
        lr_load_dll("libmysql.dll");   //加载文件
        Mconn = lr_mysql_connect(MYSQLSERVER, MYSQLUSERNAME, MYSQLPASSWORD, MYSQLDB, atoi(MYSQLPORT));  //创建数据库连接
        return 0;
    }

    6. 编写vuser_end的脚本

    断开数据库连接

    vuser_end()
    {
        lr_mysql_disconnect(Mconn);
        return 0;
    }

    7. 编写action的脚本

    (1) 查询脚本

    Action()
    {
        char sqlQuery[1024];  //1024是字符长度
        int count;
        
        
        //查询脚本:SQL select脚本,把脚本字符串存储到sqlQuery中
        sprintf(sqlQuery, "select * from lr_user;");
        count = lr_mysql_query(Mconn, sqlQuery);
    
            //count=0,说明执行成功了。不管表里有没有数据
        if(count == 0) {
            lr_output_message("============脚本执行成功===========");
            lr_output_message("========count=%d", count);
        } else {
            lr_output_message("============脚本执行失败===========");
        }
        
        
        return 0;
    }

    运行结果:

    Virtual User Script started at : 2020/4/3 22:30:14
    Starting action vuser_init.
    Web Turbo Replay of LoadRunner 12.0.0 for Windows 2008 R2; build 2739 (Nov 30 2014 23:13:05)      [MsgId: MMSG-27143]
    Run mode: HTML      [MsgId: MMSG-26993]
    Run-Time Settings file: "C:UsersAdministratorDocumentsVuGenScriptsWebHttpHtml6\default.cfg"      [MsgId: MMSG-27141]
    Ending action vuser_init.
    Running Vuser...
    Starting iteration 1.
    Maximum number of concurrent connections per server: 6      [MsgId: MMSG-26989]
    Starting action Action.
    Action.c(36): ============脚本执行成功===========
    Action.c(37): ========count=0
    Ending action Action.
    Ending iteration 1.
    Ending Vuser...
    Starting action vuser_end.
    Ending action vuser_end.
    Vuser Terminated.

    (2) 查询有多少条记录

    注意这里的返回结果类似于表格一行一列,里面的值为数据库的总记录条数:2,因此使用row[0][0].cell获取第一行第一列的值

      第一列
    第一行 2
    Action()
    {
        char sqlQuery[1024];  //1024是字符长度
        int count;
    
    
        //查询有多少个数据
        sprintf(sqlQuery, "select count(0) from lr_user;");
        count = lr_mysql_query(Mconn, sqlQuery);
    
        //row[列号][行号]
        lr_save_string(row[0][0].cell, "value");  
        lr_output_message(lr_eval_string("{value}"));
    
        //count=0,说明执行成功了。不管表里有没有数据
        if(count == 0) {
            lr_output_message("============脚本执行成功===========");
            lr_output_message("========count=%d", count);
        } else {
            lr_output_message("============脚本执行失败===========");
        }
        
        return 0;
    }

    返回结果

    Starting action Action.
    Action.c(30): Notify: Saving Parameter "value = 2".
    Action.c(31): Notify: Parameter Substitution: parameter "value" =  "2"
    Action.c(31): 2
    Action.c(37): ============脚本执行成功===========
    Action.c(38): ========count=0
    Ending action Action.

    (3) 查询某个固定的值

    现在数据库已经有两条数据了,如果想要查询第一行第二列的值,可以这样写。如果没有拿到值,将会返回空字符串

    Action()
    {
        char sqlQuery[1024];  //1024是字符长度
        int count;
    
        //查询脚本:SQL select脚本,把脚本字符串存储到sqlQuery中
        sprintf(sqlQuery, "select * from lr_user;");
        count = lr_mysql_query(Mconn, sqlQuery);
    
    
        //row[列号][行号]
        lr_save_string(row[1][0].cell, "value");  //第一个[]里为0时表示id,为1时表示手机号
        lr_output_message(lr_eval_string("{value}"));    
    
        //count=0,说明执行成功了。不管表里有没有数据
        if(count == 0) {
            lr_output_message("============脚本执行成功===========");
            lr_output_message("========count=%d", count);
        } else {
            lr_output_message("============脚本执行失败===========");
        }
        
        return 0;
    }

    运行结果

    Starting action Action.
    Action.c(30): Notify: Saving Parameter "value = 13524012256".
    Action.c(31): Notify: Parameter Substitution: parameter "value" =  "13524012256"
    Action.c(31): 13524012256
    Action.c(37): ============脚本执行成功===========
    Action.c(38): ========count=0
    Ending action Action.

    (4) 插入一条数据

    Action()
    {
        char sqlQuery[1024];  //1024是字符长度
        int count;
        
    
        //插入数据
        sprintf(sqlQuery, "INSERT INTO lr_user(`mobile`, `pwd`, `dtime`) VALUES ('13500023456', '123456', NOW());");
        count = lr_mysql_query(Mconn, sqlQuery);
    
        //count=0,说明执行成功了。不管表里有没有数据
        if(count == 0) {
            lr_output_message("============脚本执行成功===========");
            lr_output_message("========count=%d", count);
        } else {
            lr_output_message("============脚本执行失败===========");
        }
        
        return 0;
    }

    (5) 参数化插入数据

    Action()
    {
        char sqlQuery[1024];  //1024是字符长度
        int count;
    
    
        //参数化,插入脚本的手机号
        sprintf(sqlQuery, "INSERT INTO lr_user(`mobile`, `pwd`, `dtime`) VALUES ('%s', '123456', NOW());", lr_eval_string("135{phone}"));
        count = lr_mysql_query(Mconn, sqlQuery);
        
        //count=0,说明执行成功了。不管表里有没有数据
        if(count == 0) {
            lr_output_message("============脚本执行成功===========");
            lr_output_message("========count=%d", count);
        } else {
            lr_output_message("============脚本执行失败===========");
        }
        
        return 0;
    }    

    (6) 加密密码

    Action()
    {
        char sqlQuery[1024];  //1024是字符长度
        int count;
    
            //加密密码
        lr_save_string(Change_to_Md5("123456"), "pwd");
        sprintf(sqlQuery, "INSERT INTO lr_user(`mobile`, `pwd`, `dtime`) VALUES ('%s', '%s', NOW());", lr_eval_string("135{phone}"), lr_eval_string("{pwd}"));
        count = lr_mysql_query(Mconn, sqlQuery);
        
    
            //count=0,说明执行成功了。不管表里有没有数据
        if(count == 0) {
            lr_output_message("============脚本执行成功===========");
            lr_output_message("========count=%d", count);
        } else {
            lr_output_message("============脚本执行失败===========");
        }
        
        return 0;
    }

    (7) 批量造数据

    有两种办法,一种是按F4设置迭代次数,另一种是使用Controller设置虚拟用户数

    打开Controller的Design,分别设置Start Vusers,Duration和Stop Vusers。注意批量造数据脚本用的是上面加密密码的脚本

    运行后查看数据库的数据总数

    在Controller的导航栏Results下点击Analyze Results,可以看到结果分析

    如果想要添加图标,还可以点击Summary Report ==》Add New Item ==》Add New Graph

    (8) 删除数据 

    Action()
    {
        char sqlQuery[1024];  //1024是字符长度
        int count;
    
    
            //删除数据
        sprintf(sqlQuery, "DELETE FROM lr_user WHERE mobile = %s;",  "13508518632");
        count = lr_mysql_query(Mconn, sqlQuery);
    
    
                //count=0,说明执行成功了。不管表里有没有数据
        if(count == 0) {
            lr_output_message("============脚本执行成功===========");
            lr_output_message("========count=%d", count);
        } else {
            lr_output_message("============脚本执行失败===========");
        }
        
        return 0;
    }
  • 相关阅读:
    Python处理Excel文档(xlrd, xlwt, xlutils)
    张一鸣10年面试过2000人:混得好的年轻人都有这 5 种特质!
    PYTHON对文件及文件夹的一些操作
    ulipad 常用快捷键
    Python之re模块 —— 正则表达式操作
    Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
    python 深入理解 赋值、引用、拷贝、作用域
    Python 模块学习:re模块
    [置顶] Android资源文件分析
    Tomcat 7最大并发连接数的正确修改方法
  • 原文地址:https://www.cnblogs.com/my_captain/p/12629609.html
Copyright © 2020-2023  润新知