Skip to content
Hintea Dan Alexandru edited this page Mar 30, 2016 · 2 revisions

A timer is basically a histogram of the duration of a type of event and a meter of the rate of its occurrence.

Timer Sample
private readonly Timer timer = 
    Metric.Timer("HTTP Requests",Unit.Requests);

public void ProcessRequest()
{
    using(timer.NewContext())
    {
        // Actual Processing of the request
    }
}
Keeping track of user values

Using the histogram's ability to track user values, the timer also has the ability to track for which user value a Min, Max or Last duration has recorded. The user value can be any string value (documentId, operationId, etc).

The Timer will record for example for which documentId the request took the longest:

private readonly Timer timer =
    Metric.Timer("Requests", Unit.Requests);

public void Process(string documentId)
{
    using (var context = timer.NewContext(documentId))
    {
        ActualProcessingOfTheRequest(documentId);

        // if needed elapsed time is available in context.Elapsed 
    }
}

The context returned by the timer.NewContext() can provide the elapsed time, which can be useful for intermediary logging purposes. The timer.NewContext() method allows you to pass in an action that will be executed upon disposal with the total elapsed time. This can also be useful for logging the final time.

After running a few requests, the output of the timer in text format looks like this:

    Requests
             Count = 14 Requests
        Mean Value = 1.86 Requests/s
     1 Minute Rate = 1.80 Requests/s
     5 Minute Rate = 1.80 Requests/s
    15 Minute Rate = 1.80 Requests/s
             Count = 14 Requests
              Last = 869.03 ms
   Last User Value = document-1
               Min = 59.90 ms
    Min User Value = document-6
               Max = 869.03 ms
    Max User Value = document-1
              Mean = 531.81 ms
            StdDev = 212.98 ms
            Median = 594.83 ms
              75% <= 670.18 ms
              95% <= 869.03 ms
              98% <= 869.03 ms
              99% <= 869.03 ms
            99.9% <= 869.03 ms

As you can see the timer recorded that the fastest document was document-6 (59.90 ms), the slowest document-1 (869.03 ms).