Windows性能计数器
简介(百科):
Window性能计数器(Performance Counter),也叫性能监视器,实际上是Windows NT/2000提供的一种系统功能,它能实时采集、分析系统内的应用程序、服务、驱动程序等的性能数据,以此来分析系统的瓶颈、监视组件的表现,最终帮助用户进行系统的合理调配。这里,还要引入一个性能对象(Performance Object)的概念,即被监视者。一般系统中的性能对象包括处理器(Processor)、进程(Process)、线程(Thread)、网络通讯(如TCP、UDP、ICMP、IP等)、系统服务(如ACS/RSVP Service)等。PerfMon.exe中可以查看性能对象、性能计数器和对象实例,可通过添加计数器来查看相关描述信息。
Windows中查看性能计数器信息:
1:Windows+R键打开运行,输入perfmon,回车
2:在这里就可以查看性能性能监视器,可以增加一些需要监视的指标信息
3:增加监视信息--选择需要监视的信息,确认即可。
性能计数器在C#代码中的简单应用:
利用PerformanceCounter检测CPU性能,做简单的展示
PerformanceCounter类:表示 Windows NT 性能计数器组件。NextValue() 即获取计数器样本并为其返回计算所得值
知识点:
Chart 图表,VS自带的Chart图表,大大简化了对图表的开发。关于Chart,此前已有例子说明。
Queue 队列表示对象的先进先出集合。关于Queue此前已有例子说明。
TreeView 显示标记项的分层集合,每个标记项用一个 System.Windows.Forms.TreeNode 来表示。即VS自带的树状菜单
Timer 实现按用户定义的时间间隔引发事件的计时器。此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用。定时刷新计数器中的值。
最终效果:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace Counter
{
public partial class Form1 : Form
{
private PerformanceCounter mCounter;//当前计数器
private Queue<double> dataQueue = new Queue<double>(100);//初始化队列
public Form1()
{
InitializeComponent();
InitCounterCategory();
InitChart();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 初始化计数器信息
/// </summary>
private void InitCounterCategory()
{
//获取所有的计数器类别
var counterCategories = PerformanceCounterCategory.GetCategories().OrderBy(p => p.CategoryName);
int i = 0;
foreach (var counterCategory in counterCategories)
{
//属于线程级别的不显示
if (counterCategory.CategoryName == "Thread")
{
continue;
}
//将信息绑定的TreeView上
this.tvCategory.CheckBoxes = true;
this.tvCategory.Nodes.Add(counterCategory.CategoryName);
string[] instanceNames = counterCategory.GetInstanceNames();
int j = 0;
foreach (var instanceName in instanceNames)
{
this.tvCategory.Nodes[i].Nodes.Add(instanceName);
var counters = counterCategory.GetCounters(instanceName).Select(p => string.Format("{0}", p.CounterName));
int k = 0;
foreach (var counter in counters)
{
this.tvCategory.Nodes[i].Nodes[j].Nodes.Add(counter);
k++;
}
j++;
}
i++;
}
//初始化Counter
PerformanceCounter pc = new PerformanceCounter();
pc.CategoryName = "Processor";
pc.CounterName = "% Processor Time";
pc.InstanceName = "_Total";
pc.MachineName = ".";
pc.ReadOnly = true;
mCounter = pc;
mCounter.NextValue();//初始值
}
//<summary>
//初始化图表
//</summary>
private void InitChart()
{
//定义图表区域
this.chart1.ChartAreas.Clear();
ChartArea chartArea1 = new ChartArea("C1");
this.chart1.ChartAreas.Add(chartArea1);
//定义存储和显示点的容器
this.chart1.Series.Clear();
Series series1 = new Series("S1");
series1.ChartArea = "C1";
this.chart1.Series.Add(series1);
//设置图表显示样式
this.chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.SharpTriangle;
this.chart1.ChartAreas[0].AxisY.Title = "Kkbps";//坐标轴的标题
this.chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
this.chart1.ChartAreas[0].AxisY.Minimum = 0;
this.chart1.ChartAreas[0].AxisY.Maximum = 50;
this.chart1.ChartAreas[0].AxisY.Interval = 5;
this.chart1.ChartAreas[0].AxisX.Interval = 5;
this.chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.SharpTriangle;
this.chart1.ChartAreas[0].AxisX.Title = "Sec";
this.chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
//设置标题
this.chart1.Titles.Clear();
this.chart1.Titles.Add("S01");
this.chart1.Titles[0].Text = "XXX网络监控显示";
this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
//设置图表显示样式
this.chart1.Series[0].Color = Color.LightGreen;
this.chart1.Series[0].ChartType = SeriesChartType.Area;//图表形状
this.chart1.Series[0].Points.Clear();
}
/// <summary>
/// 启动定时器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStart_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
/// <summary>
/// 停止定时器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{
this.timer1.Stop();
}
/// <summary>
/// 定时执行函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
UpdateQueueValue();
this.chart1.Series[0].Points.Clear();
if (dataQueue.Max() > this.chart1.ChartAreas[0].AxisY.Maximum)
{
this.chart1.ChartAreas[0].AxisY.Maximum = Math.Ceiling(dataQueue.Max() / 10) * 10;
this.chart1.ChartAreas[0].AxisY.Interval = this.chart1.ChartAreas[0].AxisY.Maximum / 10;
}
for (int i = 0; i < dataQueue.Count; i++)
{
this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i));
}
}
//更新队列中的值
private void UpdateQueueValue()
{
if (dataQueue.Count > 100)
{
dataQueue.Dequeue();
}
//获取的值就Byte/s 所以要除以1024
dataQueue.Enqueue(mCounter.NextValue());
}
/// <summary>
/// 当选中复选框时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvCategory_AfterCheck(object sender, TreeViewEventArgs e)
{
}
/// <summary>
/// 采用递归方法修改节点的选中状态
/// </summary>
/// <param name="nodes"></param>
/// <param name="flag"></param>
private void CheckedStated(TreeNodeCollection nodes, bool flag)
{
}
}
}