java - 如何使用java将jmeter jtl httpSample/httpSample子子值替换为父值

标签 java regex xml jmeter xml-parsing

我尝试使用标准 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>

最佳答案

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

<小时/>

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

^\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
<小时/>

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.

<小时/>

关于java - 如何使用java将jmeter jtl httpSample/httpSample子子值替换为父值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57815940/

相关文章:

java - Maven 在 Docker 容器内下载时挂起

java - 如何改变HashSet中的值?

python - 负前瞻后跟空格

java - virtuoso-opensource : using jenaprovider, 如何从文件而不是从 URL 读取数据?

java - 对具有同一项目的不同版本的所有子目录运行 JUnit 测试

sql - 我是否在数据库中错误地使用了正则表达式?

python - 使用正则表达式替换文本文件中的多个条目

java - 如何通过http获取xml文件?

java.xml XMLConstants.FEATURE_SECURE_PROCESSING 无法解析或不是字段

java - Spring MVC 是否缓存在 Web 应用程序启动时未加载的 bean 文件?