首页 > 解决方案 > 何在 Jmeter 中删除 csv 文件中的重复项

问题描述

我有一个将输出保存到 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

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

谢谢。

标签: csvjenkinsjmeterjmeter-plugins

解决方案


如果您有一个 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 线程组添加到您的测试计划

  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 - 为什么以及如何使用它


推荐阅读