使用批处理文件解析Jmeter xml响应文件并在控制台上打印输出

时间:2017-07-21 11:28:06

标签: xml batch-file cmd jmeter batch-processing

我有一个Jmeter响应文件:

<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<httpSample t="22" lt="11" ts="1500633511099" s="true" lb="Url check" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="543" ng="1" na="1">
  <assertionResult>
    <name>Response Assertion</name>
    <failure>false</failure>
    <error>false</error>
  </assertionResult>
  <httpSample t="11" lt="11" ts="1500633511099" s="true" lb="http://ipaddress/application" rc="302" rm="Found" tn="Thread Group 1-1" dt="" by="149" ng="1" na="1"/>
  <httpSample t="10" lt="10" ts="1500633511111" s="true" lb="http://ipaddress/application/" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="394" ng="1" na="1"/>
</httpSample>
<httpSample t="196" lt="196" ts="1500633511133" s="true" lb="Login" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="1204" ng="1" na="1">
  <assertionResult>
    <name>Response Assertion</name>
    <failure>false</failure>
    <error>false</error>
  </assertionResult>
  <assertionResult>
    <name>Response Assertion - HttpOnly</name>
    <failure>false</failure>
    <error>false</error>
  </assertionResult>
</httpSample>
<httpSample t="98" lt="98" ts="1500633511350" s="true" lb="Logout" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="986" ng="1" na="1">
  <assertionResult>
    <name>Response Assertion</name>
    <failure>false</failure>
    <error>false</error>
  </assertionResult>
</httpSample>

</testResults>

我想使用批处理文件解析此文件,并获取以下属性的输出,即只有值:

  1. httpSample - &gt;磅
  2. httpSample - &gt; RC
  3. httpSample - &gt; RM
  4. 我已经创建了一个批处理文件,但它不起作用:

    @echo off
    set tag=httpSample
    set f_xml=response.xml
    (for /F "tokens=5,6,7 delims=<=>" %%a in ('findstr "\</%tag%\>" %f_xml%') do echo  (
        IF /i "%%a"=="lb" SET "printme=Y"   %%~a %%b
        IF /i "%%a"=="rc" SET "printme=Y"   %%~a %%b
        IF /i "%%a"=="rm" SET "printme=Y"   %%~a %%b
     )
     )
    

    另外,我想打印自己的文本作为输出的关键。 事实上,这个response.xml没有完成我们在 Jmeter GUI 中为每次调用获得的实际响应。它只是表明成功或失败。如何以 Jmeter非gui模式,即命令行模式获取所有响应数据?

    例如。如果%% a的输出为http://ipaddress/application,那么我想打印ipadderss=http://ipaddress/application

2 个答案:

答案 0 :(得分:0)

以下是仅从提供的特定内容中挑选出您请求的特定数据的示例。

@Echo Off
Set "tag=httpSample"
Set "f_xml=response.xml"
For /F "Delims=>" %%A In ('FindStr "\<%tag%\>" "%f_xml%"'
) Do For /F Tokens^=10^,12^,14Delims^=^" %%B In ("%%A"
) Do Echo "%%B" "%%C" "%%D"
Pause

它可以扩展但是使用批处理文件来读取xml数据而不使用其他语言绝对不是推荐的,或者通常是值得的。

答案 1 :(得分:0)

如果您想要保存响应数据,您需要明确地&#34;告诉&#34; JMeter要做到这一点,你有以下选择:

  1. Ad-hoc(仅限一次)设置通过-J command-line argument传递相关属性:

    jmeter -Jjmeter.save.saveservice.response_data=true -n -t test.jmx -l result.jtl
    
  2. 永久设定。如果您希望JMeter始终存储响应数据,则需要将上述行添加到 user.properties 文件(位于JMeter安装的&#34; bin&#34;文件夹中)

    jmeter.save.saveservice.response_data=true
    

    JMeter重新启动将需要选择该属性。

  3. 参考文献:

    请记住,使用XML格式存储JMeter测试结果并特别是保存响应数据不是JMeter Best Practices,因为它们会产生较大的磁盘IO开销,并可能对负载测试结果产生负面影响。