性能监视器 - IO数据操作/秒

时间:2018-06-13 08:15:46

标签: c# performancecounter

我正在尝试从特定进程的性能监视器IO Data Operations/sec中获取值并将其转换为可读值。

private static IEnumerable<String> GetProcessStatistics(String[] processList)
{
    Process[] process_List = Process.GetProcesses();

    foreach (string process in processList)
    {
        foreach (Process p in process_List)
        {
            if (process == p.ProcessName)
            {
                StringBuilder sb = new StringBuilder();

                PerformanceCounter NETWORKperformanceCounter = new PerformanceCounter("Process", "IO Data Operations/sec", p.ProcessName);

                NETWORKperformanceCounter.NextValue();
                System.Threading.Thread.Sleep(1000);

                double processNetworkdata = NETWORKperformanceCounter.RawValue;

                sb.AppendLine("ID: " + p.Id.ToString());
                sb.AppendLine("NAME: " + p.ProcessName);
                sb.AppendLine(String.Format("NETWORK USAGE: {0}", processNetworkdata));
                sb.AppendLine(String.Format("NETWORK USAGE: {0}", ConvertToReadableBandwidthSize(processNetworkdata)));

                yield return sb.ToString();
            }
        }
    }
}

返回的第一个值是4995043
返回的第二个值是4.76mbps
任务管理器中显示的值为48.5mbps

我理解价值永远不会是perfect我认为自从使用RawValue以来我认为它们会非常接近。
这是它如何转换RawValue

private static String ConvertToReadableBandwidthSize(Object size)
{
    string[] ordinals = new string[] { "","K","M", "G", "T", "P", "E" };
    long bandwidth = Convert.ToInt64(size);
    decimal rate = (decimal)bandwidth;
    int ordinal = 0;

    while (rate > 1024)
    {
        rate /= 1024;
        ordinal++;
    }

    return String.Format("{0} {1}bps", Math.Round(rate,2, MidpointRounding.AwayFromZero), ordinals[ordinal]);
 }
  

更新   我发现有更大的数字,任何应该在mbps范围内的任何内容,例如1.2mpbs,我认为它不正确,但是数字较小或mbps范围内的任何内容似乎都没问题

0 个答案:

没有答案