Create WebSurge .NET Addins to extend Functionality

West Wind WebSurge supports creating Addins using .NET code which are placed in the Addins folder of the WebSurge installation folder. Using an addin you can intercept each request before and after it has been processed in a test, as well as accessing all requests and test results before an after a test has been executed.

Addin Execution is disabled by Default

By default addins are not allowed to execute, as they represent a potential execution threat to your application. You have to explicitly enable them via the AllowAddinExecution in the File -> Settings Configuration.

Addins support the following API hooks to execute:

  • OnBeforeRequestSent()
    Fired just before a request is sent. Allows to inspect the active request and optionally change the existing request headers and content about to be sent to server. Useful for adding additional content to requests, or to look for specific request content and transform it using dynamic code logic.

  • OnAfterRequestSent()
    Fired after a request has been sent to the server and the incoming server response data has been parsed into the request structure. Again you have full access to the request and can modify both the request and response headers and content.

  • OnLoadTestStarted()
    Fired just before a load test begins processing requests. Receives a list of requests and allows you to potentially modify the requests processed before the full load test begins.

  • OnLoadTestCompleted()
    Fired after a load test has completed and the results have been compiled. Receives the full result request list as well as the TestResult structure that summarizes test results.

Creating a WebSurge Addin

Here's a very broad overview of what you need to create a .NET addin for WebSurge:

  • Create a .NET 6 Class Library
  • Add a reference to WebSurge.Core.dll
  • Implement IWebSurgeAddin or inherit from WebSurgeAddinBase
  • Write your handler code for any of the methods
  • Copy the file into the <WebSurgeInstall>\Addins folder

A very simple addin looks something like this (not all methods have to be implemented):

public class SampleAddin : WebSurgeAddinBase
{
    public override bool OnBeforeRequestSent(HttpRequestData httpRequest, StressTester stressTester)
    {
        // add a header to the request headers
        httpRequest.AddHeader("rick-message",
            "Hello World " + TimeUtils.ShortDateString(DateTime.Now, showTime: true));
        return true;
    }

    public override void OnAfterRequestSent(HttpRequestData request, StressTester stressTester)
    {
        // add a header to the response headers
        request.ResponseHeaders += "rick-endofrequest-message: Goodbye world " + 
                                   TimeUtils.ShortDateString(DateTime.Now, showTime: true);
    }

    public override bool OnLoadTestStarted(IList<HttpRequestData> requests, StressTester stressTester)
    {
        // get information about requests used for test about to start
        var result = MessageBox.Show("About to start test with " + requests.Count + "requests");
        if (result != MessageBoxResult.Yes)
            return false;

        return true;
    }

    public override void OnLoadTestCompleted(IList<HttpRequestData> requests, TestResultView results, StressTester stressTester)
    {
        // Get Test Results and display them
        MessageBox.Show("This test ran " +
                        results.TestResult.RequestsPerSecond + " req/sec");
                        
        // requests holds the raw result requests                        
    }
}

For more info on creating an addin see:

See also

Creating a WebSurge Addin

© West Wind Technologies, 2014-2023 • Updated: 01/30/22
Comment or report problem with topic