使用Jmeter中的Groovy从csv创建Dynamic Json [英] Create Dynamic Json from csv using Groovy in Jmeter

查看:111
本文介绍了使用Jmeter中的Groovy从csv创建Dynamic Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Groovy脚本使用CSV数据创建json,并将其传递到jmeter的请求正文中,但我无法创建数据:

I am trying to create a json using CSV data using Groovy scripting and pass the same in the request body in jmeter but I am not able to create the data:

CSV Created:

0.0.0.0,255.255.255.255
10.0.0.1,255.0.0.0
10.0.0.2,255.0.0.1

Request body needed as :

{"Rule":{"__type":"AndroidSamsungDeviceRelocationRule","RuleId":0,"Name":"Test","DeviceFamily":6,"Targets":{"Groups":[{"Id":"0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000"}],"Devices":[]},"Priority":0,"IsEnabled":true,"StartDate":"/Date(1536856632768)/","EndDate":null,"Mappings":[{"RelocationTarget":{"Id":"0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000","Name":"100MB","Path":"\\\\100MB\\","PathVisible":"\\\\100MB\\"},"IPRange":[{"From":"0.0.0.0","To":"255.255.255.255"}...]}],"EnrollmentCertificateId":null,"EnrollmentCertificateName":""}}

在单个请求中,需要csv中的参数的IP地址.

Need the IP address for parameters from and to that are their in the csv in a single request.

我尝试过的代码是:

@Grab('com.xlson.groovycsv:groovycsv:1.3')
import static com.xlson.groovycsv.CsvParser.parseCsv

fh = new File('examples/data/process_csv_file.csv')
def csv_content = fh.getText('utf-8')

def data_iterator = parseCsv(csv_content, separator: ';', readFirstLine: true)

for (line in data_iterator) {
    sum = line[0] as String
    destination = line[1] as String
}

def builder = new groovy.json.JsonBuilder()

builder({
   Rule:{
      __type:"AndroidSamsungDeviceRelocationRule",
      RuleId:0,
      Name:"Test",
      DeviceFamily:6,
      Targets:{
         Groups:[
            {
               Id:"0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000"
            }
         ],
         Devices:[

         ]
      },
      Priority:0,
      IsEnabled:true,
      StartDate:"/Date(1536856632768)/",
      EndDate:null,
      Mappings:[
         {
            RelocationTarget:{
               Id:"0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000",
               Name:"100MB",
               Path:"\\\\100MB\\",
               PathVisible:"\\\\100MB\\"
            },
            IPRange:[
               {
                  "From":"sum",
                  "To":"destination"
               }
            ]
         }
      ],
      EnrollmentCertificateId:null,
      EnrollmentCertificateName:""
   }
})

sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('',builder.toPrettyString(),'')
sampler.setPostBodyRaw(true);   

当我通过JSR223预处理器运行此程序时,我收到的错误请求为HTTP 400.

When I am running this through JSR223 Preprocessor I am getting blank request with error HTTP 400.

我需要为CSV中可用的IP数量创建一个动态IPRange循环

I need to create a dynamic IPRange loop for number of IPs available in CSV

推荐答案

我认为您不能使用 JSR223测试元素,因此您的测试在一开始就失败了.

I don't think you can use @Grab annotation in JSR223 Test Elements therefore your test fails at the early beginning.

您可以尝试使用以下代码,这些代码应生成您要查找的JSON,而无需任何外部依赖项:

You can try using below code which should generate the JSON you're looking for without any external dependencies:

def builder = new groovy.json.JsonBuilder()

@groovy.transform.Immutable
class IPRange {
    String From
    String To
}

def ipRanges = new File("examples/data/process_csv_file.csv").readLines().collect { line -> new IPRange(line.split(",")[0], line.split(",")[1]) }
builder.Rule(

        __type: "AndroidSamsungDeviceRelocationRule",
        RuleId: 0,
        Name: "Test",
        DeviceFamily: 6,
        Targets:
                [
                        Groups :
                                [
                                        [
                                                Id: "0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000",
                                        ]
                                ],
                        Devices:
                                [

                                ]
                ],
        Priority: 0,
        IsEnabled: true,
        StartDate: "/Date(1536856632768)/",
        EndDate: null,
        Mappings:
                [
                        [
                                RelocationTarget:
                                        [
                                                Id         : "0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000",
                                                Name       : "100MB",
                                                Path       : "\\\\100MB\\",
                                                PathVisible: "\\\\100MB\\"
                                        ],
                                IPRange         : ipRanges.collect() {
                                    [
                                            From: it.From,
                                            To  : it.To
                                    ]
                                }

                        ]
                ],
        EnrollmentCertificateId: null,
        EnrollmentCertificateName: ""

)

sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toPrettyString(), '')
sampler.setPostBodyRaw(true);

更多信息:

  • Groovy: Parsing and producing JSON
  • Apache Groovy - Why and How You Should Use It

这篇关于使用Jmeter中的Groovy从csv创建Dynamic Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆