Skip to end of metadata
Go to start of metadata

To create a  scriptable variable, implement the StresStimulus.Extensibility.IExternalVariable interface. It has the following interface definition:

bool EvaluateOnIteration { get; }//Return true if the mode of evaluation is on iteration. Otherwise the mode of evaluation is on request.

string GetValue(SessionContext context); //Returns the value of the variable for the given request consuming the variable.


The SessionContext has access to the test case iteration context information, which you may need in your custom code. It contains the following properties:

int RecordedSession { get; } //The session id.

string RecordedTestCase { get; } //The test case name.

int IterationNumber { get; } //The current iteration number.

int RequestNumber { get; } //The current request number in iteration.

int VUNumber { get; } //The VU number.

ExtractorRuntime ExtractorRuntime { get; } //The extractor runtime object for the VU.


ExtractorRuntime allows accessing runtime extractor values. It has the following methods:

string GetExtractorValue(string extractor); //Gets the given extractor’s value. If the extractor or its value is not found then returns an empty string.

void SetExtractorValue(string extractor, string value); //sets the given extractor’s value if the extractor exists.


Note: Your code can modify extractors created in StresStimulus. Be careful in doing so to avoid unintended consequences. If you wish to change extractors' value, take into account that your code will be called only on the evaluation events as described above.


Practical considerations

One instance of the class is created for every parameter in a test case. Here is an example: in a test case, a  scriptable variable is used to create 3 parameters in 3 different requests. In this case, 3 instances of the MyScriptVar class are created. They are instantiated at the moments when the scriptable variable is bound to the parameters. This happens when you open the test or on-demand when you create a parameter in the test designer. There are several consequences of this design:


  1. Every VU and every iteration during the test will use the same instance of the class associated with the corresponding parameter (3 instances per variable in this case).
  2. Class level variables persist throughout StresStimulus application session. If you restart the test without closing StresStimulus, the class level variables will be not initialized, which makes them less predictable. In most cases, it makes sense to use local method variables.
  3. During test runtime, GetValue() is called when evaluating the corresponding parameters. For example, if you put your test case in a loop which is executed twice, then every VU on every iteration will trigger six GetValue() calls.
  4. GetValue() is called asynchronously for every VU on every iteration. So if you have 100 VUs, each running 5 iterations, the total number of calls will be 100 * 5 * 6 = 3000. This method is not thread-safe, so use locks when using shared resources.
  5. The GetValue() call in Stimulus is located within a try/catch block. If your implementation throws an exception, it will be ignored, and the recorded value will be used in StresStimulus to parameterize the request.
  6. GetValue() execution time can have a performance impact on the StresStimulus load generator hit rate. Design GetValue() for performance.


Example

The following example creates a parameter that returns tomorrow's day.


using System;
using StresStimulus.Extensibility;

class Tomorrow : IExternalVariable
{
public Tomorrow()
{
//Do not edit this section.
}

#region IExternalVariable Members

/// <summary>
/// Return true if the variable will be evaluated once per VU iteration. Otherwise will be evaluated on every paramater.
/// </summary>
bool IExternalVariable.EvaluateOnIteration
{
get
{
return true;
}
}

/// <summary>
/// Return a value for the given session context.
/// </summary>
/// <param name="session">The session context object of the request consuming the variable.</param>
/// <returns>The source variable value.</returns>
string IExternalVariable.GetValue(SessionContext context)
{
DateTime tomorrow = DateTime.Now.AddDays(1d);

return tomorrow.ToString("dd");
}

#endregion
}


  • No labels