如何通过 JMeter 中的 JDBC 采样器运行多个 MySQL 语句

2022-01-04 00:00:00 performance-testing mysql jdbc jmeter

我在 JMeter 2.13 中使用 JDBC 采样器.

我的 JMeter 采样器中有大约 100 个删除语句,如下所示:

delete from abc where id >= ${Variable_Name};从 qwe 中删除其中 id >= ${Variable_Name};从 xyz 中删除其中 id >= ${Variable_Name};

问题是当我在 JDBC 采样器中运行单个语句时,它工作正常.但是当我尝试从我的 JDBC 采样器中运行 2 个或 2 个以上的语句时.它总是抛出错误.

<块引用>

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'delete from qwe where id >= 1;

附近使用的正确语法

有人可以提一个解决方法吗?以及我如何克服这个问题.

解决方案

您似乎无法在单个 JDBC Request 元素中执行多个语句.

我遇到过类似的情况,我需要在继续执行其余测试之前对数据库执行一些清理语句.我能够通过使用嵌套在 Loop Controller 中的 CSV 数据集配置 从外部文件读取 SQL 语句来实现这一点,在一个单独的setUp Thread Group.

元素是这样放置的:

我使用了以下配置:

循环控制器

  • 循环次数:Forever

CSV 数据集配置

  • 文件名:/path/to/multiple-statements.sql
  • 变量名:STMT
  • 在 EOF 上回收:False
  • 在 EOF 上停止线程:True

JDBC 请求

  • 查询:${STMT}

Loop Controller 设置为永远运行,因为在 CSV 数据集配置 上设置了停止条件.每次迭代都会读取文件的一行,设置变量STMT,然后JDBC Request将执行查询${STMT}.>

当到达文件尾时,setUp Thread Group 将停止,核心测试Thread Group 将继续.

I am using JDBC sampler in JMeter 2.13.

I have around 100 delete statements in my JMeter sampler like below:

delete from abc where id >= ${Variable_Name};
delete from qwe where id >= ${Variable_Name};
delete from xyz where id >= ${Variable_Name};

Problem is that when I run a single statement in JDBC sampler, it works fine. But when ever I try to run 2 or more than 2 statements from my JDBC sampler. It always throws error.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'delete from qwe where id >= 1;

Can someone please mention a workaround it? and how I can overcome this problem.

解决方案

It seems you cannot execute multiple statements in a single JDBC Request element.

I had a similar situation where I needed to execute some clean up statements on the database before proceeding with the rest of the tests. I was able to achieve this by reading the SQL statements from an external file, using CSV Data Set Config nested in a Loop Controller, in a separate setUp Thread Group.

The elements were placed like this:

And I used the following configurations:

Loop Controller

  • Loop Count: Forever

CSV Data Set Config

  • Filename: /path/to/multiple-statements.sql
  • Variable Name: STMT
  • Recycle on EOF: False
  • Stop thread on EOF: True

JDBC Request

  • Query: ${STMT}

The Loop Controller is set to run forever, as the stop condition is set on the CSV Data Set Config. Each iteration will read one line of the file, set the variable STMT, then JDBC Request will execute the query ${STMT}.

When the end-of-file is reached, the setUp Thread Group will stop and the core test Thread Group will proceed.

相关文章