Monday, February 23, 2009

run a standalone web test application

Introduction

Hello, VS.Net 2008 has a utility tool for Performance Testing. Simply it listens for web requests and response and generate code according to these requests. But what could we do if we want to execute these requests in another application. it is required to make some enhancement on existing code. As code generator dependes on HttpWebTestRequest, we need to wrap it to HttpWebRequest.



Solution





ASsume code generated is GetRequestEnumerator, which returns all requests recorded from visual studio testing tool.


public override IEnumerator GetRequestEnumerator()
{
WebTestRequest request1 = new WebTestRequest("http://www.masrawy.com/");
request1.Encoding = System.Text.Encoding.GetEncoding("utf-8");
request1.ThinkTime = 300;
yield return request1;
request1 = null;
WebTestRequest request2 = new WebTestRequest("http://www.masrawy.com/");
request2.Encoding = System.Text.Encoding.GetEncoding("utf-8");
request2.QueryStringParameters.Add("hl", "en", false, false);
request2.QueryStringParameters.Add("q", "gmail", false, false);
request2.QueryStringParameters.Add("meta", "", false, false);
request2.QueryStringParameters.Add("aq", "0", false, false);
request2.QueryStringParameters.Add("oq", "Gma", false, false);
yield return request2;
request2 = null;
}





we will make our cusomized execution function


void execute()
{
IEnumerator requests = GetRequestEnumerator();
int count = 0;
while (requests.MoveNext())
{
try
{
#region wrapping from WebTestRequest to WebRequest
Console.WriteLine("In request NUM " + count++);
WebTestRequest currentRequest = requests.Current;
HttpWebRequest httpRequest;
string URL = currentRequest.Url;
if (currentRequest.QueryStringParameters.Count > 0)
{
for (int i = 0; i < i ="="" httprequest =" (HttpWebRequest)HttpWebRequest.Create(URL);" method =" currentRequest.Method;" credentials =" CredentialCache.DefaultNetworkCredentials;" impersonationlevel =" System.Security.Principal.TokenImpersonationLevel.None;" timeout =" 10000;" keepalive =" true;" encoding =" new" posteddata =" currentRequest.Body.ToString();" contentlength =" PostedData.Length;" stream =" httpRequest.GetRequestStream();" data =" encoding.GetBytes(PostedData);"> 0)
{
Console.WriteLine("Thinkin in " + currentRequest.ThinkTime);
Thread.Sleep(currentRequest.ThinkTime);
}
#endregion
# region get response
WebResponse response = httpRequest.GetResponse();
Console.WriteLine("Succeed Response");
Console.WriteLine("**************\n");
#endregion
}
catch (Exception ex)
{
Console.WriteLine("Failed Response"+ex.Message);
}
}
Console.WriteLine("All Requests are Finished");
}





We can take that function and call it through main function, like that


public static void Main()
{
Console.WriteLine("welcome, Press a key to begin sending requests");
Console.Read();
try { new WebTest1Coded().execute(); }
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.Read();
Console.Read();
}


Result
we can get that application and measure its execution performance through any measurement too.

Thanks and waiting for ur comment

4 comments:

  1. Thanks for posting, I was looking for same thing :-)

    ReplyDelete
  2. I blog frequently and I really appreciate your content.

    The article has truly peaked my interest. I will take
    a note of your site and keep checking for new details about once a week.
    I opted in for your RSS feed too.

    Feel free to visit my weblog ... Arabic Books

    ReplyDelete