Jmeter-file compare

别来无恙 提交于 2020-01-03 04:58:08

问题


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!


回答1:


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:

References:

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


来源:https://stackoverflow.com/questions/45101895/jmeter-file-compare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!