将JMeter响应保存为CSV

将JMeter响应保存为CSV

本文介绍了将JMeter响应保存为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JMeter的新手,正在尝试为我正在从事的项目执行负载测试.我已经创建了一个测试计划来创建2000个用户.结果请求如下所示:{:状态":"200",:错误":为空,:内容":"1858"}我想将所有2000个请求的"Content"值保存在一个csv文件中.有什么办法吗?

I am new to JMeter and trying to perform a load test for the project that I am working on.I have created a test plan to create 2000 users. The resultant request is like below:{: "Status":"200",: "Error":null,: "Content":"1858"}I want to save the value of "Content" for all the 2000 requests in a single csv file.Is there any way to do this?

推荐答案

简便方法:将值附加到.jtl结果文件

Easy way: append values to .jtl results file

  1. 将以下行添加到 user.properties 文件(位于JMeter安装的/bin文件夹下)

  1. Add the following line to user.properties file (lives under /bin folder of your JMeter installation)

sample_variables=content

  • 添加正则表达式提取器作为请求的子代,返回该内容并将其配置如下:

  • Add Regular Expression Extractor as a child of the request which returns that content and configure it as follows:

    • 参考名称:content
    • 正则表达式:"Content":"(\d+)"
    • 模板:$1$
    • Reference Name: content
    • Regular Expression: "Content":"(\d+)"
    • Template: $1$

    当您运行JMeter时在命令行非GUI模式下

    jmeter -n -t /path/to/your/script.jmx -l /path/to/test/results.jtl
    

    并且测试执行完成,您将能够在 results.jtl 结果文件的最后一列

    and test execution finishes you will be able to see "Content" values as the last column of results.jtl results file


    硬体:自定义脚本


    Hard way: custom scripting

    1. 添加 Bean Shell PostProcessor 作为请求的子代,该请求将返回结果
    2. 将以下代码放入PostProcessor的脚本"区域

    1. Add a Beanshell PostProcessor as a child of the request which returns that result
    2. Put the following code into the PostProcessor's "Script" area

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    String response = new String(data);
    
    FileOutputStream out = new FileOutputStream("content.csv", true);
    String regex = "\"Content\":\"(\\d+)\"";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(response);
    if (m.find()) {
        String content = m.group(1);
        out.write(content.getBytes());
        out.write(System.getProperty("line.separator").getBytes());
        out.flush();
    }
    

  • 一旦测试完成,您将在JMeter的工作目录(通常为/bin)中看到包含所有"Content"值的 content.csv 文件.

    Once test finishes you will see content.csv file in JMeter's working directory (usually /bin) containing all "Content" values.

    这篇关于将JMeter响应保存为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-14 13:54