• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    C#
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Performance Monitoring and instrumentation for .NET (4.52+ and Core)

NuGet version AppVeyor

PerfIt!

Join the chat at https://gitter.im/perfIt-dotnet/Lobby

A light-weight library/framework for code/performance instrumentation in .NET (both .NET 4.52+ and .NET Core)

Core Concepts

  • Instrumentation: an aspect-oriented or closure-based wrapping of your code to measure how it performances and capture its context
  • SimpleInstrumentor: core class within PerfIt responsible for all instrumentations
  • Trace: result of an instrumentation. Contains time taken plus both configuration-based and runtime-based context
  • Tracer: A class/system that receives traces and can publish traces to storage/transports (e.g. Console, EventHub, ETW)
  • Zipkin: A distributed tracing system/framework/specification developed by Twitter
  • Zipkin Emitter: A hub where all Zipkin traces within a process is received
  • Zipkin Dispatcher: A class that receives Zipkin traces from the emitter and sends them to a transport/storage
  • EventSource: A class in .NET Framework where it raises ETW events under windows and LTTng events in Linux

Breaking changes in version 5.0.0

PerfIt 5 now supports both .NET 4.52+ and .NET Core, and it was an opportunity to get rid of some tech debt and clean up the interfaces. If you have been using previous versions of PerfIt, apart from some minor changes, your code will work as it was. But here are main changes:

  • PublishEvent property no longer exists. PerfIt always publishes EventSource events although you may remove it from the Tracers dictionary to suppress that.
  • Everything to do with Performance Counters on PerfItRuntime has moved to CounterInstaller.
  • Instead of InstrumentationContext string, now there is InstrumentationContext object where you can add a bit more context.
  • PerfitClientDelegatingHandler has moved to PerfIt.Http project/package to support both .NET 4.52+ and .NET Core
  • There is a Name property now on IInstrumentationInfo
  • There is a global static event (PerfItRuntime.InstrumentorCreated) whenever an Instrumentor is created. Ideal for modifying instrumentors that are created using attributes such as adding/removing tracers.

FAQ

What is PerfIt?

PerfIt is a light-weight and generic instrumentation lib for publishing standard instrumentation traces (Performance Counters and ETW events on Windows, LTTng traces on Linux via .NET Core's EventSource class, Zipkin traces) or your custom ones. Historically, PerfIt would only support Performance Counters and ETW, but it publish those if they are supported on the platform it runs.

What are PerfIt packages?

  • PerfIt: common constructs for instrumentation
  • PerfIt.Http: for instrumenting HttpClient
  • PerfIt.WebApi: for instrumenting ASP.NET Web API
  • PerfIt.Mvc: for instrumenting ASP.NET MVC
  • PerfIt.CoreMvc: for instrumenting ASP.NET Core MVC
  • PerfIt.Tracers.EventHub: for publishing PerfIt traces to EventHub
  • PerfIt.Zipkin: common constructs for adding Zipkin traces
  • PerfIt.Zipkin.EventHub: for publishing Zipkin traces to EventHub

Why do I need PerfIt?

If you are using .NET for a serious project, you would most likely want to instrument your code and network-bound calls. PerfIt helps to make instrumentation easier by looking after boilerplate stuff so that you focus on your business logic.

What is the overhead of using PerfIt?

It is negligible compared to the code you would normally find within an API. It should be <1ms.

What about instrumenting serving or consuming HTTP?

PerfIt has constructs for ASP.NET MVC, ASP.NET Web API, ASP.NET Core MVC and HttpClient.

What counters does it publish?

If your platform supports performance counters (Windows) and you have not disabled it, it will publish counters. There are 5 standard counters that come with PerfIt out of the box (TotalNoOfOperations, AverageTimeTaken, LastOperationExecutionTime, NumberOfOperationsPerSecond and CurrentConcurrentOperationsCount) and you can choose one or all (typically you would choose all).

You can also create your own counters by implementing a simple base class.

What is category and instance name? Is this not a left-over from Performance Counters?

Yes and no. Yes, it was added initially because of Performance Counters but still is a common sense two-level hiararchy for naming traces. So they are here to stay.

What if I just want ETW events and no Performance Counters since installing them is a hassle?

You can turn only ETW or Performance Counters or both (or None).

I am using a library which publishes counters and I have my own counters in my project and I want to turn off library counters but keep mine?

As of version 2.1, you can control counters or raising ETW by their category name in your app.config (or web.config) appSettings. The syntax for category-based configuration is to have keys as "perfit:<feature>:<categoryName>" where feature is one of "publishCounters", "publishErrors" and "publishEvent" and value is boolean. The previous global syntax of "perfit:<feature>" which turns features blanket on or off. [Bear in mind, appSettings overrides values set in code.]

Example:

<add key="perfit:publishCounters" value="true"/>
<add key="perfit:publishErrors:a" value="false"/>
<add key="perfit:samplingRate:dd" value="0.02"/>

In this case, publishCounters is globally on and publishErrors for category a is off. Also sampling rate is set to 2% for category dd.

If you need define these for an Azure application, Azure configuration schema does not allow you to use colon (:) in the name of the configuration. In this case, use pound sign (#) as the delimiter:

<add key="perfit#publishCounters" value="true"/>
<add key="perfit#publishErrors#a" value="false"/>
<add key="perfit#samplingRate#dd" value="0.02"/>

Getting Started (Measuring any part of your code)

Best way to get started is to look at the samples solution and have a look at its README.

But here are two typical scenarios, one for .NET Core (Windows/Mac/Linux) with closure-base instrumentation and other for .NET 4.52 (Windows) with aspect-oriented publishing Performance Counters

Getting Started - .NET Core (Windows/Mac/Linux)

Step 1: Create an Console project

Use Visual Studio or dotnet new to create an empty console application

Step 2: Add PerfIt to your project

Add reference to PerfIt

PM> Install-Package PerfIt

Step 3: Add this code

var si = new SimpleInstrumentor(new InstrumentationInfo()
{
    CategoryName = "test-category",
    InstanceName = "test-instance"
});

si.Tracers.Add("TextFile", new SeparatedFileTracer("trace.txt"));
si.InstrumentAsync(() => Task.Delay(100)).GetAwaiter().GetResult();
Thread.Sleep(100);
si.Dispose();
Console.WriteLine(File.ReadAllText("trace.txt"));

As you can see, traces get stored on the file. This is not an option for very high performance cases but it is OK for cases where sampling is low or the load is not too big and you can use File tailers such as Elatico's Beats/LogStash to tail this trace file.

Getting Started - ASP.NET Web API (Windows)

Step 1: Create an ASP.NET Web API

Use Visual Studio to create an ASP.NET Web API and add a controller (such as DefaultController).

Step 2: Add PerfIt to your project

In the package manager console, type:

PM> Install-Package PerfIt.WebApi

Step 3: Decorate your actions

In the actions you would want to monitor, add this attribute (we are using default counters so no need to specify them):

[PerfItFilter("MyApiCategory", Description = "My cool API"]
public string Get()
{
    ...
}

Step 4: Add an installer class

Right-click on your ASP.NET Web API and choose Add an Item and then from the menu choose "Installer Class".

Once added, click F7 to see the code and then add these lines:

public override void Install(IDictionary stateSaver)
{
 base.Install(stateSaver);
 CounterInstaller.Install(Assembly.GetExecutingAssembly(), new FilterDiscoverer());
}

public override void Uninstall(IDictionary savedState)
{
	base.Uninstall(savedState);
	CounterInstaller.Uninstall(Assembly.GetExecutingAssembly());
}

Step 5: Register your counters using InstallUtil.exe

In the command line, change directory to the .NET framework in use and then use -i switch to install ASP.NET Web API dll:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil -i "c:\myproject\path\bin\MyAspNetWebApi.dll"

Step 6: Use your API and see the counters being published

Your counters will be published under a category, with the same name as your project. Underneath, you will see instance(s) of your individual counters.

counters

Common scenarios

Changing default category name

You can do this by supplying your custom category name to all three methods below (make sure they are all the same):

CounterInstaller.Install("MyCategoryName");

// AND

CounterInstaller.Uninstall("MyCategoryName");

// AND

var handler = new PerfItDelegatingHandler("MyCategoryName");

Changing default instance name

By default, instance name is composed of controller name and action name. If you have two actions with the same name (overloading a method), either you need to use ActionName attribute or change the method name (e.g. instead of Get using GetCustomer).

An alternative method is to supply instance name (also useful when you want to supply your custom name):

[PerfItFilter(Description = "Gets all items",
    Counters = ...,
    InstanceName="AnyName")]
public string Get()
{
    ...
}

Turn off publishing counters

For whatever reason you might decide to turn off publishing counters. All you have to do is to add this entry to the AppSetting of your web.config:

  <appSettings>
    ...
    <add key="perfit:publishCounters" value="false"/>
    ...
  </appSettings>

As of version 2.1, there is option to turn on and off by CategoryName - see above.

Not to throw publishing errors

By default, publishing performance counters are regarded as having the same importance as the application's business logic and all publishing errors are thrown. If you would like to change this behaviour, you can do so in config:

  <appSettings>
    ...
    <add key="perfit:publishErrors" value="false"/>
    ...
  </appSettings>

If you need to turn on or off publishErrors, publishCounters or publishEvents by category, please see examples above.

FileNotFoundException when registering the DLL using InstallUtil.exe

A common problem is to encounter FileNotFoundException when registering your counters using InstallUtil. This is more common when your use Web API 2. In any case, this is a problem with InstallUtil not honouring your assembly redirects. To solve the problem (and it is just a workaround), simply copy the assembly redirect directives to InstallUtil.exe.config, run the installation and then remove them.

This has been raised a few times (see the issues, for example #11) but the problem is simply the way InstallUtil works - or rather doesn't.

What I found the best solution is to include a copy of InstallUtil.exe and its custom config (which works for your project by copying content of assemblyBinding section of the web.config) along with your deployables and have a script to install the counter, rather than relying on the standard InstallUtil on the box. These files are small and certainly a good solution.

More Repositories

1

CacheCow

An implementation of HTTP Caching in .NET Core and 4.5.2+ for both the client and the server
C#
846
star
2

SuperBenchmarker

Apache Benchmark (ab.exe) on steroids
C#
571
star
3

BeeHive

A Reactive Cloud Actor library/mini-framework for .NET 4.5.2+ or .NET Standard 2.0+
C#
131
star
4

RandomGen

A simple random generation framework for .NET Core and 4.52+
C#
49
star
5

ConveyorBelt

Shovels Azure diagnostics data to ElasticSearch
C#
35
star
6

deep-learning-lang-detection

Deep Learning using Keras to detect programming language of a file or snippet
F#
26
star
7

avalon

An Implementation of Raft in .NET for prime-time - along with an eCommerce availability State Machine
C#
24
star
8

WikiRockWord2Vec

A word2vec snippet
CSS
22
star
9

FiveLevelsOfMediaType

C#
11
star
10

aws-distributed-mutex

A distributed lock (mutex) implementation for AWS using DynamoDB
C#
10
star
11

psyfon

A High-Performance Azure EventHubs Sink capable of buffering/batching
C#
9
star
12

NoSqlBenchmarking

Benchmarking on various NoSQL technologies available on Microsoft platform
C#
7
star
13

CyberInsekt

A web crawling library based on ASP.NET Web API
C#
7
star
14

hexagon-rl

A Reinforcement Learning implementation for Danske Bank's Hexagon game
Python
6
star
15

ApiActionSelector

C#
6
star
16

grpc-dotnet-minimal-sample

Minimal gRPC sample for dotnetcore
C#
5
star
17

CarManager

A sample used for various Web API examples
C#
4
star
18

wiki-album-genre

Deep Learning Model for wiki articles - predicting genre of an album based on name
Python
4
star
19

CacheCore

HTTP Caching for .NET - Soon to be deleted (PLEASE USE CACHECOW)
4
star
20

MemoryLeakOnUndisposedHttpResponseMessage

A Sample showing Memory Leak and Socket Leak if HttpResponseMessage is not consumed or disposed
3
star
21

WebPerfTestFx

A playground for proving a few performance points
C#
3
star
22

EntityRange

A sample on implementing custom Range on ASP.NET Web API
C#
3
star
23

hokm

Implementation of Hokm card game (Court Piece) for C#
C#
2
star
24

SeparatedFileProcessor

C#
2
star
25

HierarchicalRouting

A fluent hierarchical routing for ASP.NET Web API
2
star
26

EncryptionBenchmark

Comparing crypto algorithm performances in .NET Framework
C#
2
star
27

MemoryLeakSamples

A collection of snippets for memory leak simulation and diagnosis
C#
1
star
28

CondtionionalValidation

Conditional Validation for ASP.NET MVC 3
1
star
29

ApiLayerExample

An implemented example of API Layer
C#
1
star
30

wiki-rock

A Rock Music history DataViz
JavaScript
1
star
31

TraceView

A light-weight and cross-platform trace capturing and viewing tool for developers. Inspired by Sysinternal's DebugView
CSS
1
star
32

zipkin-collector-eventhub

Azure EventHub Collector for Zipkin
Java
1
star
33

CloudBigTableBenchmarks

Benchmark of a few cloud BigTable impl
C#
1
star
34

blog-posts

Markdown of public blog posts
1
star
35

GuidVersusIntBenchmark

Some code for benchmarking GUID vs. INT primary keys
C#
1
star
36

PocoHttp-local

A .NET Client library for working with HTTP data stores
C#
1
star
37

NDecisionTree

Logical Decision Trees in .NET for simple to moderately complex business rules
1
star