locked
How to measure http request time in multithreads

    Question

  • Hi.

    I'm using C# and .Net 2.0 And visual studio 2005.

    I want to create a "load test" tool for my web server. the tool should open a few threads, and each thread will post http request, and download the response.

    My problem is that I want to messure the time it takes for each thread.

    As I see it I have to lock the thread before i take the time, and release the lock after i recieve my response and stop taking time.

    The problem is that If I'll do that, I will not have a few requests simultaneously on my server.

    The code I used is:

    lock (new Object())
    {
      reqTime = System.
    DateTime.Now;
      response = (
    HttpWebResponse)request.GetResponse();
      sr =
    new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
      fileString = sr.ReadToEnd();
      resTime = System.
    DateTime.Now;
    }

    Friday, September 08, 2006 6:04 AM

All replies

  • If you lock on a local  object, you're not locking at all (every thread gets his own new lock). You would need to provide a sync lock as an instance field of your class to synchronize your worker threads.

    You're much better off without locking anyway -- simply store the time in local variables and put the actual execution time in a synchronized hash table (see http://msdn2.microsoft.com/en-us/library/system.collections.hashtable.synchronized.aspx).

    Friday, September 08, 2006 1:55 PM
  • Thank you for your answer.

    My experience is very little regarding threads, and it based on C++.

    My concern is that I need the time to be accurate as possible. The problem is that I don't know how to get the actual execution time, I'm afraid that I'll start measure time, and the dispatcher will change the current running thread before the http request was made at all - so the actual time will include the time spent on waiting for other threads.

     

    Saturday, September 09, 2006 7:19 AM