0

The below request being the base request,

    [
    {
     "name": "Test1",
     "description": "testings",
    "unitname": simple,
    "ID": 02,
      "val": "item"
      },
     {
    "name": "Test2",
   "description": "testing",
    "unitname": simple3,
    "ID": 23,
    "val": "item"
    }
  ]

I want to simulate this with multiple (1000) 'child' sections like the below in a single JMeter request: It should create 1000 data set(name,description,unitname,ID,val) with unique values and then post the request. Instead of manually creating multiple tags, can i automate it or create a script to generate this automatically ?

    [
    {
   "name": "Test1",
    "description": "testings",
    "unitname": simple,
     "ID": 02,
     "val": "item"
     },
      {
     "name": "Test2",
     "description": "testing",
      "unitname": simple3,
       "ID": 23,
       "val": "item"
     }

         {
         "name": "Test3",
             "description": "testing",
            "unitname": simple4,
            "ID": 23,
            "val": "item"
            }
       {
          "name": "Test4",
         "description": "testing",
         "unitname": simple6,
           "ID": 23,
           "val": "item"
             } 
           ]

Any help please?

Lava
  • 3
  • 2

1 Answers1

0
  1. Add JSR223 PreProcessor as a child of your request where you need to send the generated JSON
  2. Put the following code into "Script" area:

    import groovy.json.JsonBuilder
    import groovy.json.internal.LazyMap
    import org.apache.commons.lang3.RandomStringUtils
    
    def data = new ArrayList()
    
    1.upto(1000, {
        def entry = new LazyMap()
        entry.put('name', 'test' + it)
        entry.put('description', RandomStringUtils.randomAlphabetic(10))
        entry.put('unitname', 'simple')
        entry.put('ID', it)
        entry.put('val', 'item')
        data.add(entry)
    
    })
    
    def builder = new JsonBuilder()
    
    builder(
            data.collect {
                [
                        name        : it.get('name'),
                        descrtiption: it.get('description'),
                        unitname    : it.get('unitname'),
                        ID          : it.get('ID'),
                        val         : it.get('val')
                ]
            }
    )
    
    
    sampler.setPostBodyRaw(true)
    sampler.addNonEncodedArgument("", builder.toPrettyString(), "")
    
  3. Tick Cache compiled script if available box

  4. Make sure groovy is selected in "Language" dropdown

That's it, the above script will generate a JSON Array and set it as the HTTP Request sampler's body.

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks for the response, @Dmitri T. Can you tell me how do i set the response of the above JSON script in the POST request ? – Lava Jan 25 '18 at 18:52
  • I have created a JSR233 PreProcessor as suggested by you above, but the POST data in my HTTP Request is still blank in the 'View Results Tree'. Am i missing something? should i use the return from the groovyscript in the HTTP Request POST Data section? – Lava Jan 25 '18 at 19:00
  • I did take a look at your old post on how to use JSR Prepocessor https://stackoverflow.com/questions/36444737/how-to-use-jsr-223-preprocessor-in-jmeter. This one works well.. but i'm unable to use the baove groovy script.. any help please? My HTTP POST request body shows blank data on the View Results Tree. – Lava Jan 25 '18 at 19:07
  • Corrected last couple of lines, kindly try again – Dmitri T Jan 25 '18 at 21:29
  • one question, what does the variable 'it' refer to? entry.put('name', 'test' + it) – Lava Jan 26 '18 at 18:41