-
Notifications
You must be signed in to change notification settings - Fork 3
Usage
ikopylov edited this page Dec 25, 2014
·
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();
PerfCounters.TestMulti[random.Next(0, 10).ToString()].Count.Increment();
- Dispose counters factory when application is about to close:
counterFactory.Dispose();
Full sample you can find here: TestProject.Program.cs