将JDBC查询结果写入CSV文件?

时间:2018-01-02 17:19:51

标签: jmeter

我想设置一个测试计划来执行查询并将结果写入csv文件。

遵循这个问题的建议:

https://sqa.stackexchange.com/questions/26456/write-jdbc-request-results-to-csv-file

我已经设定了我的测试计划。它运行没有问题,但是没有创建foo.csv文件。

这是JSR223预处理器中的代码:

resultSet = vars.getObject("resultSet");
StringBuilder result = new StringBuilder();

for (Object row : resultSet ) {
    iter = row.entrySet().iterator();
    while (iter.hasNext()) {
        pair = iter.next();
        result.append(pair.getValue());
        result.append(",");
    }
    result.append(System.getProperty("line.separator"));
}

org.apache.commons.io.FileUtils.writeStringToFile(new File("foo.csv"), result.toString(), "UTF-8");

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

这实际上是有效的,csv文件没有被写入我预期的位置。我添加了这个来找到文件:

log.info(System.getProperty("user.dir"));

答案 1 :(得分:0)

该文件正在当前工作目录中编写,您可以通过在Terminal应用程序中运行以下命令找到JMeter写入的位置:

find / -type f -name 'foo.csv'

您还可以修改最后一行代码,以便明确指定CSV文件的完整路径,如:

org.apache.commons.io.FileUtils.writeStringToFile(new File("/Users/aoppenheim/Desktop/foo.csv"), result.toString(), "UTF-8");

另外我建议将“语言”切换为groovy,因为java假设使用Beanshell解释器,并且在高负载情况下它可能会成为性能瓶颈。有关详细信息,请参阅Apache Groovy - Why and How You Should Use It指南。

相关问题