【问题标题】:Jmeter- Read all the files from a folder and based on file type perform the validationJmeter-从文件夹中读取所有文件并根据文件类型执行验证
【发布时间】:2018-08-23 14:46:57
【问题描述】:

我有不同类型的 csv 文件 Company、Jobcode、Workforce,我将把它们放到 SFTP 中,文件将被处理并保存在 DB 中。我需要比较和验证 csv 文件和 DB 值的值,以便正确保存所有记录。

对于单个文件,我已按照以下步骤进行验证。

  1. 线程组 1 --> 将单个文件拖放到 SFTP 并获取 Beanshell 后处理器中的行数
  2. 线程组 2 --> JDBC 请求和循环控制器 - csv 文件配置和 JSSR 采样器循环每个 csv 值并与 DB 值进行比较

删除多个文件 (一世)。 Beanshell 处理器 - 从文件夹中读取文件并存储在变量文件中,并将此变量(文件)传递给 -> ForEachController 并放置 SFTP 采样器来处理文件(以便将记录保存在数据库中)。

为了验证 csv 文件和 DB 值,我需要获取每个文件的 csv 行数,我将在步骤 (i) 中从文件夹中读取的所有文件写入另一个 csv 文件(命名为 Allfiles.csv) .现在的问题是“Allfiles.csv”有以下内容

C:\Users\AllFiles\Company_V1_0_201712251403.txt
C:\Users\AllFiles\JobCode_V1_0_201712040436.txt
C:\Users\AllFiles\Workc_V1_0_201802081914.txt

由于是反斜杠(),我已将其替换为正斜杠(//)并保存在变量中。

现在我需要获取每个文件的行号。

我使用了 Loop Controller-> CSV 数据配置和 JSSR Sampler,代码如下

import org.apache.commons.io.FilenameUtils
import java.io.*

csvLineContent = vars.get('AllFiles')
log.info("-------------->" + csvLineContent)

csvLineContent = csvLineContent.replace('\\', '//') //replacing slash
vars.put('csvLineContent', csvLineContent)
log.info("==============" + csvLineContent)
csvfileWithoutExt = FilenameUtils.getName(csvLineContent)
log.info("File name is " + csvfileWithoutExt) //Get only file name

try {
    if (csvfileWithoutExt.startsWith("C")) //Get the line count for company file
    {
        log.info("File Name of Company file-------> " + csvfileWithoutExt)
        log.info("File full path ---> " + csvLineContent)
        reader = new LineNumberReader(new FileReader('csvLineContent')) // Issue here
        log.info("Reading file ====" + reader)
     }
} catch (Throwable ex) {
     log.error("Error in Beanshell", ex);
     throw ex;
}

我无法获取每个文件的行号。

2018-08-22 22:06:14,583 ERROR o.a.j.p.j.s.JSR223Sampler: Error in Beanshell
java.io.FileNotFoundException: csvLineContent (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method) ~[?:1.8.0_161]
    at java.io.FileInputStream.open(Unknown Source) ~[?:1.8.0_161]
    at java.io.FileInputStream.<init>(Unknown Source) ~[?:1.8.0_161]
    at java.io.FileInputStream.<init>(Unknown Source) ~[?:1.8.0_161]
    at java.io.FileReader.<init>(Unknown Source) ~[?:1.8.0_161]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_161]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_161]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_161]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_161]

【问题讨论】:

    标签: jmeter beanshell


    【解决方案1】:

    目前您正在读取文件名,通过变量上下文读取文件删除变量名周围的引号:

     reader = new LineNumberReader(new FileReader(csvLineContent)) 
    

    【讨论】:

      【解决方案2】:
      1. 你需要改变这一行:

        reader = new LineNumberReader(new FileReader('csvLineContent'))
        

        到这个:

        reader = new LineNumberReader(new FileReader(csvLineContent))
        

        但是我认为它不会工作,因为文件位于 C:\Users\AllFiles 文件夹下,为了阅读它,您需要提供完整路径,而不仅仅是名称

      2. 根据JMeter Best Practices 建议用户使用JSR223 测试元素和Groovy 语言,如果您在JSR223 元素中使用beanshell 语言 - 您不会有任何性能改进
      3. 在 Groovy 中计算文件中的行数很简单:

        int linesNo = new File('csvLineContent').readLines().size()
        

        更多信息:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-06
        • 2020-03-03
        相关资源
        最近更新 更多