【问题标题】:How to replace jmeter jtl httpSample/httpSample subchild value with parent value using java如何使用java将jmeter jtl httpSample/httpSample子值替换为父值
【发布时间】:2019-09-06 04:55:17
【问题描述】:

我尝试使用标准 jmeter 报告模板 XSL(jmeter-results-detail-report_21.xsl) 生成 jmeter 报告,但不知何故,报告无法将 httpSample/httpSample 的子子项显示为 httpSample 的细分。

是否有将子 httpSample/httpSample 结果显示为其父 httpSample 的细分?

我有一个想法,将子 lb 值替换为其父 lb 值,以使其独一无二。但是我是 java/regex/XSL 的新手,我该怎么做呢?

below is original jtl
<sample t="1605" it="15032" lt="0" ct="0" ts="1567595832665" s="true" lb="A02 Landing" rc="200" rm="Number of samples in transaction : 3, number of failing samples : 0" tn="Thread Group 1-1" dt="" by="70695" sby="3136" ng="2" na="2">
  <httpSample t="649" it="0" lt="629" ct="440" ts="1567595837679" s="true" lb="A02.01 /abcd/login.action?do=Login" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="2094" sby="1671" ng="2" na="2">
    <httpSample t="629" it="0" lt="629" ct="440" ts="1567595837679" s="true" lb="https://10.111.11.111:7351/abcd/login.action?do=Login" rc="302" rm="Moved Temporarily" tn="Thread Group 1-1" dt="text" by="856" sby="1001" ng="2" na="2"/>
    <httpSample t="18" it="0" lt="18" ct="0" ts="1567595838310" s="true" lb="https://10.111.11.111:7351/abcd/HandlerInit.action?do=Redirect" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="1238" sby="670" ng="2" na="2"/>
  </httpSample>
  <httpSample t="549" it="0" lt="533" ct="137" ts="1567595843333" s="true" lb="A02.02 /abcd/HandlerLogin.action?do=InitLoad" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="34332" sby="794" ng="2" na="2"/>
  <httpSample t="407" it="0" lt="402" ct="332" ts="1567595848894" s="true" lb="A02.03 /abcd/message_board.action" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="34269" sby="671" ng="2" na="2"/>
</sample>
<sample t="5863" it="15038" lt="0" ct="0" ts="1567595835027" s="true" lb="A03 Landing 2" rc="200" rm="Number of samples in transaction : 3, number of failing samples : 0" tn="Thread Group 1-2" dt="" by="70695" sby="3136" ng="2" na="2">
  <httpSample t="609" it="0" lt="290" ct="142" ts="1567595840037" s="true" lb="A03.01 /abcd/login.action?do=Login2" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="2094" sby="1671" ng="2" na="2">
    <httpSample t="290" it="0" lt="290" ct="142" ts="1567595840037" s="true" lb="https://10.111.11.111:7351/abcd/login.action?do=Login2" rc="302" rm="Moved Temporarily" tn="Thread Group 1-2" dt="text" by="856" sby="1001" ng="2" na="2"/>
    <httpSample t="318" it="0" lt="318" ct="0" ts="1567595840328" s="true" lb="https://10.111.11.111:7351/abcd/HandlerInit.action?do=Redirect2" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="1238" sby="670" ng="2" na="2"/>
  </httpSample>
  <httpSample t="2064" it="0" lt="1323" ct="342" ts="1567595845660" s="true" lb="A03.02 /abcd/HandlerLogin.action?do=InitLoad" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="34332" sby="794" ng="2" na="2"/>
  <httpSample t="3190" it="0" lt="1302" ct="333" ts="1567595852737" s="true" lb="A03.03 /abcd/message_board.action" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="34269" sby="671" ng="2" na="2"/>
</sample>
expected result as below. It's will be few hundred thousand row need to be replace. Is there any way to replace it?
<sample t="1605" it="15032" lt="0" ct="0" ts="1567595832665" s="true" lb="A02 Landing" rc="200" rm="Number of samples in transaction : 3, number of failing samples : 0" tn="Thread Group 1-1" dt="" by="70695" sby="3136" ng="2" na="2">
  <httpSample t="649" it="0" lt="629" ct="440" ts="1567595837679" s="true" lb="A02.01 /abcd/login.action?do=Login" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="2094" sby="1671" ng="2" na="2">
    <httpSample t="629" it="0" lt="629" ct="440" ts="1567595837679" s="true" lb="A02.01 https://10.111.11.111:7351/abcd/login.action?do=Login" rc="302" rm="Moved Temporarily" tn="Thread Group 1-1" dt="text" by="856" sby="1001" ng="2" na="2"/>
    <httpSample t="18" it="0" lt="18" ct="0" ts="1567595838310" s="true" lb="A02.01 https://10.111.11.111:7351/abcd/HandlerInit.action?do=Redirect" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="1238" sby="670" ng="2" na="2"/>
  </httpSample>
  <httpSample t="549" it="0" lt="533" ct="137" ts="1567595843333" s="true" lb="A02.02 /abcd/HandlerLogin.action?do=InitLoad" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="34332" sby="794" ng="2" na="2"/>
  <httpSample t="407" it="0" lt="402" ct="332" ts="1567595848894" s="true" lb="A02.03 /abcd/message_board.action" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="34269" sby="671" ng="2" na="2"/>
</sample>
<sample t="5863" it="15038" lt="0" ct="0" ts="1567595835027" s="true" lb="A03 Landing 2" rc="200" rm="Number of samples in transaction : 3, number of failing samples : 0" tn="Thread Group 1-2" dt="" by="70695" sby="3136" ng="2" na="2">
  <httpSample t="609" it="0" lt="290" ct="142" ts="1567595840037" s="true" lb="A03.01 /abcd/login.action?do=Login2" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="2094" sby="1671" ng="2" na="2">
    <httpSample t="290" it="0" lt="290" ct="142" ts="1567595840037" s="true" lb="A03.01 https://10.111.11.111:7351/abcd/login.action?do=Login2" rc="302" rm="Moved Temporarily" tn="Thread Group 1-2" dt="text" by="856" sby="1001" ng="2" na="2"/>
    <httpSample t="318" it="0" lt="318" ct="0" ts="1567595840328" s="true" lb="A03.01 https://10.111.11.111:7351/abcd/HandlerInit.action?do=Redirect2" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="1238" sby="670" ng="2" na="2"/>
  </httpSample>
  <httpSample t="2064" it="0" lt="1323" ct="342" ts="1567595845660" s="true" lb="A03.02 /abcd/HandlerLogin.action?do=InitLoad" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="34332" sby="794" ng="2" na="2"/>
  <httpSample t="3190" it="0" lt="1302" ct="333" ts="1567595852737" s="true" lb="A03.03 /abcd/message_board.action" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="34269" sby="671" ng="2" na="2"/>
</sample>

【问题讨论】:

  • 您最好不要在这里使用正则表达式,而应该使用 XML 解析器(Java 有许多很好的库可以做到这一点)。
  • 你好,蒂姆,Java 是个好主意。但是 XML 解析器如何知道 httpSample 有子子项并用它的父 lb 值替换。并非所有的 httpSample 都有子标签,只有那些 httpSample 包含子标签的才会被父标签替换。

标签: java regex xml jmeter xml-parsing


【解决方案1】:

也许,正则表达式不是解决这个问题的最佳方法,因为它看起来相当复杂。


话虽如此,父母在行首有两个空格,而“子孩子”似乎有四个,如果你愿意,你可以将其用作左边界,并设计任何你必须的表达式,例如:

^\s{2}<httpSample\s+.*?lb="(\S*).*[\r\n]^\s{4}<httpSample\s+.*?lb="([^"]*)"

只是一个例子。

测试

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class re{

    public static void main(String[] args){

        final String regex = "^\\s{2}<httpSample\\s+.*?lb=\"(\\S*).*[\\r\\n]^\\s{4}<httpSample\\s+.*?lb=\"([^\"]*)\"";
        final String string = "<sample t=\"1605\" it=\"15032\" lt=\"0\" ct=\"0\" ts=\"1567595832665\" s=\"true\" lb=\"A02 Landing\" rc=\"200\" rm=\"Number of samples in transaction : 3, number of failing samples : 0\" tn=\"Thread Group 1-1\" dt=\"\" by=\"70695\" sby=\"3136\" ng=\"2\" na=\"2\">\n"
             + "  <httpSample t=\"649\" it=\"0\" lt=\"629\" ct=\"440\" ts=\"1567595837679\" s=\"true\" lb=\"A02.01 /abcd/login.action?do=Login\" rc=\"200\" rm=\"OK\" tn=\"Thread Group 1-1\" dt=\"text\" by=\"2094\" sby=\"1671\" ng=\"2\" na=\"2\">\n"
             + "    <httpSample t=\"629\" it=\"0\" lt=\"629\" ct=\"440\" ts=\"1567595837679\" s=\"true\" lb=\"https://10.111.11.111:7351/abcd/login.action?do=Login\" rc=\"302\" rm=\"Moved Temporarily\" tn=\"Thread Group 1-1\" dt=\"text\" by=\"856\" sby=\"1001\" ng=\"2\" na=\"2\"/>\n"
             + "    <httpSample t=\"18\" it=\"0\" lt=\"18\" ct=\"0\" ts=\"1567595838310\" s=\"true\" lb=\"https://10.111.11.111:7351/abcd/HandlerInit.action?do=Redirect\" rc=\"200\" rm=\"OK\" tn=\"Thread Group 1-1\" dt=\"text\" by=\"1238\" sby=\"670\" ng=\"2\" na=\"2\"/>\n"
             + "  </httpSample>\n"
             + "  <httpSample t=\"549\" it=\"0\" lt=\"533\" ct=\"137\" ts=\"1567595843333\" s=\"true\" lb=\"A02.02 /abcd/HandlerLogin.action?do=InitLoad\" rc=\"200\" rm=\"OK\" tn=\"Thread Group 1-1\" dt=\"text\" by=\"34332\" sby=\"794\" ng=\"2\" na=\"2\"/>\n"
             + "  <httpSample t=\"407\" it=\"0\" lt=\"402\" ct=\"332\" ts=\"1567595848894\" s=\"true\" lb=\"A02.03 /abcd/message_board.action\" rc=\"200\" rm=\"OK\" tn=\"Thread Group 1-1\" dt=\"text\" by=\"34269\" sby=\"671\" ng=\"2\" na=\"2\"/>\n"
             + "</sample>\n"
             + "<sample t=\"5863\" it=\"15038\" lt=\"0\" ct=\"0\" ts=\"1567595835027\" s=\"true\" lb=\"A03 Landing 2\" rc=\"200\" rm=\"Number of samples in transaction : 3, number of failing samples : 0\" tn=\"Thread Group 1-2\" dt=\"\" by=\"70695\" sby=\"3136\" ng=\"2\" na=\"2\">\n"
             + "  <httpSample t=\"609\" it=\"0\" lt=\"290\" ct=\"142\" ts=\"1567595840037\" s=\"true\" lb=\"A03.01 /abcd/login.action?do=Login2\" rc=\"200\" rm=\"OK\" tn=\"Thread Group 1-2\" dt=\"text\" by=\"2094\" sby=\"1671\" ng=\"2\" na=\"2\">\n"
             + "    <httpSample t=\"290\" it=\"0\" lt=\"290\" ct=\"142\" ts=\"1567595840037\" s=\"true\" lb=\"https://10.111.11.111:7351/abcd/login.action?do=Login2\" rc=\"302\" rm=\"Moved Temporarily\" tn=\"Thread Group 1-2\" dt=\"text\" by=\"856\" sby=\"1001\" ng=\"2\" na=\"2\"/>\n"
             + "    <httpSample t=\"318\" it=\"0\" lt=\"318\" ct=\"0\" ts=\"1567595840328\" s=\"true\" lb=\"https://10.111.11.111:7351/abcd/HandlerInit.action?do=Redirect2\" rc=\"200\" rm=\"OK\" tn=\"Thread Group 1-2\" dt=\"text\" by=\"1238\" sby=\"670\" ng=\"2\" na=\"2\"/>\n"
             + "  </httpSample>\n"
             + "  <httpSample t=\"2064\" it=\"0\" lt=\"1323\" ct=\"342\" ts=\"1567595845660\" s=\"true\" lb=\"A03.02 /abcd/HandlerLogin.action?do=InitLoad\" rc=\"200\" rm=\"OK\" tn=\"Thread Group 1-2\" dt=\"text\" by=\"34332\" sby=\"794\" ng=\"2\" na=\"2\"/>\n"
             + "  <httpSample t=\"3190\" it=\"0\" lt=\"1302\" ct=\"333\" ts=\"1567595852737\" s=\"true\" lb=\"A03.03 /abcd/message_board.action\" rc=\"200\" rm=\"OK\" tn=\"Thread Group 1-2\" dt=\"text\" by=\"34269\" sby=\"671\" ng=\"2\" na=\"2\"/>\n"
             + "</sample>";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);

        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }
        }

    }
}

输出

Full match:   <httpSample t="649" it="0" lt="629" ct="440" ts="1567595837679" s="true" lb="A02.01 /abcd/login.action?do=Login" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" by="2094" sby="1671" ng="2" na="2">
    <httpSample t="629" it="0" lt="629" ct="440" ts="1567595837679" s="true" lb="https://10.111.11.111:7351/abcd/login.action?do=Login"
Group 1: A02.01
Group 2: https://10.111.11.111:7351/abcd/login.action?do=Login
Full match:   <httpSample t="609" it="0" lt="290" ct="142" ts="1567595840037" s="true" lb="A03.01 /abcd/login.action?do=Login2" rc="200" rm="OK" tn="Thread Group 1-2" dt="text" by="2094" sby="1671" ng="2" na="2">
    <httpSample t="290" it="0" lt="290" ct="142" ts="1567595840037" s="true" lb="https://10.111.11.111:7351/abcd/login.action?do=Login2"
Group 1: A03.01
Group 2: https://10.111.11.111:7351/abcd/login.action?do=Login2

如果您想探索/简化/修改表达式,它已经 在右上角的面板上进行了解释 regex101.com。如果你愿意,你 也可以在this link看,怎么搭配 针对一些样本输入。


【讨论】:

  • 晚安,艾玛,谢谢你指导我的逻辑。但是我如何用我们为 matcher.group(1) + " " + matcher.group(2) 获得的值替换 httpSample/httpSample lb 值;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
相关资源
最近更新 更多