如何在 SQL 查询中参数化 IN 子句以在 JMETER 中使用

How to parameterize IN clause in SQL Query to use in JMETER

我需要参数化一个 SQL 查询以在 JMETER 中使用,这样每次它触发时,都会从要在 IN 子句中使用的值列表中选择一个随机值。

父查询 - Select * 来自员工,其中 Emp_Id in ( 3,9,11,12,13)​​ 和 Dept_Name in('HR',IT' ,'Admin','Audit')

Post 当我通过 JDBC 触发查询时的参数化请求 运行 不同用户的请求需要随机选择。 前任: 查询 1 应该类似于 - Select * from Employee where Emp_Id in ( 3,9) and Dept_Name in('HR',IT')

查询 2 应该类似于 - Select * from Employee where Emp_Id in ( 11,12,13)​​ and Dept_Name in('HR',IT', 'Admin')

我正在尝试使用 CSV 数据集配置,但无法实现上述输出。

首先,我建议重新考虑您的整个方法,因为测试需要 repeatable, if you need to check all the possible combinations of the emp and dept IDs - go for pairwise testing, store the generated queries in the CSV file and parameterize the queries using CSV Data Set Config.

如果您仍然想要使参数的数量绝对随机,您可以采用以下方法:

  1. User Defined Variables 添加到您的测试计划并在那里定义以下变量:

    Emp_Id=3,9,11,12,13
    Dept_Name=HR,IT,Admin
    
  2. 修改您的查询以包含 JMeter 的 __groovy() function,例如:

    Select * from Employee where Emp_Id in (${__groovy(def values = vars.get('Emp_Id').split('\,').collect(); values.shuffle(); values.take(org.apache.commons.lang3.RandomUtils.nextInt(1\,values.size())).join('\,'),)}) and Dept_Name in(${__groovy(def values = vars.get('Dept_Name').split('\,').collect{value -> "'" + value + "'"}; values.shuffle(); values.take(org.apache.commons.lang3.RandomUtils.nextInt(1\,values.size())).join('\,'),)})
    

演示: