写了个在线的性能计数器,希望对大家有用
写了个在线的性能计数器,希望对大家有用
这两天在研究测试,顺手写了个在线的性能计数器。使用ZedGraph控件做为展示,并配合Asp.net Ajax的UpdatePanel和Timer控件实现定时无刷新。
System.Diagnostics命名空间里提供了与NT性能计数器相关的类,使用性能计数器提取数据,然后再使用ZedGraph控件来展示数据,最终的效果如下:
添加计数器界面:
1、获取计数器类别:
PerformanceCounterCategory[] catalogs = PerformanceCounterCategory.GetCategories();
2、获取某个分类下的计数器,并且排序:
PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName);
string[] instances = category.GetInstanceNames();
PerformanceCounter[] counters = null;
//判断是否有实例
if (instances != null &&
instances.Length > 0)
{
SortedList list = new SortedList();
foreach (string ins in instances)
{
list.Add(ins, ins);
}
this.lbInstance.DataSource = list;
this.lbInstance.DataBind();
this.lbInstance.SelectedIndex = 0;
counters = category.GetCounters(instances[0]);
}
else
{
counters = category.GetCounters();
}
SortedList listCounter = new SortedList();
foreach (PerformanceCounter c in counters)
{
listCounter.Add(c.CounterName, c.CounterName);
}
string[] instances = category.GetInstanceNames();
PerformanceCounter[] counters = null;
//判断是否有实例
if (instances != null &&
instances.Length > 0)
{
SortedList list = new SortedList();
foreach (string ins in instances)
{
list.Add(ins, ins);
}
this.lbInstance.DataSource = list;
this.lbInstance.DataBind();
this.lbInstance.SelectedIndex = 0;
counters = category.GetCounters(instances[0]);
}
else
{
counters = category.GetCounters();
}
SortedList listCounter = new SortedList();
foreach (PerformanceCounter c in counters)
{
listCounter.Add(c.CounterName, c.CounterName);
}
3、添加计数器到统计
使用一个类来保存计数器信息。
[Serializable]
private class CounterInfo
{
public string CategoryName;
public string CounterName;
public string InstanceNames;
public CounterInfo(string categoryName, string counterName, string instanceNames)
{
CategoryName = categoryName;
CounterName = counterName;
InstanceNames = instanceNames;
}
}
private class CounterInfo
{
public string CategoryName;
public string CounterName;
public string InstanceNames;
public CounterInfo(string categoryName, string counterName, string instanceNames)
{
CategoryName = categoryName;
CounterName = counterName;
InstanceNames = instanceNames;
}
}
将计数器信息添加到SortedList对象中:
SortedList<string, CounterInfo> list = this.Counters;
string strInstance = "";
if (this.lbInstance.Items.Count > 0)
{
bool bFlag = true;
foreach (ListItem item in this.lbInstance.Items)
{
if (item.Selected)
{
if (bFlag)
{
strInstance = item.Value;
bFlag = false;
}
else
{
strInstance += "," + item.Value;
}
}
}
}
foreach(ListItem item in this.lbCounter.Items)
{
if (item.Selected)
{
list[this.ddlCategory.SelectedValue + " " + item.Value + " " + strInstance] = new CounterInfo(this.ddlCategory.SelectedValue,item.Value,strInstance);
}
}
string strInstance = "";
if (this.lbInstance.Items.Count > 0)
{
bool bFlag = true;
foreach (ListItem item in this.lbInstance.Items)
{
if (item.Selected)
{
if (bFlag)
{
strInstance = item.Value;
bFlag = false;
}
else
{
strInstance += "," + item.Value;
}
}
}
}
foreach(ListItem item in this.lbCounter.Items)
{
if (item.Selected)
{
list[this.ddlCategory.SelectedValue + " " + item.Value + " " + strInstance] = new CounterInfo(this.ddlCategory.SelectedValue,item.Value,strInstance);
}
}
4、ZedGraph控件展现计数器值:
//这里不能使用ViewState
Dictionary<string, PointPairList> dic = Cache["datasource"] as Dictionary<string,PointPairList>;
if (dic == null)
{
dic = new Dictionary<string, PointPairList>();
}
ArrayList labels = new ArrayList();
foreach (CounterInfo ci in this.Counters.Values)
{
PerformanceCounter counter = null;
if (string.IsNullOrEmpty(ci.InstanceNames))
{
PointPairList list = null;
dic.TryGetValue(ci.CategoryName + " " + ci.CounterName, out list);
if (list == null)
{
list = new PointPairList();
}
counter = new PerformanceCounter(ci.CategoryName, ci.CounterName);
list.Add(list.Count + 1, counter.NextValue());
dic[ci.CategoryName + " " + ci.CounterName] = list;
labels.Add(ci.CategoryName + " " + ci.CounterName);
}
else
{
foreach (string ins in ci.InstanceNames.Split(','))
{
PointPairList list = null;
if (!dic.TryGetValue(ci.CategoryName + " " + ci.CounterName + " " + ins,out list))
{
list = new PointPairList();
}
counter = new PerformanceCounter(ci.CategoryName, ci.CounterName,ins);
list.Add(list.Count + 1, counter.NextValue());
dic[ci.CategoryName + " " + ci.CounterName + " " + ins] = list;
labels.Add(ci.CategoryName + " " + ci.CounterName + " " + ins);
}
}
}
//生成随机颜色
Random random = new Random(128);
for (int i = 0; i < labels.Count; i++)
{
pane[0].AddCurve(labels[i] as string, dic[labels[i] as string], Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)));
}
pane.AxisChange(g);
Cache["datasource"] = dic;
Dictionary<string, PointPairList> dic = Cache["datasource"] as Dictionary<string,PointPairList>;
if (dic == null)
{
dic = new Dictionary<string, PointPairList>();
}
ArrayList labels = new ArrayList();
foreach (CounterInfo ci in this.Counters.Values)
{
PerformanceCounter counter = null;
if (string.IsNullOrEmpty(ci.InstanceNames))
{
PointPairList list = null;
dic.TryGetValue(ci.CategoryName + " " + ci.CounterName, out list);
if (list == null)
{
list = new PointPairList();
}
counter = new PerformanceCounter(ci.CategoryName, ci.CounterName);
list.Add(list.Count + 1, counter.NextValue());
dic[ci.CategoryName + " " + ci.CounterName] = list;
labels.Add(ci.CategoryName + " " + ci.CounterName);
}
else
{
foreach (string ins in ci.InstanceNames.Split(','))
{
PointPairList list = null;
if (!dic.TryGetValue(ci.CategoryName + " " + ci.CounterName + " " + ins,out list))
{
list = new PointPairList();
}
counter = new PerformanceCounter(ci.CategoryName, ci.CounterName,ins);
list.Add(list.Count + 1, counter.NextValue());
dic[ci.CategoryName + " " + ci.CounterName + " " + ins] = list;
labels.Add(ci.CategoryName + " " + ci.CounterName + " " + ins);
}
}
}
//生成随机颜色
Random random = new Random(128);
for (int i = 0; i < labels.Count; i++)
{
pane[0].AddCurve(labels[i] as string, dic[labels[i] as string], Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)));
}
pane.AxisChange(g);
Cache["datasource"] = dic;
本来是做了个演示的在http://www.massany.com/perfmon.aspx,但是服务器那边出于安全考虑,运行不了,只有大家自己下载了运行了。
工程下载:http://www.massany.com/perfmon.zip