简体   繁体   English

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

[英]How to parameterize IN clause in SQL Query to use in JMETER

I need to parametrize a SQL Query to use in JMETER such that every time it triggers, a random value is picked from the list of values to be used in IN clause.我需要参数化一个 SQL 查询以在 JMETER 中使用,这样每次它触发时,都会从要在 IN 子句中使用的值列表中选择一个随机值。

Parent Query - Select * from Employee where Emp_Id in ( 3,9,11,12,13) and Dept_Name in('HR',IT','Admin','Audit')父查询 - Select * 来自 Employee,其中 Emp_Id 在 ( 3,9,11,12,13) 和 Dept_Name in('HR',IT','Admin','Audit')

Post the Parameterization when i trigger the Query through JDBC Request the request run for different user needs to have random selection made.当我通过 JDBC 触发查询时发布参数化请求为不同用户运行的请求需要进行随机选择。 Ex: Query 1 should be like - Select * from Employee where Emp_Id in ( 3,9) and Dept_Name in('HR',IT')例如:查询 1 应该类似于 - Select * 来自 Employee,其中 Emp_Id in ( 3,9) 和 Dept_Name in('HR',IT')

Query 2 should be like - Select * from Employee where Emp_Id in ( 11,12,13) and Dept_Name in('HR',IT','Admin')查询 2 应该类似于 - Select * 来自 Employee,其中 Emp_Id in ( 11,12,13) 和 Dept_Name in('HR',IT','Admin')

I am trying to use CSV Data Set Config but not able to achieve the above output.我正在尝试使用 CSV 数据集配置但无法实现上述 output。

First of all I would recommend reconsidering your whole approach because tests needs to be 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 .首先,我建议重新考虑您的整个方法,因为测试需要可重复,如果您需要检查 emp 和部门 ID 的所有可能组合 - go 以进行成对测试,请将生成的查询存储在 CSV 文件中并参数化查询使用CSV 数据集配置

If you still want to make the number of arguments absolutely random you can go for the following approach:如果你仍然想让 arguments 的数字完全随机,你可以使用以下方法 go :

  1. Add User Defined Variables to your Test Plan and define the following variables there:用户定义的变量添加到您的测试计划并在那里定义以下变量:

     Emp_Id=3,9,11,12,13 Dept_Name=HR,IT,Admin
  2. Amend your query to include JMeter's __groovy() function like:修改您的查询以包含 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('\,'),)})

Demo:演示:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM