如何从Jmeter中的数据库返回值



我正在尝试从MSSQL Server数据库返回一个值,并将其存储在Jmeter中的JSR222计时器脚本中的变量中。但是我在日志中遇到以下错误 -

**警告O.A.J.T.JSR223Timer:脚本没有返回值**

这是我在脚本中写的代码 -

try {
        def promoName = ${promotionName}; //extracted the value using JSONExtractor
        log.info("Promotion Name is " + promoName); //not displaying in the log
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:sqlserver://ServerIP;databaseName="";integratedSecurity=true","<userId>","<password>");
    String query ="select query";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
        vars.put("promotionInstanceId", rs.getString(1)); //The query returns an instanceId which i need to store in a variable and use it later
        log.info("Promotion Instance is " + ${promotionInstanceId});            
    }
    conn.close();
    } 
catch (Exception e) {
    e.printStackTrace();

任何人都可以帮助我理解我可能出错的地方吗?

  1. 有什么理由使用计时器?您收到的警告是关于丢失返回关键字,因为JSR223计时器旨在用于计算虚拟用户的计算时间,因此它应该以毫秒为单位返回"睡眠"值。鉴于您不想介绍思考时间是有意义的,而是去找JSR223后处理器。

  2. 不要在脚本中插入jmeter函数或变量,因为它可能会导致脚本行为不当,因为与GSTRING模板发生冲突,或变量将被缓存,并且随后的呼叫将返回相同的值。

  3. 建议使用Jmeter的内置组件,并在可能的情况下避免脚本,您可以使用即JDBC后处理器来提取有趣的值并将其存储在Jmeter变量中。查看调试JDBC Sampler在Jmeter文章中的结果,以了解如何执行SQL语句并使用结果

您应该使用 vars.get获取变量值

vars.put("promotionInstanceId", rs.getString(1)); //The query returns an instanceId which i need to store in a variable and use it later
log.info("Promotion Instance is " +  vars.get("promotionInstanceId"));   

最新更新