Shahar Wiener
Shahar Wiener

Reputation: 53

How to Duplicate file using JMeter

I am trying to duplicate a file using JMeter,

The scenario:

  1. Loading a JSON file. For example, {"name":"John","age":"$age_place"}
  2. Modify one property - age_place from 1 to 20
  3. Save each modified iteration into a separate file

I have tried to do that with Simple Data Writer but it didn't work.

Upvotes: 1

Views: 488

Answers (2)

Dmitri T
Dmitri T

Reputation: 168217

If you need to generate 20 files with different age you can do it using any of JSR223 Test Elements. Example Groovy code would look like:

def json = new groovy.json.JsonSlurper().parseText("{\"name\":\"John\",\"age\":\"\"}")
def builder = new groovy.json.JsonBuilder(json)
1.upto(20, {
    builder.content.age= "${it}"
    def writer = new File('file' + "${it}" + ".json").newWriter()
    writer << builder.toPrettyString()
    writer.close()
})

Once you execute your test it will create the following files in the "bin" folder of your JMeter installation:

  • file1.json - with the age of 1
  • file2.json - with the age of 2
  • ...
  • file20.json - with the age of 20

References:

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58892

You have JSR223 Elements as Sampler or Pre processor which you can add powerful script,

The easiest is to replace age_place with your value, for example if saved in variable age_place:

f = new FileOutputStream("c:\\temp\\template.json", false);
p = new PrintStream(f); 
this.interpreter.setOut(p); 
print("{\"name\":\"John\",\"age\":\"age_place\"}".replaceAll("age_place", vars.get("age_place")));
f.close();

Upvotes: 1

Related Questions