Skip to content

Latest commit

 

History

History
110 lines (77 loc) · 3.32 KB

README.md

File metadata and controls

110 lines (77 loc) · 3.32 KB

code-timer

Build status nuget nuget

CodeTimer provides an easy way to add timing code to procedure-style methods. It provides visibility into the contribution of individual code segments within the method.

The following image shows a snippet from a Log which has both AppInsights and CodeTimer running.

AppInsights does the job of identifying the cost of the method while CodeTimer logs a cost breakdown for individual segments within the method.

AppInsights and CodeTimer working together

Read more about why you would use CodeTimer in this article.

Installation

Nuget
Install-Package codetimer

dotnet CLI
dotnet add package codetimer

Example

Creates a new CodeTimer, passing in a logger and specifying the expected ceiling time for the timing operation.

var codeTimer = new CodeTimer.CodeTimer("GetDetailsView", localLogger)
     {
         ExpectedMilliseconds = 1200
     };

Usage example

Call Mark() after to measure the performance cost of individual blocks to help identify which parts of a method are the most expensive.

public List<Blah> DoSomething() {

    var timer = new CodeTimer("DoSomething", localLogger)
    {
        ExpectedMilliseconds = 1200
    };

    var data = MakeDatabaseCall();
    timer.Mark("Get initial data");

    myWebService.CallSomething(data.Id);
    timer.Mark("Updated backend service");

    var coll = new List<Blah>();
    foreach(var foo in data.Bars) {
        coll.Add(foo.Blah);
    }
    timer.Mark("Created list of Blah's");

    // Stops the timer and logs the result 
    // Logs as Error if (Success == false)
    timer.Complete(); 

    return coll;
}

Formatting Example

The CodeTimer has a pluggable formatter for returning results and which is used to write to Logs if an ILogger is passed in to the constructor.

The example below displays an example result which is returned by the default Formatter.

var codeTimer = new CodeTimer("Case1", localLogger)
{
    ExpectedMilliseconds = 1200
};

// Do something
codeTimer.Mark("Start");  // 400ms

// Do another thing
codeTimer.Mark("Middle"); // 800ms

// Do last thing
codeTimer.Mark("End"); // 1200ms
codeTimer.Complete(); // 1201ms

Console.WriteLine(codeTimer.GetFormattedResult())

// Displays
Case1 timer failed.  Ran for 1201ms.  Expected 1000ms
 - Start: 400ms
 - Middle: 800ms
 - End: 1200ms

Meta

Darren Neimke – @digory

Distributed under the MIT license. See LICENSE for more information.

https://github.com/dneimke/code-timer/blob/master/LICENSE

Contributing

  1. Fork it (https://github.com/yourname/yourproject/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request