LR十五:Query类型_JDBC的方法_JAVA方法_Loadrunner脚本

数据库查询压力测试脚本

jdbc_java_查询类型接口测试

 1 package com.test;
 2 
 3 
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.PreparedStatement;
 7 import java.sql.ResultSet;
 8 
 9 public class Jdbc_Query {    
10     // 定义全局变量
11     String username = "root";
12     String password = "123456";
13     String url = "jdbc:mysql://localhost:3306/oa?useUnicode=true&characterEncoding=utf8";
14     String sql = "select * from itcast_role where id = ?";
15     PreparedStatement ps;
16     Connection conn;
17     public int init() throws Throwable {
18         //1、注册mysql驱动
19         Class.forName("com.mysql.jdbc.Driver");
20         //2、通过url、用户名、密码建立mysql连接,返回一个Connection对象
21         conn = DriverManager.getConnection(url, username, password);
22         //3、对sql进行预编译,返回一个已经编译过的sql对象PreparedStatement
23         ps = conn.prepareStatement(sql);
24         return 0;
25     }//end of init
26 
27     public int action() throws Throwable {
28                 
29         //4、执行sql语句之前,先对sql中不确定的数据进行赋值
30         ps.setInt(1, 1);
31         //5、执行sql语句,返回受影响的行数      
32         //(executeUpdate 增删改)(executeQuery 查询单独用这个,返回结果集的对象,这个对象叫ResultSet,然后取个变量名叫set)
33         ResultSet set = ps.executeQuery();
34         // 只有结果集中有数据,就认为查询成功 set.next()       next方法
35         while (set.next()){
36             String name = set.getString("name");
37             String desc = set.getString("description");
38             System.out.println("name"+name + ",desc"+desc);
39         }
40         return 0;
41     }
42 
43 
44     public int end() throws Throwable {
45         //6、关闭mysql连接
46         conn.close();
47         return 0;
48     }//end of end
49     
50     //执行前需要申明一下 谁调用谁处理
51     public static void main(String[] args) throws Throwable {
52         // 模拟lr一下执行
53         Jdbc_Query Query = new Jdbc_Query();
54         Query.init();
55         Query.action();
56         Query.end();
57 
58     }
59 
60 }
jdbc_java_query

 loadrunner_jdbc_查询的方法

 1 /*
 2  * LoadRunner Java script. (Build: _build_number_)
 3  * 
 4  * Script Description: 
 5  *                     
 6  */
 7 
 8 import lrapi.lr;
 9 import java.sql.Connection;
10 import java.sql.DriverManager;
11 import java.sql.PreparedStatement;
12 import java.sql.ResultSet;
13 
14 
15 public class Actions
16 {
17 
18     // 定义全局变量
19     String username = "root";
20     String password = "123456";
21     String url = "jdbc:mysql://localhost:3306/oa?useUnicode=true&characterEncoding=utf8";
22     String sql = "select * from itcast_role where id = ?";
23     PreparedStatement ps;
24     Connection conn;
25     public int init() throws Throwable {
26         //1、注册mysql驱动
27         Class.forName("com.mysql.jdbc.Driver");
28         //2、通过url、用户名、密码建立mysql连接,返回一个Connection对象
29         conn = DriverManager.getConnection(url, username, password);
30         //3、对sql进行预编译,返回一个已经编译过的sql对象PreparedStatement
31         ps = conn.prepareStatement(sql);
32         return 0;
33     }//end of init
34 
35     public int action() throws Throwable {
36             
37     lr.start_transaction("query");
38 
39                 
40         //4、执行sql语句之前,先对sql中不确定的数据进行赋值
41         ps.setInt(1, Integer.parseInt("<num>"));
42         //5、执行sql语句,返回受影响的行数      
43         //(executeUpdate 增删改)(executeQuery 查询单独用这个,返回结果集的对象,这个对象叫ResultSet,然后取个变量名叫set)
44         ResultSet set = ps.executeQuery();
45         // 只有结果集中有数据,就认为查询成功 set.next()       next方法
46                 
47         if (set.next()) {
48             
49             lr.end_transaction("query", lr.PASS);
50     
51         }else{
52             
53             lr.end_transaction("query", lr.FAIL);
54     
55         }
56 
57         return 0;
58     }
59 
60 
61 
62     public int end() throws Throwable {
63         //6、关闭mysql连接
64         conn.close();
65         return 0;
66     }//end of end
67     
68 
69 }
loadrunner_jdbc_query

 loadrunner运行日志

 1 Virtual User Script started at : 2019-03-30 20:18:47
 2 Starting action vuser_init.
 3 Ending action vuser_init.
 4 Running Vuser...
 5 Starting iteration 1.
 6 Starting action Actions.
 7 Notify: Transaction "query" started.
 8 Notify: Parameter Substitution: parameter "num" =  "15"
 9 Notify: Transaction "query" ended with "Pass" status (Duration: 0.0197).
10 Ending action Actions.
11 Ending iteration 1.
12 Starting iteration 2.
13 Starting action Actions.
14 Notify: Transaction "query" started.
15 Notify: Parameter Substitution: parameter "num" =  "6"
16 Failed to get file name and line of the script for failed transaction "query".
17 Notify: Transaction "query" ended with "Fail" status (Duration: 0.0123).
18 Ending action Actions.
19 Ending iteration 2.
20 Starting iteration 3.
21 Starting action Actions.
22 Notify: Transaction "query" started.
23 Notify: Parameter Substitution: parameter "num" =  "18"
24 Notify: Transaction "query" ended with "Pass" status (Duration: 0.0132).
25 Ending action Actions.
26 Ending iteration 3.
27 Starting iteration 4.
28 Starting action Actions.
29 Notify: Transaction "query" started.
30 Notify: Parameter Substitution: parameter "num" =  "15"
31 Notify: Transaction "query" ended with "Pass" status (Duration: 0.0124).
32 Ending action Actions.
33 Ending iteration 4.
34 Starting iteration 5.
35 Starting action Actions.
36 Notify: Transaction "query" started.
37 Notify: Parameter Substitution: parameter "num" =  "20"
38 Notify: Transaction "query" ended with "Pass" status (Duration: 0.0130).
39 Ending action Actions.
40 Ending iteration 5.
41 Starting iteration 6.
42 Starting action Actions.
43 Notify: Transaction "query" started.
44 Notify: Parameter Substitution: parameter "num" =  "11"
45 Notify: Transaction "query" ended with "Pass" status (Duration: 0.0127).
46 Ending action Actions.
47 Ending iteration 6.
48 Starting iteration 7.
49 Starting action Actions.
50 Notify: Transaction "query" started.
51 Notify: Parameter Substitution: parameter "num" =  "12"
52 Notify: Transaction "query" ended with "Pass" status (Duration: 0.0121).
53 Ending action Actions.
54 Ending iteration 7.
55 Starting iteration 8.
56 Starting action Actions.
57 Notify: Transaction "query" started.
58 Notify: Parameter Substitution: parameter "num" =  "7"
59 Failed to get file name and line of the script for failed transaction "query".
60 Notify: Transaction "query" ended with "Fail" status (Duration: 0.0124).
61 Ending action Actions.
62 Ending iteration 8.
63 Starting iteration 9.
64 Starting action Actions.
65 Notify: Transaction "query" started.
66 Notify: Parameter Substitution: parameter "num" =  "12"
67 Notify: Transaction "query" ended with "Pass" status (Duration: 0.0137).
68 Ending action Actions.
69 Ending iteration 9.
70 Starting iteration 10.
71 Starting action Actions.
72 Notify: Transaction "query" started.
73 Notify: Parameter Substitution: parameter "num" =  "6"
74 Failed to get file name and line of the script for failed transaction "query".
75 Notify: Transaction "query" ended with "Fail" status (Duration: 0.0129).
76 Ending action Actions.
77 Ending iteration 10.
78 Ending Vuser...
79 Starting action vuser_end.
80 Ending action vuser_end.
81 Vuser Terminated.
log

 

 

 

 跳转到 ------- Update 数据库增删改 压测方法             https://www.cnblogs.com/zhenyu1/p/10620524.html

 

posted @ 2019-03-30 20:28  测试艺术家  阅读(554)  评论(0编辑  收藏  举报