0

Here is the code in a paste

I'm using this code to stress test my server. I tried webpound X 1000 http://local_dev.com.

The code appears to work and is fine. Its <100 lines and isn't hard to understand. In short it uses webclient to hit the site once in blocking and async mode (hence the while loop next to it). Then it repeatedly hits the site counting how many succeeded.

The problem is on two different machines i get 600-850 in async mode (using 0 as the first arg) and randomly 160-350 in single thread mode.

My CPUs are idle while running this app. My webapp never hits 1% according to task manager, webpound is no more then 2% while the cpu total is never past 5%.

Whats the problem here? The server is nginx using fastcgi. I suspect the problem is webclient/C#. Whats another tool i may use that has the same idea? or how do i modify this so i can do hit more per second?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;


namespace webpound
{
    class Program
    {
        static long count = 0;
        static void Main(string[] args)
        {
            var s = new Stopwatch();
            var wc = new WebClient();
            int time = 100, mthread=0;

            if (args.Count() == 0)
            {
                Console.WriteLine("[1 (for single) [ms time]] url");
                return;
            }
            string url = null;
            bool useSingle = false;
            if (args.Count() == 1)
            {
                url = args[0];;
            }
            else if (args.Count() == 2)
            {
                useSingle = args[0] == "1";
                url = args[1];
            }
            else if (args.Count() == 3)
            {
                useSingle = args[0] == "1";
                time = int.Parse(args[1]);
                url = args[2];
            }
            else
            {
                throw new Exception();
            }

            var uri = new Uri(url);
            wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
            wc.DownloadStringCompleted += DownloadStringCompletedEventHandler;
            //wc.DownloadProgressChanged += DownloadProgressChangedEventHandler;
            wc.DownloadString(uri);
            wc.DownloadStringAsync(uri);

            while (System.Threading.Interlocked.Read(ref count)==0)
            {
                System.Threading.Thread.Sleep(1);
            }
            count = 0;
            WebException ex1 = null;
            s.Start();
            try
            {
                while (s.ElapsedMilliseconds < time)
                {
                    if (useSingle)
                    {
                        wc.DownloadString(url);
                        count++;
                    }
                    else
                    {
                        var wwc = new WebClient();
                        wwc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
                        wwc.DownloadStringCompleted += DownloadStringCompletedEventHandler;
                        var url2 = url + mthread.ToString();
                        wwc.DownloadStringAsync(new Uri(url2));
                        mthread++;
                        System.Threading.Thread.Sleep(1);
                    }
                }
            }
            catch(WebException ex)
            {
                ex1 = ex;
            }
            var mycount = count;
            s.Stop();
            if (ex1 == null)
            {
                Console.WriteLine("Finished with {0}/{1} in {2}", mycount,mthread, s.ElapsedMilliseconds);
            }
            else
            {
                var aa = new StreamReader(ex1.Response.GetResponseStream()).ReadToEnd();
                Console.WriteLine(ex1.Message);
                Console.WriteLine("Finished with {0}/{1} in {2}", mycount, mthread, s.ElapsedMilliseconds);
            }
        }

        static void DownloadProgressChangedEventHandler(object sender, DownloadProgressChangedEventArgs e)
        {
        }
        static void DownloadStringCompletedEventHandler(object sender, DownloadStringCompletedEventArgs e)
        {
            System.Threading.Interlocked.Increment(ref count);
        }
    }
}
  • 3
    WebClient has that 2 connection limit. [how to fix it](http://stackoverflow.com/questions/866350/how-can-i-programmatically-remove-the-2-connection-limit-in-webclient) – James Jul 26 '12 at 04:14
  • May I ask, why to write own stress tester, while there are free ones with many features, like http://www.loadui.org/ – Giedrius Jul 26 '12 at 06:03
  • I know this doesn't answer your question, but does provide a solution: there are free load testing tools available, such as Load Tester LITE: http://www.webperformance.com/load-testing/free-load-tester-lite.html – CMerrill Jul 27 '12 at 13:27

0 Answers0