Skip to end of metadata
Go to start of metadata

A scriptable variable can access the runtime extractor values using the ExtractorRuntime object available from the SessionContext object. This is useful in situations where it necessary to emulate javascript actions that the browser would otherwise perform. See the example below.

 

ExtractorRuntime
/// <summary>
/// A virtual user's extractor runtime.
/// </summary>
public class ExtractorRuntime
{   
    /// <summary>
    /// Returns the extractor's current value for the VU.
    /// </summary>
    /// <param name="extractor">The name of the extractor.</param>
    /// <returns>The value of the extractor. Returns an empty string if the extractor is not found.</returns>
    public string GetExtractorValue(string extractor);

    /// <summary>
    /// Set the extractor's value for the current VU iteration.
    /// </summary>
    /// <param name="extractor">The name of the extractor.</param>
    /// <param name="value">The value of the extractor to set/</param>
    public void SetExtractorValue(string extractor, string value);
}

Changing extractor values

In some cases, it may be necessary to Create a scriptable variable with the value generated using the extractor's value for the current iteration. You can use the SetExtractorValue() method to do so. 

Example

The following example returns the value of a login key that is a combination of a server defined prefix and the current epoch timestamp. In the browser environment, the server sends a key prefix, and then some client javascript function would take the prefix and add the current epoch timestamp to it to be used in subsequent requests. This behavior can be replicated in StresStimulus using the following code. For this example assume that we created an extractor named KeyPrefix

 

Extractor example
class ExtractorKeyVariable : IExternalVariable
{
    public ExtractorKeyVariable()
    {
        //Do not edit this section.
    }
 
    /// <summary>
    /// Return true if the variable will be evaluated once per VU iteration. Otherwise will be evaluated on every parameter.
    /// </summary>
    bool IExternalVariable.EvaluateOnIteration
    {
        get
        {
            return true;
        }
    }
    
    /// <summary>
    /// Return a combination of server defined prefix and current timestamp.
    /// </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)
    {
        string prefix = context.ExtractorRuntime.GetExtractorValue("KeyPrefix");
        if (!string.IsNullOrEmpty(prefix)) //check this to make sure the server did in fact send the prefix.
        {
            return string.Format("{0}_{1}", prefix, ToUnixTime(DateTime.Now));
        }
        return null;
    }
 
    /// <summary>
    /// Returns given DateTime to epoch time format.
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    static long ToUnixTime(DateTime date)
    {
        var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return Convert.ToInt64((date - epoch).TotalMilliseconds);
    }
}

Note

In the above example, the extractor KeyPrefix must be extracted from a response that comes before the request where ExtractorKeyVariable scriptable variable is used.

 

 

 

  • No labels