Saturday, July 30, 2011

Generate thousand of request

Shout it kick it on DotNetKicks.com
Lastly I was working on the project where I have to designed web page which is exposing some set of the functions and which is get consume by some external application like java and flex. It's worked fine and providing the output I want.

But now I want to perform the load test, so for that I want to generate thousand of request which consume the page and show me how it works. But the problem is how to generate the request because I don't have that many people who can make request at given time.

Solution for this problem I make use of the Thread class and loop it to the no. of request I have to generate as you see in below code.
for (int i = 0; i < 100; i++)
{
    Thread thread1 = new Thread(new ThreadStart(WorkThreadFunction1));
    thread1.Start();
}
Now the secondly I made use of HttpWebRequest class which allow me to call the page and to get the response. You can get more information about HttpWebRequest.
public void WorkThreadFunction1()
{
   try
   {
     string param = string.Empty;
     HttpWebRequest oWebReq =     
      (HttpWebRequest)WebRequest.Create("http://mywebsite/WebForm1.aspx" + param);
     HttpWebResponse oWebResp = (HttpWebResponse)oWebReq.GetResponse();
     StreamReader oStream = new StreamReader(oWebResp.GetResponseStream(),     
                                                     System.Text.Encoding.ASCII);
     Console.WriteLine(oStream.ReadToEnd());
   }
   catch (Exception ex)
   {
      // log errors
   }
}
As you can see in the above code I am making the call to the my page and writing the response to the console window.
Note:
Here WebForm1 is entry point based on the parameter passed to it , it calls the different function and may be the pages in your site.
Here I am making request to one single page but you can write you own script to call the different page and pass the parameter to it.

Summary
So you can easily make no. of request without need of the no. of people. But this thing depends on the requirement in you case.

2 comments:

  1. Nice post man.. and congratulations on completing 50 C# posts :)

    ReplyDelete