Best Solutions: Saving Values, Writing to External Files During a Load Test

During your load test, you may want to log information related to your script. This is useful if you want to check data afterward, whether it was used or modified during your load test.

There are a few ways to log data during your tests:

  1. You can use the NeoLoad logger JavaScript class to log your information. The data will be stored in the load generator log files. logger.info(myMessage);
    1. Note: The logging level can be configured in the project settings in the Runtime parameters section.
  2. You can write to .bin files directly from NeoLoad (only for responses, see 2nd part of this document).
  3. You can use a specific JavaScript with a defined variable; The data will be stored in a file of your choice. Create this JavaScript in the NeoLoad JavaScript libraries folder:

//Script to write variable to external file

var lock = new java.util.concurrent.locks.ReentrantLock();

var date = context.variableManager.getValue(“CurrentDate”);

if (date==null)

{

context.fail(“Variable ‘CurrentDate’ not found”);

}

function writeFile(text)

{

lock.lock();

var writer = new java.io.FileWriter(“c:\\log.txt”,true);

writer.write(date+”;”+context.currentVU.id+”;”+text);

writer.write(“\r\n”);

writer.close();

lock.unlock();

}

Note: if you use it inside the JavaScript library, copy all the code inside the function otherwise the compilation won’t work.  If you use all the code inside a NeoLoad JavaScript you can leave it as is.

Pre-requisite: “CurrentDate” is a NeoLoad variable (see Variable manager, create it) that will display the current date. It’s optional and can be replaced with any variable needed.

Note: “context.currentVU.id” will display the instance of the VU, it’s also optional. And then insert this JavaScript to use the function:

 //Script to get variable from running test

var cpt = context.variableManager.getValue(“StringNameFooBar”);

if (cpt==null)

{

context.fail(“Variable ‘StringRandom’ not found”);

}

writeFile(cpt+”;was the value from my test”);

Note: “StringRandom” is a Neoload variable, it’s optional and can be replaced with any other variable needed.

For more information, see “NeoLoad Javascript API” in Neoload documentation.

Writing Data via the GUI

In NeoLoad, it is possible to write content to a file from the GUI interface of a specific response given during playback or test.  The Response storage checkbox makes it possible to save the content of the response in a .bin file. This function stores the content of the response on the machine executing the Virtual Users to check it externally. Here’s how:

  1. Create a test recording in a project to use.
  2. To define a file to write to and select the option “Write response to a file” under the Advanced Settings tab of one of the recorded actions. (Navigate by double-clicking on a node endpoint and clicking “Advanced”). The dialogue looks like this:
  3. After selecting the checkbox to write to a file a .bin file is provided in the /responses/ directory
    1. The option “Write response to a file” must be selected to enforce the storage function:
    2. The field “Path and template of the file to create” contains the file path relative to the storage folder —%AppData%\Neotys\NeoLoad\v5.4— as described in NeoLoad files. The field also includes the template of the default filename response{ID}.bin. The {ID} variable is replaced with a unique integer. A single file is generated each time the request is executed.
  4. The optional field “Store evaluated file path in the variable” may be used for the Virtual User variable (runtime variable), which will contain the absolute path of the storage files generated during the test campaign.
  5. The option “Delete the file when the test stops” must be cleared to keep the files once the test is over. When the options box is selected, the files are available when the test is running only. Run the test and view the selected .bin files for review of data exported during the test.

Writing to a File with Command-Line Action

The goal of this technique is to capture an extraction and write this value to a file. NeoLoad has an advanced action, under the “Launchers” section, called “command line.”

Combining this technique together with a shell script to better enhance the data manipulation should I need to. To use the command line, drag the action into your User Path, and add the required parameters. As you can see in the figure below, I’m calling the shell script and then pushing data from the extraction (variable) that I’m getting from the response.

Here is the simple shell script that is being called:

#!/bin/bash

echo $1 >>/Users/neotys/Desktop/nl_text.txt

Next Steps: Review Documentation or Talk to Us

  • Check out all of the content included within NeoLoad documentation.
  • Want to discuss further, contact support.

Learn More about Performance Testing & Load Testing

Discover more load testing and performance testing content on the Neotys Resources pages, or download the latest version of NeoLoad and start testing today.

Leave a Reply

Your email address will not be published. Required fields are marked *