我正在使用Jmeter v 4.0 r1823414 。根据{{3}}的建议,逐字使用JSR223 PostProcessor:
建议对任何形式的脚本使用Groovy考虑 迁移到JSR223后处理器
在使用这种方法之前,我尝试使用BeanShell Sampler将数据简单地写入csv文件。我从this answer页面获取的代码示例。但是,我遇到了错误
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采样器中使用:
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("\n");
//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();
}
此特定脚本失败,并显示以下消息:
Script8.groovy:15:意外令牌:hello @第15行,第39列。 私有静态String [] params = {“ hello”,“ world”,“ jmeter”};
如果我在 IntelliJ IDE 中对此进行了测试,只是将writeLine方法包装到main方法中,则一切正常
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("\n");
//add to file the line
System.out.println(stringBuilder.toString());
writer.append(stringBuilder.toString());
}
}
我的问题是:groovy sampler是否理解Java(在一篇SO帖子中,我看到提到Groovy理解99%的Java语法)。有没有人成功使用Jmeter将数据写入csv文件?
答案 0 :(得分:1)
尝试一下此修改:
//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("\n");
//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中有更简单的方法可以实现相同目的,请查看:
答案 1 :(得分:0)
您可以尝试以下代码。这是一个简单的代码段,但是您可以使用JSR223后处理器从Jmeter成功写入csv文件。
select name, amount,
max(action) over (partition by name, amount order by date) as action,
date
from t;