Jmeter JSR223 采样器 - 无法将数据写入 CSV 文件 [英] Jmeter JSR223 Sampler - Unable to Write Data To CSV File
问题描述
我使用的是 Jmeter v 4.0 r1823414.根据这个答案,有一个使用 JSR223 后处理器的建议,逐字逐句:
I'm using Jmeter v 4.0 r1823414. According to this answer, there is an advisory to use JSR223 PostProcessor, verbatim:
建议使用 Groovy 进行任何形式的脚本编写以考虑迁移到 JSR223 后处理器
it is recommended to use Groovy for any form of scripting to consider migrating to JSR223 PostProcessor
在此方法之前,我尝试使用 BeanShell Sampler 将数据简单地写入 csv 文件.我从 blazemeter 中获取的代码示例教程页面.但是,我收到错误
Before this approach I tried to use BeanShell Sampler to simply write data to csv file. The code sample I took from the blazemeter tutorial page. However, I was getting error
Sourced file: inline evaluation of: ``import java.io.FileWriter; import java.util.Arrays; import java.io.Writer; impor . . . '' : Typed variable declaration : Attempt to access property on undefined variable or class name
好吧,由于有人建议迁移到 JSR223 Sampler(或 PostProcessor,不确定它们在功能方面的区别),因此我稍微修改了代码以使其更基于 Java用于 JSR223 采样器:
Well, since it was suggested to move to JSR223 Sampler (or PostProcessor, not sure what's the difference between them in terms of functionality), I modified the code a little bit to be more Java based to be used in JSR223 Sampler:
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.io.Writer;
import java.util.List;
public class Main {
//Default separator
private static char SEPARATOR = ',';
//get path of csv file (creates new one if its not exists)
private static String csvFile = "C:/TiredOfProgramming/Work/results.csv"; // for example '/User/Downloads/blabla.csv'
private static String[] params = {"hello", "world", "jmeter"};
//function write line in csv
private static void writeLine(FileWriter writer, String[] parameters, char separator) throws IOException
{
boolean firstParameter = true;
StringBuilder stringBuilder = new StringBuilder();
String parameter = " ";
for (int i = 0; i < parameters.length; i++)
{
//get param
parameter = parameters[i];
log.info(parameter);
//if the first param in the line, separator is not needed
if (!firstParameter)
{
stringBuilder.append(separator);
}
//Add param to line
stringBuilder.append(parameter);
firstParameter = false;
}
//prepare file to next line
stringBuilder.append("
");
//add to file the line
log.info(stringBuilder.toString());
writer.append(stringBuilder.toString());
}
FileWriter fileWriter = new FileWriter(csvFile, true);
writeLine(fileWriter, params, SEPARATOR);
//proper close to file
fileWriter.flush();
fileWriter.close();
}
此特定脚本失败并显示以下消息:
This particular script fails with the message:
Script8.groovy:15:意外标记:你好@第 15 行,第 39 列.private static String[] params = {"hello", "world", "jmeter"};
Script8.groovy: 15: unexpected token: hello @ line 15, column 39. private static String[] params = {"hello", "world", "jmeter"};
如果我在 IntelliJ IDE 中测试这个,只需将 writeLine 方法包装到 main 方法中,一切正常
If I test this in IntelliJ IDE with just wrapping writeLine method into the main method, everything works fine
package com.tiredofprogramming;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.io.Writer;
import java.util.List;
public class Main {
//Default separator
private static char SEPARATOR = ',';
//get path of csv file (creates new one if its not exists)
private static String csvFile = "C:\TiredOfProgramming\Work\results.csv"; // for example '/User/Downloads/blabla.csv'
private static String[] params = {"hello", "world", "jmeter"};
public static void main(String[] args) throws IOException {
FileWriter fileWriter = new FileWriter(csvFile, true);
writeLine(fileWriter, params, SEPARATOR);
//proper close to file
fileWriter.flush();
fileWriter.close();
}
//function write line in csv
private static void writeLine(FileWriter writer, String[] parameters, char separator) throws IOException
{
boolean firstParameter = true;
StringBuilder stringBuilder = new StringBuilder();
String parameter = " ";
for (int i = 0; i < parameters.length; i++)
{
//get param
parameter = parameters[i];
System.out.println(parameter);
//if the first param in the line, separator is not needed
if (!firstParameter)
{
stringBuilder.append(separator);
}
//Add param to line
stringBuilder.append(parameter);
firstParameter = false;
}
//prepare file to next line
stringBuilder.append("
");
//add to file the line
System.out.println(stringBuilder.toString());
writer.append(stringBuilder.toString());
}
}
我的问题是:groovy 采样器是否理解 Java(在一篇 SO 文章中我看到提到 Groovy 理解 99% 的 Java 语法).有没有人成功地使用 Jmeter 将数据写入 csv 文件?
My question is: does groovy sampler understand Java (in one of SO post I saw mention that Groovy understands 99% of the java syntax). Was anyone ever successful at writing data into the csv file using Jmeter?
推荐答案
试试这个小小的修改:
//Default separator
char SEPARATOR = ',';
//get path of csv file (creates new one if its not exists)
String csvFile = "C:\TiredOfProgramming\Work\results.csv"; // for example '/User/Downloads/blabla.csv'
def params = ["hello", "world", "jmeter"];
//function write line in csv
def writeLine(FileWriter writer, List<String> parameters, char separator) throws IOException {
boolean firstParameter = true;
StringBuilder stringBuilder = new StringBuilder();
String parameter = " ";
for (int i = 0; i < parameters.size(); i++) {
//get param
parameter = parameters[i];
log.info(parameter);
//if the first param in the line, separator is not needed
if (!firstParameter) {
stringBuilder.append(separator);
}
//Add param to line
stringBuilder.append(parameter);
firstParameter = false;
}
//prepare file to next line
stringBuilder.append("
");
//add to file the line
log.info(stringBuilder.toString());
writer.append(stringBuilder.toString());
}
FileWriter fileWriter = new FileWriter(csvFile, true);
writeLine(fileWriter, params, SEPARATOR);
//proper close to file
fileWriter.flush();
fileWriter.close();
但是在 Groovy 中有更简单的方法可以实现相同的目标,请查看:
However there are easier method to achieve the same in Groovy, check out:
这篇关于Jmeter JSR223 采样器 - 无法将数据写入 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!