C#: Simple multithread website stress tool

You have probably searched online for a simple tool that could try and time your server response, handy for benchmarking especially when you are about to move to a new server.
You probably found that there are not much, but some cloud-based online services that actually can’t help much! Why? Cause simply you might end up stress testing a VM from another VM on the same set.

Solution was to write a custom simple multithread tool that you could:
1. Enter a url to be hit
2. Enter number of simultaneous threads
3. Enter number of concurrent hits per thread.
4. Start DoS attack 🙂

The complete working app can be found in my GitHub account: https://github.com/georgekosmidis/WebStressTool

In order to write the tool, i would need three basic things: create and start the threads, get a timed HttpWebResponse from within each thread, and finally update the UI not only from within each thread, but also at the end after everything has finished
These are the technics I used:

Step 1, Initialize all threads (with a delegate with the method to be invoked) and start them

    for ( int i = 0; i < numConThreads.Value; i++ ) {
        var thread = new Thread(
            () => {
                for ( int j=0; j < numTries.Value; j++ ) {
                    //Step 2, get the time returned
                    //Step 3, update ui about it
                }
            }
        );
    }
    foreach ( var t in _threads )
        t.Start();

Step 2, Get the HttpWebResponse and timed it (from within the thread)

var dt = DateTime.Now;
var request = (HttpWebRequest)WebRequest.Create( txtURL.Text );
...
using ( var response = (HttpWebResponse)request.GetResponse() ) {
    ...
    return (DateTime.Now - dt).TotalMilliseconds;
}

Step 3, Update UI (from within the thread)

Invoke( (MethodInvoker)delegate {
    //Update UI
} );

* Needless to say that the results of this tool are of course depended from your internet connection. A good idea is also to try and stress test a server locally, by uploading the tool with remote desktop connection and test the results there.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s