Jmeter JSR223采样器-无法将数据写入CSV文件

问题描述:

我正在使用Jmeter v 4.0 r1823414 .根据此答案,有关使用JSR223 PostProcessor的建议,逐字:

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("\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();
}

此特定脚本失败,并显示以下消息:

This particular script fails with the message:

Script8.groovy:15:意外令牌:hello @第15行,第39列. 私有静态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("\n");

        //add to file the line
        System.out.println(stringBuilder.toString());
        writer.append(stringBuilder.toString());

  }
}

我的问题是:groovy sampler是否理解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("\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中有更简单的方法可以实现相同目的,请查看:

However there are easier method to achieve the same in Groovy, check out:

  • Groovy Goodness: Working with Files
  • Apache Groovy - Why and How You Should Use It