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:

IExternalVariable Members
	/// <summary>
    /// An external variable.
    /// </summary>
    public interface IExternalVariable
    {
        /// <summary>
        /// Return true if the variable will be evaluated once per VU iteration. Otherwise will be evaluated on every parameter.
        /// </summary>
        bool EvaluateOnIteration { get; }
        
        /// <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 GetValue(SessionContext context);
    }

 

This interface has one property and one method:

  • The GetValue() method should have all the logic of the scriptable variable and must return the parameter value. If no value (or null value) is returned, then StresStimulus will use the recorded value when sending the request. This method receives a SessionContext object object that describes the iteration and VU context of which this scriptable variable is called from.
  • The EvaluateOnIteration property should return true if this scriptable variable has the same value throughout each VU's iteration. This property should only return false if the scriptable variable is used in several requests in the test case and the value is expected to change from request to request.

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 MyScriptVar is used to create three parameters in 3 different requests. In this case, three 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 MyScriptVar 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 five 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 GetValue() 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 is a simple example of creating a parameter that returns a unique time-based id. For more complex examples see the subsequent sections.

 

Simple Example
using System;
using StresStimulus.Extensibility;
class Ticks : IExternalVariable
{
	public Ticks()
	{
		//Do not edit this section.
	}

	/// <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)
	{
		return DateTime.Now.Ticks.ToString();
	}
}

 

 

  • No labels