4.0十九章 检测----性能计数器
2016-01-28 14:35
169 查看
计数器是用以收集性能数据的机制。注册表存储所有计数器的名称,每个计数器均与系统功能的一个特定区域相关。示例包括处理器繁忙时间、内存占用情况或通过某个网络连接接收的字节数。
通过计数器的名称和位置可唯一地标识每个计数器。与文件路径包括驱动器、目录、一个或多个子目录和文件名一样,计数器信息由四个元素组成:计算机、对象、对象实例和计数器名称。
///事例演示了
多长时间才能进一次:j % 10 == 9
public static void CollectSamples()
{
const String categoryName = "ElapsedTimeSampleCategory";
const String counterName = "ElapsedTimeSample";
// If the category does not exist, create the category and exit.
// Performance counters should not be created and immediately used.
// There is a latency time to enable the counters, they should be created
// prior to executing the application that uses the counters.
// Execute this sample a second time to use the category.
if (!PerformanceCounterCategory.Exists(categoryName))
{
CounterCreationDataCollection CCDC = new CounterCreationDataCollection();
// Add the counter.
CounterCreationData ETimeData = new CounterCreationData();
ETimeData.CounterType = PerformanceCounterType.ElapsedTime;
ETimeData.CounterName = counterName;
CCDC.Add(ETimeData);
// Create the category.
PerformanceCounterCategory.Create(categoryName,
"Demonstrates ElapsedTime performance counter usage.",
PerformanceCounterCategoryType.SingleInstance, CCDC);
// Return, rerun the application to make use of the new counters.
return;
}
else
{
Console.WriteLine("Category exists - {0}", categoryName);
}
// Create the performance counter.
PerformanceCounter PC = new PerformanceCounter(categoryName,
counterName,
false);
// Initialize the counter.
PC.RawValue = Stopwatch.GetTimestamp();
DateTime Start = DateTime.Now;
// Loop for the samples.
for (int j = 0; j < 100; j++)
{
// Output the values.
if ((j % 10) == 9)
{
Console.WriteLine("NextValue() = " + PC.NextValue().ToString());
Console.WriteLine("Actual elapsed time = " + DateTime.Now.Subtract(Start).ToString());
OutputSample(PC.NextSample());//Obtains a counter sample, and returns the raw, or uncalculated, value for it.
Start = DateTime.Now;//reset the time
}
// Reset the counter on every 20th iteration.
//if (j % 20 == 0)
//{
// PC.RawValue = Stopwatch.GetTimestamp();
// Start = DateTime.Now;
//}
System.Threading.Thread.Sleep(50);
}
Console.WriteLine("Elapsed time = " + DateTime.Now.Subtract(Start).ToString());
}
private static void OutputSample(CounterSample s)
{
Console.WriteLine("\r\n+++++++++++");
Console.WriteLine("Sample values - \r\n");
Console.WriteLine(" BaseValue = " + s.BaseValue);
Console.WriteLine(" CounterFrequency = " + s.CounterFrequency);
Console.WriteLine(" CounterTimeStamp = " + s.CounterTimeStamp);
Console.WriteLine(" CounterType = " + s.CounterType);
Console.WriteLine(" RawValue = " + s.RawValue);
Console.WriteLine(" SystemFrequency = " + s.SystemFrequency);
Console.WriteLine(" TimeStamp = " + s.TimeStamp);
Console.WriteLine(" TimeStamp100nSec = " + s.TimeStamp100nSec);
Console.WriteLine("++++++++++++++++++++++");
}
通过计数器的名称和位置可唯一地标识每个计数器。与文件路径包括驱动器、目录、一个或多个子目录和文件名一样,计数器信息由四个元素组成:计算机、对象、对象实例和计数器名称。
///事例演示了
多长时间才能进一次:j % 10 == 9
public static void CollectSamples()
{
const String categoryName = "ElapsedTimeSampleCategory";
const String counterName = "ElapsedTimeSample";
// If the category does not exist, create the category and exit.
// Performance counters should not be created and immediately used.
// There is a latency time to enable the counters, they should be created
// prior to executing the application that uses the counters.
// Execute this sample a second time to use the category.
if (!PerformanceCounterCategory.Exists(categoryName))
{
CounterCreationDataCollection CCDC = new CounterCreationDataCollection();
// Add the counter.
CounterCreationData ETimeData = new CounterCreationData();
ETimeData.CounterType = PerformanceCounterType.ElapsedTime;
ETimeData.CounterName = counterName;
CCDC.Add(ETimeData);
// Create the category.
PerformanceCounterCategory.Create(categoryName,
"Demonstrates ElapsedTime performance counter usage.",
PerformanceCounterCategoryType.SingleInstance, CCDC);
// Return, rerun the application to make use of the new counters.
return;
}
else
{
Console.WriteLine("Category exists - {0}", categoryName);
}
// Create the performance counter.
PerformanceCounter PC = new PerformanceCounter(categoryName,
counterName,
false);
// Initialize the counter.
PC.RawValue = Stopwatch.GetTimestamp();
DateTime Start = DateTime.Now;
// Loop for the samples.
for (int j = 0; j < 100; j++)
{
// Output the values.
if ((j % 10) == 9)
{
Console.WriteLine("NextValue() = " + PC.NextValue().ToString());
Console.WriteLine("Actual elapsed time = " + DateTime.Now.Subtract(Start).ToString());
OutputSample(PC.NextSample());//Obtains a counter sample, and returns the raw, or uncalculated, value for it.
Start = DateTime.Now;//reset the time
}
// Reset the counter on every 20th iteration.
//if (j % 20 == 0)
//{
// PC.RawValue = Stopwatch.GetTimestamp();
// Start = DateTime.Now;
//}
System.Threading.Thread.Sleep(50);
}
Console.WriteLine("Elapsed time = " + DateTime.Now.Subtract(Start).ToString());
}
private static void OutputSample(CounterSample s)
{
Console.WriteLine("\r\n+++++++++++");
Console.WriteLine("Sample values - \r\n");
Console.WriteLine(" BaseValue = " + s.BaseValue);
Console.WriteLine(" CounterFrequency = " + s.CounterFrequency);
Console.WriteLine(" CounterTimeStamp = " + s.CounterTimeStamp);
Console.WriteLine(" CounterType = " + s.CounterType);
Console.WriteLine(" RawValue = " + s.RawValue);
Console.WriteLine(" SystemFrequency = " + s.SystemFrequency);
Console.WriteLine(" TimeStamp = " + s.TimeStamp);
Console.WriteLine(" TimeStamp100nSec = " + s.TimeStamp100nSec);
Console.WriteLine("++++++++++++++++++++++");
}
相关文章推荐
- 物联网
- 俄罗斯方块Tetris (javascript+HTML+CSS)
- POJ 2718 -- 穷竭搜索
- gridcontrol中LayoutView层叠图片效果
- SoftKeyboard按键View找焦点
- Centos6 安全防护设置指南
- MySQL死锁原因分析
- Struts2入门实例
- LAMP中如何设置open_basedir
- 简单的 数据加载显示“数据正在加载,请稍后”
- 版本控制系统SVN
- 10条命令,一分钟分析Linux性能问题
- POJ 1979 -- DFS
- android 更新下载apk
- 数据库备份,及清理备份计划
- bat批处理笔记——为指定类型文件添加前缀名
- 高性能服务器架构
- LINUX - awk命令之NF和$NF区别
- Error:.exe has triggered a breakpoint.
- Android中asset文件夹和raw文件夹区别与用法