title: Measure Response Time ASP.net
body: <p>I have an ASP Web Service (.asmx) which simply gets a SOAP Request with a query string, executes it on the database, and returns either the data, or the rows affected. 
  I want to log the response time it takes the server to reply in a Log Table on the Database, but I don't know how I can get the request and reply times, so I can calculate it. The way I did it now is it the WebMethod, I get the current time, then execute the query, and then get the timespan and save it on the DB. This does however not measure the serialization time it takes the server when it returns an object and serializes it in XML, so it is less than the actual response time. Here is my code:</p>
  
  <pre><code>[WebMethod]
  public Table Select(string environment, string username, string password, string query)
  {
      DateTime start = DateTime.Now;
  
      Table table = Methods.Select(environment, username, password, query);
  
      string constring = ConfigurationManager.ConnectionStrings[environment].ConnectionString;
      OracleConnection con = new OracleConnection(String.Format(ConfigurationManager.ConnectionStrings[environment].ConnectionString, username, password));
      con.Open();
      TimeSpan time = DateTime.Now - start;
      OracleCommand cmd = new OracleCommand("INSERT INTO WS_REQUESTS VALUES (TIMESTAMP'" + start.ToString("yyyy-MM-dd HH:mm:ss.ffffff") + "', '" + username + "', '" + query + "', " + time.TotalMilliseconds + ", " + errorflag + ", '" + message + "')", con);
      int rows = cmd.ExecuteNonQuery();
      con.Close();
      return table;
  }
  </code></pre>
  
  <p>The Table is a DTO I created for the data, and Methods is the class where I have the query functions.
  Can anyone tell me how can I measure the response time as accurate as possible? The best would be as simple as possible, with code inside my service, at most something I can embed in my service, so that it is standalone, and not additional performance measuring tools I need to install on my server. Thanks.</p>