using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DTech.Processor
{
/// <summary>
/// Helper class to demonstrate the setup of performance counters.
/// </summary>
public static class PerformanceCounterHelper
{
public static readonly string CATEGORY = "IFP";
public static readonly string COUNTER_FILES_RECEIVED = "Total Files Processed";
public static readonly string COUNTER_Transactions_RECEIVED = "Total Transactions Processed";
public static readonly string COUNTER_DUP_Transactions_RECEIVED = "Total Duplicates Transactions Processed";
public static readonly string COUNTER_AVG_FILE_DURATION = "Avg time to process file";
public static readonly string COUNTER_AVG_FILE_DURATION_BASE = "Avg time to process file Base";
public static readonly string COUNTER_IFP_ERRORS = "Total IFP Errors";
// Total number of tests executed
private static PerformanceCounter _TotalFiles = null;
// Total Transactions
private static PerformanceCounter _TotalTransactions = null;
// Total dup Transactions
private static PerformanceCounter _TotalDupTransactions = null;
// avg files
private static PerformanceCounter _AvgFiles = null;
// avg files
private static PerformanceCounter _AvgFilesBase = null;
// avg files
private static PerformanceCounter _TotalErrors = null;
/// <summary>
/// Constructor
/// </summary>
static PerformanceCounterHelper()
{
try
{
// Set up the performance counter(s)
if (!PerformanceCounterCategory.Exists(CATEGORY))
{
// Create the collection container
CounterCreationDataCollection counters = new
CounterCreationDataCollection();
addCounter(counters, COUNTER_FILES_RECEIVED, COUNTER_FILES_RECEIVED, PerformanceCounterType.NumberOfItems64);
addCounter(counters, COUNTER_Transactions_RECEIVED, COUNTER_Transactions_RECEIVED, PerformanceCounterType.NumberOfItems64);
addCounter(counters, COUNTER_DUP_Transactions_RECEIVED, COUNTER_DUP_Transactions_RECEIVED, PerformanceCounterType.NumberOfItems64);
addCounter(counters, COUNTER_IFP_ERRORS, COUNTER_IFP_ERRORS, PerformanceCounterType.NumberOfItems64);
addCounter(counters, COUNTER_AVG_FILE_DURATION, COUNTER_AVG_FILE_DURATION, PerformanceCounterType.AverageTimer32);
addCounter(counters, COUNTER_AVG_FILE_DURATION_BASE, COUNTER_AVG_FILE_DURATION_BASE, PerformanceCounterType.AverageBase);
//// Create counter #1 and add it to the collection
//CounterCreationData tests = new CounterCreationData();
//tests.CounterName = "Total Tests Executed";
//tests.CounterHelp = "Total number of tests executed.";
//tests.CounterType = PerformanceCounterType.NumberOfItems32;
//counters.Add(tests);
//// Create counter #2 and add it to the collection
//CounterCreationData testsPerSec = new CounterCreationData();
//testsPerSec.CounterName = "Tests Executed / sec";
//testsPerSec.CounterHelp = "Number of tests executed per second.";
//testsPerSec.CounterType =
// PerformanceCounterType.RateOfCountsPerSecond32;
//counters.Add(testsPerSec);
//// Create counter #3 and add it to the collection
//CounterCreationData avgTest = new CounterCreationData();
//avgTest.CounterName = "Average Test Duration";
//avgTest.CounterHelp = "Average time to execute a test.";
//avgTest.CounterType = PerformanceCounterType.AverageTimer32;
//counters.Add(avgTest);
//// Create counter #4 and add it to the collection
//CounterCreationData avgTestBase = new CounterCreationData();
//avgTestBase.CounterName = "Average Test Duration Base";
//avgTestBase.CounterHelp = "Average time to execute a test base.";
//avgTestBase.CounterType = PerformanceCounterType.AverageBase;
//counters.Add(avgTestBase);
// Create the category and all of the counters.
PerformanceCounterCategory.Create(CATEGORY,
"IFP Counters.",
counters);
}
_TotalFiles = buildPerfCounter(COUNTER_FILES_RECEIVED);
_TotalFiles.RawValue = 0;
_TotalTransactions = buildPerfCounter(COUNTER_Transactions_RECEIVED);
_TotalTransactions.RawValue = 0;
_TotalDupTransactions = buildPerfCounter(COUNTER_DUP_Transactions_RECEIVED);
_TotalDupTransactions.RawValue = 0;
_TotalErrors = buildPerfCounter(COUNTER_IFP_ERRORS);
_TotalErrors.RawValue = 0;
_AvgFiles = buildPerfCounter(COUNTER_AVG_FILE_DURATION);
_AvgFiles.RawValue = 0;
_AvgFilesBase = buildPerfCounter(COUNTER_AVG_FILE_DURATION_BASE);
_AvgFilesBase.RawValue = 0;
}
catch (Exception exc)
{
Debug.Write("PerformanceCounterHelper: ERROR-> " + exc.Message);
}
}
/// <summary>
/// Increment the CodeGuru sample counters.
/// </summary>
/// <param name="Ticks">Timing interval</param>
public static void IncrementFiles()
{
try
{
checkReset();
if (_TotalFiles.RawValue >= Int64.MaxValue || rF)
{
_TotalFiles.RawValue = 0;
rF = false;
}
else
_TotalFiles.Increment();
}
catch { }
//_TestsPerSecond.Increment();
//_TotalDupTransactions.IncrementBy(Ticks);
//_TotalDupTransactionsBase.Increment();
}
public static void IncrementTransactions()
{
try
{
checkReset();
if (_TotalTransactions.RawValue >= Int64.MaxValue || rS)
{
_TotalTransactions.RawValue = 0;
rS = false;
}
else
_TotalTransactions.Increment();
}
catch { }
}
public static void IncrementDupTransactions()
{
try
{
checkReset();
if (_TotalDupTransactions.RawValue >= Int64.MaxValue || rDS)
{
_TotalDupTransactions.RawValue = 0;
rDS = false;
}
else
_TotalDupTransactions.Increment();
}
catch { }
}
public static void IncrementErrors()
{
try
{
checkReset();
if (_TotalErrors.RawValue >= Int64.MaxValue || rE)
{
_TotalErrors.RawValue = 0;
rDS = false;
}
else
_TotalErrors.Increment();
}
catch { }
}
public static void IncrementAvgFiles(long ticks)
{
try
{
_AvgFiles.IncrementBy(ticks);
if (_AvgFilesBase.RawValue > Int64.MaxValue)
_AvgFilesBase.RawValue = 0;
else
_AvgFilesBase.Increment();
}
catch { }
}
public static void IncrementAvgFiles(long ticks, int howmany)
{
try
{
_AvgFiles.IncrementBy(ticks);
_AvgFilesBase.IncrementBy(howmany);
}
catch { }
}
private static void addCounter(CounterCreationDataCollection counters, string counterName, string help, PerformanceCounterType type)
{
// Create counter #1 and add it to the collection
CounterCreationData tests = new CounterCreationData();
tests.CounterName = counterName;
tests.CounterHelp = help;
tests.CounterType = type;
counters.Add(tests);
}
private static PerformanceCounter buildPerfCounter(string name)
{
PerformanceCounter r = new PerformanceCounter();
r.CategoryName = CATEGORY;
r.CounterName = name;
r.MachineName = ".";
r.ReadOnly = false;
return r;
}
private static DateTime lastResetDate = DateTime.Now;
private static bool rF = false;
private static bool rS = false;
private static bool rDS = false;
private static bool rE = false;
private static object _lock = new object();
private static void checkReset()
{
lock (_lock)
{
if (DateTime.Now.Subtract(lastResetDate).TotalHours > 24)
{
Debug.WriteLine("RESET Perf Counters");
rF = true;
rS = true;
rS = true;
rE = true;
lastResetDate = DateTime.Now;
}
}
}
}
}
No comments:
Post a Comment