Jmeter文件比较 [英] Jmeter-file compare

查看:177
本文介绍了Jmeter文件比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jmeter&的新手.我正在寻找一个使用Jmeter比较两个文件的选项.这两个文件都是使用jmeter中的Save Response to file生成的.同样,两个文件都包含对jdbc请求的响应,跨多列&行.我在响应断言中使用__FileToString()函数比较两个文件.但是,如果我的文件中包含带有某些特殊字符的数据,则此操作将失败.有什么提示我该如何处理?或任何其他方法来比较两个Jmeter创建的文件?我也想知道两个文件中不同的记录. 我知道可以使用许多其他工具来比较文件,但是我真的很想使用Jmeter进行此操作.谢谢!

I am new to jmeter & I am looking for an option to compare two files using Jmeter. Both the files are generated using Save Response to file in jmeter. Also both files contain response to a jdbc request, with 100s of values across multiple columns & rows. I am using __FileToString() function in my response assertion to compare the two files. But this fails if my file has data with some special chars. Any tips how could I handle this ? OR any other ways to compare two Jmeter created files ? I would also want to know the records that are different in both files. I know files could be compared using a lot of other tools, but I would really want to do this using Jmeter please. Thank you!

推荐答案

例如,您可以使用 Groovy语言和类似如下的代码:

You can do it for example using JSR223 Sampler, Groovy language and code like:

def file1 = new File('/path/to/file1')
def file2 = new File('/path/to/file2')

def file1Lines = file1.readLines('UTF-8')
def file2Lines = file2.readLines('UTF-8')

if (file1Lines.size() != file2Lines.size()) {
   SampleResult.setSussessful(false)
   SampleResult.setResponseMessage('Files size is different, omitting line-by-line compare')
} else {
   def differences = new StringBuilder()

   file1Lines.eachWithIndex { String file1Line, int number ->
       String file2Line = file2Lines.get(number)
       if (!file1Line.equals(file2Line)) {
           differences.append('Difference # ').append(number).append('. Expected: ')
                   .append(file1Line).append('. Actual: ' + file2Line)
           differences.append(System.getProperty('line.separator'))
       }
   }

   if (differences.toString().length() > 0) {
       SampleResult.setSuccessful(false)
       SampleResult.setResponseMessage(differences.toString())
   }
}

如果有任何差异,采样器将失败,您将在响应消息"部分中看到有关增量"的信息:

In case of any differences the sampler will be failed and you will see the information about "deltas" in the "Response Message" section:

参考文献:

  • Groovy: reading files
  • SampleResult class JavaDoc
  • Apache Groovy - Why and How You Should Use It

这篇关于Jmeter文件比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆