尝试从性能监视器中提取 CPU 和网络使用信息

Trying To Pull CPU & Network Usage Information From Performance Monitor

我正在尝试从进程中获取 CPU 和网络使用信息。
在我的示例中,我将使用进程 chrome

这是我如何使用 IEnumerable<String>

调用方法
foreach (string p in GetProcessStatistics(new string[] { "chrome" }))
{
  Console.WriteLine(p);
}

这是方法。

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

        foreach (string process in processesTosearch)
        {
            foreach (Process p in processList)
            {
                if (p.ProcessName == process)
                {
                    StringBuilder sb = new StringBuilder();
                    PerformanceCounter CPUperformanceCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
                    double cpuData = CPUperformanceCounter.NextValue();
                    PerformanceCounter NETWORKperformanceCounter = new PerformanceCounter("Process", "IO Data Operations/Sec", p.ProcessName);
                    double networkData = NETWORKperformanceCounter.NextValue();

                    sb.AppendLine("ID: " + p.Id.ToString());
                    sb.AppendLine("NAME: " + p.ProcessName);
                    sb.AppendLine("CPU USAGE: " + cpuData);
                    sb.AppendLine("RAM USAGE: " + ConvertToReadableSize(p.PrivateMemorySize64));
                    sb.AppendLine("NETWORK USAGE: " + networkData);
                    yield return sb.ToString();
                }
            }
        }
    }

这是其中一个结果的输出

ID: 17624
NAME: chrome
CPU USAGE: 0
RAM USAGE: 23.2MB
NETWORK USAGE: 0

当我查看性能监视器时,cpu 和网络值不是 0,但是在控制台中,它们是。

我从一些研究中了解到值永远不会是完美的,但为什么它们在控制台应用程序中显示 0 而不是在性能监视器上?

每个计数器至少需要两次读数,至少间隔一秒才能获得可用读数。

根据需要重新排列,但您需要这样做:

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

    foreach (string process in processesTosearch)
    {
        foreach (Process p in processList)
        {
            if (p.ProcessName == process)
            {
                StringBuilder sb = new StringBuilder();
                PerformanceCounter CPUperformanceCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
                PerformanceCounter NETWORKperformanceCounter = new PerformanceCounter("Process", "IO Data Operations/Sec", p.ProcessName);

                // set a baseline
                CPUperformanceCounter.NextValue();
                NETWORKperformanceCounter.NextValue();

                Thread.Sleep(1000);

                double cpuData = CPUperformanceCounter.NextValue();
                double networkData = NETWORKperformanceCounter.NextValue();

                sb.AppendLine("ID: " + p.Id.ToString());
                sb.AppendLine("NAME: " + p.ProcessName);
                sb.AppendLine("CPU USAGE: " + cpuData);
                sb.AppendLine("RAM USAGE: " + ConvertToReadableSize(p.PrivateMemorySize64));
                sb.AppendLine("NETWORK USAGE: " + networkData);
                yield return sb.ToString();
            }
        }
    }
}

我喜欢 Jeff 的解决方案,但对我来说,我想要一个平均值。 CPU 利用率有几个问题,似乎应该有一个简单的包可以解决,但我没有看到。

首先当然是第一个请求的值为0是没有用的。既然您已经知道第一个响应是 0,为什么该函数不考虑它并 return 真正的 .NextValue()?

第二个问题是,在尝试决定您的应用可能有哪些资源可用时,瞬时读数可能会非常不准确,因为它可能会出现峰值或峰值之间。

我的解决方案是做一个循环循环的 for 循环,并为您提供过去几秒钟的平均值。您可以调整计数器使其更短或更长(只要它大于 2)。

public static float ProcessorUtilization;

public static float GetAverageCPU()
{
    PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
    for (int i = 0; i < 11; ++i)
    {
        ProcessorUtilization += (cpuCounter.NextValue() / Environment.ProcessorCount);
    }
    // Remember the first value is 0, so we don't want to average that in.
    Console.Writeline(ProcessorUtilization / 10); 
    return ProcessorUtilization / 10;
}