【问题标题】:Ho to remove duplicate items in csv file in Jmeter如何在Jmeter中删除csv文件中的重复项
【发布时间】:2021-05-27 07:09:24
【问题描述】:

我有一个将输出保存到 CSV 文件的 Jmeter 脚本。在输出(CSV)文件中,我想删除 URL 列表(第 3 列)中的重复项。我该怎么做?

csv 文件中的示例行如下:

responsecode, responsemessage,url 
404,Not Found,http://test1 
404,NotFound,http://test1 
404,Not Found,http://test2 
404,NotFound,http://test3 
404,Not Found,http://test2

预期:

responsecode, responsemessage,url 
404,Not Found,http://test1 
404 Not Found,http://test2 
404,Not Found,http://test3

任何人都可以帮助如何做到这一点?

谢谢。

【问题讨论】:

    标签: csv jenkins jmeter jmeter-plugins


    【解决方案1】:

    如果您的 CSV 文件如下所示:

    200,OK,http://example.com
    404,Not Found,http://example.com/foo
    200,OK,http://example.com
    404,Not Found,http://example.com/bar
    

    并希望将其转换为以下内容:

    200,OK,http://example.com
    404,Not Found,http://example.com/foo
    404,Not Found,http://example.com/bar
    
    1. tearDown Thread Group 添加到您的测试计划中

    2. JSR223 Sampler添加到tearDown线程组

    3. 将以下代码放入“脚本”区域:

       def dedup = new File('original.csv').readLines().collectEntries { [it.split(',')[2], it] }
      
       dedup.entrySet().each { entry ->
           new File('without-duplicates.csv') << entry.getValue() << System.getProperty('line.separator')
       }
      
    4. original.csv 替换为 CSV 文件的相对或完整路径,将 without-duplicates.csv 替换为所需的文件名称/位置,不重复

    有关 JMeter 中 Groovy 脚本的更多信息:Apache Groovy - Why and How You Should Use It

    【讨论】:

    • 我尝试了代码,但它没有创建没有重复的文件。此外,CSV 在第一行有字段名称。使用提供的脚本似乎不起作用
    • 您的 CSV 文件包含空格,如果您需要在上面的示例代码 sn-p 中将 it.split(',')[2] 更改为 it.split(',')[2].trim()。我想提醒您,这不是代码编写服务,我也不是您个人的心灵感应 JMeter 故障排除助手。 Groovy syntax不是什么大秘密,The Groovy Templates Cheat Sheet for JMeter也不是
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多