Posts

....
Technical Blog for .NET Developers ©

Tuesday, October 2, 2018

Stopwatch of System.Diagnostics

For the sake of performance, the response times of many processes should be tested

The namespace System.Diagnostics exposes diverse classes for this purpose, one of them is the class StopWatch, this class represents a stopwatch to measure the time delay of the synchronous processes

We can browse the code up to its definition, exposed below

     
    public class Stopwatch
    {
        public static readonly long Frequency;
        public static readonly bool IsHighResolution;
        public Stopwatch();
        public TimeSpan Elapsed { get; }
        public long ElapsedMilliseconds { get; }
        public long ElapsedTicks { get; }
        public bool IsRunning { get; }
        public static long GetTimestamp();
        public void Reset();
        public void Restart();
        public void Start();
        public static Stopwatch StartNew();
        public void Stop();
    }
    


In this example we are working on an Oracle database invoking a stored procedure to update all records in a table, and we need to know with high precission how much time it delays when it´s called from our web site

We have data of our Contacts and the next stored procs to retrieve and update the whole table

     
  create or replace
  PROCEDURE P_SELECT_CONTACTS
    (RES OUT PKG_CONTACTS.CURSOR_ALL)
  IS
  BEGIN
  
    OPEN RES FOR SELECT FirstName "First Name", LastName "Last Name", 
      Phone "Phone", EMail "E Mail", PreferredContactForm "Preferred Contact Form" 
    FROM C_CONTACTS
    ORDER BY FirstName, LastName;
    
    RETURN;
  
  END P_SELECT_CONTACTS;
  
  --
  
  create or replace
  PROCEDURE P_UPDATE_CONTACT_FORM
    (v_ContactForm C_CONTACTS.PreferredContactForm%TYPE,
     v_RecordsAffected OUT NUMBER)
  IS
  BEGIN
  
    UPDATE C_CONTACTS SET PreferredContactForm = v_ContactForm;
    v_RecordsAffected := sql%rowcount;
    
  END P_UPDATE_CONTACT_FORM;


In our web site this is the display



Now we update the table through the next code

     
using (connection = new OracleConnection(ConfigurationManager.ConnectionStrings["csMethod"].ConnectionString))
{
    System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
    stopWatch.Start();

    connection.Open();
    OracleCommand command = connection.CreateCommand();
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "P_UPDATE_CONTACT_FORM";
    OracleParameter pContactForm = new OracleParameter("v_ContactForm", OracleType.VarChar);
    pContactForm.Direction = ParameterDirection.Input;
    pContactForm.Value = ddContactForm.SelectedValue;
    OracleParameter pRecordsAffected = new OracleParameter("v_RecordsAffected", 
            OracleType.Number);
    pRecordsAffected.Direction = ParameterDirection.Output;

    command.Parameters.AddRange(new OracleParameter[] { pContactForm, pRecordsAffected });
    command.ExecuteNonQuery();

    Int64 recordsAffected = Convert.ToInt64(pRecordsAffected.Value);

    lblResult.Text = recordsAffected.ToString() + " records affected";

    stopWatch.Stop();
    string elapsedTime = stopWatch.Elapsed.ToString(@"mm\:ss\.ffff");

    lblStopWatch.Text = "Elapsed time: " + elapsedTime;
}


And the result is the next



As you can see, we have a measurement of round-trip time very accurate

<METHOD SOFTWARE © 2012>