Write a file from a JMeter Script

This is somewhat of a strange post here but it’s something I need to remember how to do and because it was hard to find.  So if you’re not into JMeter please move on, there’s nothing to see here!

The problem, that I am solving here is, how to get unconventional data out of JMeter. When you run JMeter all performance data is neatly saved away in any way you like. But say you want to make a list of the customers and customer numbers you created. JMeter does not deliver such functionality out of the box. Well….there is one way but it’s ugly and tedious at best so I won’t even mention it here.

So I am under the assumption you know your way a little around JMeter and what it does.

  1. You will need to install BeanShell on all machines hat this script will execute on. Download bsh-core interpreter from http://www.beanshell.org/download.html
  2. Copy the file to the ~/lib directory of JMeter
  3. Have some variables ( ${ExampleVar} ) defined that you’d like to write to a file.
  4. In your script go to the sampler that you want to write the file and insert a BeanShell PostProcessor
  5. In the Script box insert the following script and change to hearts content.
 
import org.apache.jmeter.services.FileServer;

// Get the variable(s) from the JMeter script
tempVar = vars.get("ExampleVar");

// Static elements or calculations
part1 = "Car Speed is: ";
part2 = " km/h";

// Open File(s)
f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"\\carSpeed.csv", true); 
p = new PrintStream(f); 

// Write data to file 
p.println( part1 + tempVar + part2 );

// Close File(s)
p.close();f.close();

This will store a file directly on the Desktop. Enhance the content as needed. So far I have not noticed any adverse performance issues but I also haven’t tested it in too strenuous situations. I mainly use this when creating test data for performance tests.

It becomes a bit more difficult when you have a distributed test harness. You will need a process, by which you amalgamate all the separate files being created on the clients. So for now I’d suggest not using this when using remote JMeter instances.

Author: Oliver Erlewein

4 thoughts on “Write a file from a JMeter Script

  1. nice write up, had been searching for a while to find this solution. I made a change to the ‘write data to file’ line, so data would write three separate columns of the csv file:

    //write data to file
    p.println(part1+ “,” + tempVar+ “,” + part2);

  2. Thanks for great tip. By the way, there is an easier option to store variable names directly in JMeter’s .jtl results file without having to use Beanshell or any other scripting language. There is a property which control which variable(s) will be stored – “sample_variables”. So if you configure it to look like:

    sample_variables=ExampleVar

    “ExampleVar” variable value will be appended to each sampler information in the last column. The property can be set in at least 3 different ways:

    1. Adding “sample_variables=ExampleVar” line to user.properties file which lives under /bin folder of your JMeter installation
    2. Uncomment “sample_variables” in “jmeter.properties” file – same location /bin folder of JMeter installation and set its value to match your variable name(s)
    3. Provide “sample_variables” value as a command-line argument via -J key as

    jmeter -Jsample_variables=ExampleVar -n -t /path/to/testplan.jmx -l /path/to/results.jtl

    For more information on using, setting and overriding JMeter Properties see Apache JMeter Properties Customization Guide

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s