Class VariableManager
Manages NeoLoad variables.
There are several types of NeoLoad variables:
- Declared variables
These variables are explicitly declared in the Variables panel in NeoLoad. They have a "value change policy" that defines when they should take a new value.
- Runtime variables
These variables are set at runtime, using Variable Extractors for example. Their values must be explicitly changed to another value.
- Shared Queues
These variables are explicitly declared in the NeoLoad Variable Manager. They work on the "Producer/Consumer" model, meaning that values generated from variables or extractors can be added to the queue then later consumed.
The Variable Manager allows you to:
- get and set the values of runtime variables
- get and change the values of declared variables
Synopsis
public class VariableManager {
// Public Methods
public boolean addSharedValue(String variableName,
String value);
public void changeValue(String variableName);
public boolean createSharedQueue(String variableName,
int queueSize,
int timeout);
public boolean createSharedQueue(String variableName,
int queueSize,
int timeout,
String filename,
char delimiter,
boolean swapLoaded,
boolean swapDumped);
public int getSharedQueueSize(String variableName);
public String getValue(String variableName);
public String parseString(String text);
public String peekSharedValue(String variableName);
public String pollSharedValue(String variableName);
public void setValue(String variableName,
String value);
}addSharedValue(String, String)
public boolean addSharedValue(String variableName,
String value);Parameters
- variableName
the variable name.- value
the value to be added.- return
true if the value has been successfully added to the queue and the maximum capacity has not been exceeded.Adds a value to the shared queue.
If the queue has reached its maximum capacity, the value will not be added and the function returns false
changeValue(String)
public void changeValue(String variableName);
Parameters
Change the value of a declared variable, such as a File variable or a Counter variable.
The variable name should be used, not the variable expression: use 'varname' and NOT '${varname}'. the name can be compound, for example: "data_for_${login}" if variable "data_for_jsmith" is defined.
createSharedQueue(String, int, int)
public boolean createSharedQueue(String variableName,
int queueSize,
int timeout);Parameters
- variableName
the variable name.- queueSize
the queue size.- timeout
the maximum read wait time (in ms).- return
true if the queue has been successfully created and the variable does not exist.Creates a queue that is shared between the Virtual Users.
createSharedQueue(String, int, int, String, char, boolean, boolean)
public boolean createSharedQueue(String variableName,
int queueSize,
int timeout,
String filename,
char delimiter,
boolean swapLoaded,
boolean swapDumped);Parameters
- variableName
the variable name.- queueSize
the queue size.- timeout
the maximum read wait time (in ms).- filename
the CSV swap file location. If the file does not exist, it is created automatically.- set
the CSV column separator. Optional- swapLoaded
true if the queue is to be populated from the swap file at the start of the test.- swapDumped
true if the queue is to be saved to the file at the end of the test.- return
true if the queue has been successfully created and the variable does not exist.Creates a queue that is shared between the Virtual Users associated with a CSV swap file.
getSharedQueueSize(String)
public int getSharedQueueSize(String variableName);Parameters
- variableName
the name of the shared queue.- return
the size of the shared queue.Returns the number of items in the shared queue, -1 if the queue does not exist.
getValue(String)
public String getValue(String variableName);Parameters
Returns the value of a variable or null if the variable does not exist.
The variable name should be used, not the variable expression: use 'varname' and NOT '${varname}'. the name can be compound, for example: "data_for_${login}" if variable "data_for_jsmith" is defined.
- Warning: The result is always a String. If the variable value represents a number, we may need to explicitly convert it to a number before using it. Use parseInt(varname) or parseFloat(varname) for example.
- // variable value is "45"
var varAsString = context.variableManager.getValue("myVar");
var varAsInt = parseInt(varAsString);
varAsString = varAsString + 1; // New value is "451"
varAsInt = varAsInt + 1; // New value is "46"parseString(String)
public String parseString(String text);Parameters
- text
the string to be parsed.- return
the parsed text. Returns the same text even if it contains no variables; never returns null.Evaluate a string that contains variables. For example, "the counter value is: ${counter}" will return "the counter value is: 7".
Variables can be compound, for instance${var1_${var2}}
peekSharedValue(String)
public string peeksharedvalue(string variablename);Parameters
Returns the first value in the shared queue without deleting it, or null if the queue does not exist.
If the queue does not contain any values, the value returned will be in the form ${variableName}
- Warning: The result is always a character string. If the value represents a number, you must explicitly convert it to a number before carrying out any arithmetic operations. Use parseInt(varname) or parseFloat(varname), for example:
- // The value of the variable is "45"
var varAsString = context.variableManager.peekSharedValue("myVar");
var varAsInt = parseInt(varAsString);
varAsString = varAsString + 1; // The new value is "451"
varAsInt = varAsInt + 1; // The new value is "46"pollSharedValue(String)
public String pollSharedValue(String variableName);Parameters
Returns the first value in the shared queue and deletes it, or null if the queue does not exist.
If the queue does not contain any values, the value returned will be in the form ${variableName}
- Warning: The result is always a character string. If the value represents a number, you must explicitly convert it to a number before carrying out any arithmetic operations. Use parseInt(varname) or parseFloat(varname), for example:
- // The value of the variable is "45"
var varAsString = context.variableManager.pollSharedValue("myVar");
var varAsInt = parseInt(varAsString);
varAsString = varAsString + 1; // The new value is "451"
varAsInt = varAsInt + 1; // The new value is "46"setValue(String, String)
public void setValue(String variableName,
String value);Parameters
Assigns a value to a runtime variable. The variable is created if it does not exist. Note that the variable name should be used, not the variable expression: use 'varname' and NOT '${varname}'. Nevertheless, the name can be compound, for example: "data_for_${login}" if variable "data_for_jsmith" is defined.
When using the name of a declared variable, a runtime variable will be created that will override the declared variable. Overriding declared variables is not recommended.
Home