0

I want to stress my website with multiple access. To do that i created a windows based application that call 1000 times the website. Unfortunatly it work just for 2 call. This is the code:

    static void myMethod( int i)
    {
        int j = 0;

        try
        {
            string url = "";
            WebRequest wr = null;
            HttpWebResponse response = null;                                
            url = String.Format("http://www.google.com");
            wr = WebRequest.Create(url);
            //wr.Timeout = 1000;
            response = (HttpWebResponse)wr.GetResponse();                
            MessageBox.Show("end");
        }
        catch (Exception ex)
        {
            MessageBox.Show(j.ToString() + "   " + ex.Message);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 1000; i++)
        {
            ThreadStart starter = delegate { myMethod(i); };
            Thread thread = new Thread(starter);
            thread.Start();               
        }

    }
Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
user422688
  • 597
  • 2
  • 10
  • 20
  • What happens after two tries? Is any exception thrown? – granaker Jan 13 '12 at 09:48
  • 1
    You know that if this had worked, you would spawn 1000 messageboxes right? Also - I'm not sure if you are allowed to open a messagebox from a background thread. Can anyone confirm this? – Øyvind Bråthen Jan 13 '12 at 09:49
  • 2
    Why try and reinvent what is already available. http://support.microsoft.com/kb/231282 http://loadimpact.com/ – Lloyd Jan 13 '12 at 09:58
  • 1
    possible duplicate of [Best way to stress test a website](http://stackoverflow.com/questions/340564/best-way-to-stress-test-a-website), [ASP.NET Stress Testing](http://stackoverflow.com/questions/299518/asp-net-stress-testing#299784) –  Jan 13 '12 at 10:09

3 Answers3

2

Rather use the Free WCAT Tool to load test your ASP.NET page.

Also view this video [How Do I:] Load Test a Web Application

If you have Visual Studio 2010 Ultimate, see this link

I hope this helps.

Dirk Strauss
  • 630
  • 1
  • 8
  • 19
1

By default HttpRequest only allows two connections to the same host. You can change this by setting the DefaultConnectionLimit property.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • 1
    Indeed. The reason OP is butting-up so unexpectedly against the connection limit is because they are not closing/disposing the responses and therefore leaving open (but unreferenced) connections in the ServicePoint that will only be closed when GC comes along. It's also quite rude to the other end of the connection... hanging a connection without reading the response then not closing the connection promptly. – spender Jan 13 '12 at 10:17
0

Try disposing the IDisposable instances (i.e. the response) before continuing.

static void myMethod( int i)
{
    int j = 0;

    try
    {

        string url = String.Format("http://www.google.com");
        WebRequest wr = WebRequest.Create(url);
        using(HttpWebResponse response = (HttpWebResponse)wr.GetResponse())
        using(Stream responseStream = wr.GetResponseStream())
        {
            //handle response / response stream
        }                
        MessageBox.Show("end");  //this won't scale!!!
    }
    catch (Exception ex)
    {
        MessageBox.Show(j.ToString() + "   " + ex.Message);
    }
}
spender
  • 117,338
  • 33
  • 229
  • 351
  • This change doesn't make any sense. – Tamilmaran Jan 13 '12 at 09:54
  • @WAPGuy: Really? What doesn't make sense to you? Leaving the GC to initiate the closing of the request response is a good idea to you? – spender Jan 13 '12 at 09:58
  • @WAPGuy If you're going to say something like that, you should enlighten us all as to why you think that. – Simon Halsey Jan 13 '12 at 10:00
  • @WAP Guy "This change doesn't make any sense" - why not? The original approach doesn't make much sense, but the advice to dispose the IDisposable objects is sound. – Joe Jan 13 '12 at 10:04