【问题标题】:performance Counter for disk read / write time磁盘读/写时间的性能计数器
【发布时间】:2014-04-26 18:58:15
【问题描述】:

在我的应用程序中,我正在收集有关系统性能的数据,我需要在哪里找到

  • % 可用空间
  • % 磁盘时间
  • % 磁盘读取时间
  • % 磁盘写入时间
  • % 空闲时间
  • % 使用率
  • % 使用峰值

使用下面的函数;

private void CollectnPopulatePerfCounters()
    {
        try
        {
            foreach (var pc in System.Diagnostics.PerformanceCounterCategory.GetCategories())
            {
                if (pc.CategoryName == "LogicalDisk" || pc.CategoryName == "Paging File" || pc.CategoryName == "ProcessorPerformance")
                {
                    try
                    {
                        foreach (var insta in pc.GetInstanceNames())
                        {
                            try
                            {
                                foreach (PerformanceCounter cntr in pc.GetCounters(insta))
                                {
                                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\amit.txt", true))
                                    {
                                        sw.WriteLine("--------------------------------------------------------------------");
                                        sw.WriteLine("Category Name : " + pc.CategoryName);
                                        sw.WriteLine("Counter Name : " + cntr.CounterName);
                                        sw.WriteLine("Explain Text : " + cntr.CounterHelp);
                                        sw.WriteLine("Instance Name: " + cntr.InstanceName);
                                        sw.WriteLine("Value : " + Convert.ToString(cntr.RawValue));  //TODO:
                                        sw.WriteLine("Counter Type : " + cntr.CounterType);
                                        sw.WriteLine("--------------------------------------------------------------------");
                                    }
                                }
                            }
                            catch (Exception) { }
                        }
                    }
                    catch (Exception) { }
                }
            }
        }
        catch (Exception) { }
    }

执行代码时会生成数据。在观察时我发现上述列表的值[即% 可用空间、% 磁盘时间等] 格式不正确。

在我的机器上

  • % 磁盘读取时间 = C 盘的 44553438000
  • % 使用峰值 = 48386 for \??\C:\pagefile.sys

实际上该值应该是百分比形式[即在 0 到 100 % 的范围内]

除了[我计算的可用空间百分比]之外,有什么方法可以得到所有这些的确切值。

或者

有谁知道如何计算所有标题的其余部分。

【问题讨论】:

    标签: c# performance performancecounter


    【解决方案1】:

    使用以下

    sw.WriteLine("Value : " + Convert.ToString(Math.Round(cntr.NextValue(),2)) + "%");
    

    更多信息请访问:

    Why the cpu performance counter kept reporting 0% cpu usage?

    一切顺利!

    别忘了投票:-D

    【讨论】:

    • 使用 (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\New1.txt", true)) { sw.WriteLine("% Disk Time"); var cpuload = new PerformanceCounter("LogicalDisk", "% Disk Time", "C:"); for (int i = 0; i
    • 如果你运行上面的代码,你会得到不同的值。为什么会这样?请评论
    • 答案在 MSDN 文档中。 msdn.microsoft.com/en-us/library/…
    • RawValue - 获取或设置此计数器的原始值或未计算值。 NextValue() - 获取一个计数器样本并为其返回计算值。
    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 2018-05-31
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多