Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Tip/Trick

Multiple URL Stress

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
23 Oct 2015CPOL 8K   195   3  
Multiple URL Stress is a tool to test the performance of servers and websites, allowing you to test multiple URLs at the same time

Image 1

Image 2

Introduction

Multiple URL Stress is a tool to test the performance of servers and websites, allowing you to test multiple URLs at the same time.

It sets the number of requests for each of the sites, and few threads used, so the number of requests is multiplied by the number of configured threads.

It also has the ability to configure authentication and proxy server for your connection.

Using the Code

Browse the list of charged sites and initiates the configured number of threads for each.

C#
foreach (string url in listBoxUrls.Items)
{
    for (int i = 1; i <= Settings.CountThreads; i++)
    {
        Thread workerThread = new Thread(() => WorkerThreadProc(url));
        workerThread.Start();
    }
}

The WorkerThreadProc method is responsible for conducting and requests to sites.

Interlocked.Increment allows increased variable that is located in the main thread to reflect the partial results in the user view.

C#
public void WorkerThreadProc(string url)
{
    Interlocked.Increment(ref numWorkItems);
    int tries = 0;
    while ((!_shouldStop) && ((tries < Settings.CountRequest || (Settings.isforever))))
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.KeepAlive = true;
        try
        {
            request.Method = "GET";
            
            if (Settings.autenticate)
            {
                var cache = new CredentialCache();
                cache.Add(new Uri(url), "Basic", 
                new NetworkCredential(Settings.authUser, Settings.authPass));
                request.Credentials = cache;
            }
            
            if (Settings.proxy)
            {
                WebProxy myproxy = new WebProxy(Settings.proxyHost, Settings.proxyPort);
                if (Settings.proxyUser != "")
                    myproxy.Credentials = 
                    new NetworkCredential(Settings.proxyUser, Settings.proxyPass);
                request.Proxy = myproxy;
            }
            
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            
            if (response.StatusCode == HttpStatusCode.OK) 
            		Interlocked.Increment(ref count200);
            if (response.StatusCode == HttpStatusCode.Unauthorized) 
            		Interlocked.Increment(ref count401);
            if (response.StatusCode == HttpStatusCode.NotFound) 
            		Interlocked.Increment(ref count404);
            if (response.StatusCode == HttpStatusCode.NotModified) 
            		Interlocked.Increment(ref count304);
            response.Close();
        }
        catch
        {
            Interlocked.Increment(ref countFailures);
        }
        
        tries++;
        Interlocked.Increment(ref requests);
        Interlocked.Increment(ref requestsLastSec);
    }
    Interlocked.Decrement(ref numWorkItems);
}

History

  • 23rd October, 2015: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --