-
Notifications
You must be signed in to change notification settings - Fork 3
Usage
ikopylov edited this page Jan 14, 2015
·
13 revisions
- Write class for every category you need. You class should be inherited from one of 3 library category class:
internal class TestEmptyCategory: EmptyCategoryWrapper
{
public TestEmptyCategory() : base("Empty", "desc") { }
}
internal class TestSingleInstanceCategory: SingleInstanceCategoryWrapper
{
public TestSingleInstanceCategory() : base("Single", "desc") { }
}
internal class TestInstance: InstanceInMultiInstanceCategoryWrapper
{
}
internal class TestMultiInstanceCategory : MultiInstanceCategoryWrapper<TestInstance>
{
public TestMultiInstanceCategory () : base("Multi", "desc") { }
}
- Define counters as properties inside your categories and mark them with 'Counter' attribute:
internal class TestSingleInstanceCategory: SingleInstanceCategoryWrapper
{
public TestSingleInstanceCategory() : base("Single", "desc") { }
[Counter("TestElapsedTimeCounter")]
public ElapsedTimeCounter Elapsed { get; private set; }
[Counter("NumberOfItemsCounter")]
public NumberOfItemsCounter Count { get; private set; }
[Counter("TestOperationsPerSecondCounter")]
public OperationsPerSecondCounter OperationPerSec { get; private set; }
[Counter("TestAverageCountCounter")]
public AverageCountCounter Avg { get; private set; }
[Counter("TestAverageTimeCounter")]
public AverageTimeCounter AvgTime { get; private set; }
[Counter("TestMomentTimeCounter")]
public MomentTimeCounter MomentTime { get; private set; }
}
internal class TestInstance: InstanceInMultiInstanceCategoryWrapper
{
[Counter("Count")]
public NumberOfItemsCounter Count { get; private set; }
}
- If you need, you can write the singleton counters container:
internal class PerfCounters: Qoollo.PerformanceCounters.PerfCountersContainer
{
private static TestSingleInstance _singleInstance = CreateNullCategoryWrapper<TestSingleInstance>();
private static TestMultiInstance _multiInstance = CreateNullCategoryWrapper<TestMultiInstance>();
public static TestSingleInstance TestSingle { get { return _singleInstance; } }
public static TestMultiInstance TestMulti { get { return _multiInstance; } }
[PerfCountersInitializationMethod]
public static void Init(CategoryWrapper parent)
{
var intermediate = parent.CreateEmptySubCategory("PerfCounterTest", "description");
_singleInstance = intermediate.CreateSubCategory<TestSingleInstance>();
_multiInstance = intermediate.CreateSubCategory<TestMultiInstance>();
}
}
- Create on application start-up the CounterFactory or load it from App.config:
var counterFactory = PerfCountersInstantiationFactory.CreateCounterFactoryFromAppConfig("PerfCountersConfigurationSection");
- Initialize you categories:
PerfCounters.Init(counterFactory.CreateRootWrapper());
- Call InitAll on your factory:
counterFactory.InitAll();
- Use counters:
PerfCounters.TestSingle.Count.Increment();
PerfCounters.TestSingle.OperationPerSec.OperationFinished();
PerfCounters.TestSingle.Avg.RegisterValue(random.Next(0, 100));
var timer = PerfCounters.TestSingle.AvgTime.StartNew();
timer.Complete();
using (PerfCounters.TestSingle.AvgTime.StartNew())
{
}
PerfCounters.TestMulti["1"].Count.Increment();
PerfCounters.TestMulti["1"].Remove();
- Dispose counters factory when application is about to close:
counterFactory.Dispose();
Full example you can find here: TestProject.Program.cs
Every assembly can use it's own performance counters and define own PerfCountersContainer singleton. To simplify the counter initialization you can use 'Initializer' class:
Qoollo.PerformanceCounters.Initialization.Initializer.InitializeCategoryWrapper(rootCategory, typeof(<any public type in dependent assembly>).Assembly);
It scans the supplied assembly for classes inherited from 'PerfCountersContainer' (or tagged with attribute 'PerfCountersContainerAttribute') and call their initialization method (method tagged with 'PerfCountersInitializationMethodAttribute').