0

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:

[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;
}

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.

  • 1
    first off: please don't use that quirky `DateTime.Now - start` (there are soo many reasons against this), rather use `Stopwatch.StartNew()` and `.Stop()` and `.Elapsed` –  Oct 10 '14 at 12:09
  • second: you could handle all the http-requests with a pre- and post-handler and filtering according to a fileName-pattern –  Oct 10 '14 at 12:10
  • Why don't you use [`StopWatch` class](http://msdn.microsoft.com/library/system.diagnostics.stopwatch.aspx)? It is the best way to measure elapsed time. And you should always use [parameterized queries](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/). This kind of string concatenations are open for [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) attacks. – Soner Gönül Oct 10 '14 at 12:16
  • I'm a beginner in this stuff, so I don't really know which is better. Can I also get the start timestamp with the stopwatch? I need that too. and second, can you help me more with the pre- and post-handlers, I have no idea how to use them and I've been searching. An example maybe if you can, I would be really grateful – stackoverflowthebest Oct 10 '14 at 12:29
  • From my search I learned that the best way to do this would be to use a SOAP Extension. This way I can get the input information, start the time, and after the WebMethod, calculate the response time and save everything in the database. However I can't find any proper information on how exactly I get the fields of the soap request, and write a SOAP extension for this. I don't exactly know how to use the streams. Can anyone give me an example or point me somewhere? – stackoverflowthebest Oct 12 '14 at 22:35

0 Answers0