计算并写入正则表达式中提取的值的数量

时间:2016-11-09 09:53:11

标签: jmeter

我有一个json与下面的部分。我想写入提供者的数量并将其写在结果文件中。例如,这里有10个提供者。所以我的要求是在文件中写入数字10。我该怎么办?

 "providers":
    [
        {
            "MM_logofile":"agd.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"AGD",
            "name":"Agoda.com",
            "logo":"AGD.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"bks.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"BKS",
            "name":"Booking.com",
            "logo":"BKS.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"pln.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"PLN",
            "name":"Priceline.com",
            "logo":"PLN.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"ian.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"IAN",
            "name":"Hotels.com",
            "logo":"IAN-Other.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"gar.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"GAR",
            "name":"getaroom.com",
            "logo":"GAR.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"htv.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"HTV",
            "name":"HotelTravel.com",
            "logo":"HTV.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"exp.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"EXP",
            "name":"Expedia.dk",
            "logo":"EXP-DK.png",
            "isOfficial":false
        },
        {
            "MM_logofile":null,
            "MM_isOfficialWithoutLogo":true,
            "code":"BOO",
            "name":"Book with hotel",
            "logo":"",
            "isOfficial":true
        },
        {
            "MM_logofile":"hcl.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"HCL",
            "name":"HotelsClick.com",
            "logo":"HCL.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"vnn.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"VNN",
            "name":"Venere.com",
            "logo":"VNN.png",
            "isOfficial":false
        }
    ],

1 个答案:

答案 0 :(得分:1)

使用JSONPATH Extractor插件(使用Jmeter-plugin管理器添加插件)并将其作为子项添加到您获得JSON响应的采样器中。

使用表达式如下:

$.providers[*]

,引用名称为providers,如下图所示

enter image description here

将值捕获到参考变量后,您可以按照以下方式使用参考名称来获取匹配结果的数量(即提供者数量),如下所示:

${providers_matchNr}

enter image description here

将提供者的计数保存到文件中:

添加BeanShell Post处理器并添加以下代码:

    count = vars.get("providers_matchNr");

log.info(count);  // if you want to log something to jmeter.log file

// Pass true if you want to append to existing file
// If you want to overwrite, then don't pass the second argument
f = new FileOutputStream("C:\\Users\\USER_HP_2013_03\\Desktop\\New folder\\result.csv", true);
p = new PrintStream(f); 
this.interpreter.setOut(p); 
print(count);
f.close();

参考图片:

enter image description here

参考:

  1. Write extracted data to a file using jmeter