0

我正在编写这个类来监控通过所有网络接口的带宽。虽然这整个事情非常好,但我想尝试在查询期间减少 CPU 使用率。原因是因为我每隔几秒钟运行一次,每次都会为进程增加大约 7-8% 的 CPU 使用率。有什么办法可以减少 CPU 周期的数量?

PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
        String[] instancename = category.GetInstanceNames();

        foreach (string name in instancename)
        {
            String networkCard = name;

            const int numberOfIterations = 10;

            PerformanceCounter bandwidthCounter = new PerformanceCounter("Network Interface", "Current Bandwidth", networkCard);

            float bandwidth = bandwidthCounter.NextValue();

            PerformanceCounter dataSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkCard);

            PerformanceCounter dataReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkCard);

            float sendSum = 0;
            float receiveSum = 0;

            for (int index = 0; index < numberOfIterations; index++)
            {
                sendSum += dataSentCounter.NextValue();
                receiveSum += dataReceivedCounter.NextValue();
            }
            float dataSent = sendSum;
            float dataReceived = receiveSum;


            double dout = (((8 * (dataSent)) / (bandwidth * numberOfIterations) * 100) * 1024) / 100;
            double din = (((8 * (dataReceived)) / (bandwidth * numberOfIterations) * 100) * 1024) / 100;

            dout = Math.Round(dout, 2);
            din = Math.Round(din, 2);

            string doutround = Convert.ToString(dout);
            string dinround = Convert.ToString(din);



            String output = dinround + "/" + doutround;


                GlobalsWMI.NetIOs.Add(output);



        }
        String tooutput = String.Join("AND", GlobalsWMI.NetIOs);
        GlobalsWMI.NetIOs.Clear();
        return tooutput;
4

2 回答 2

0

您可以在某些时候使用 System.Threading.Thread.Sleep(x) 来减少 CPU 使用率 - 您的检查会持续更长时间,但平均 CPU 使用率会更低。

于 2013-06-05T13:18:51.293 回答
0

好的,我尝试了各种方法,我认为这个版本要快得多:

private string GetNetworkUsage(System.Net.NetworkInformation.NetworkInterface[] nis)
{

foreach (NetworkInterface ni in nis)
            {
                float bandwidth = ni.Speed;
                const int numberOfIterations = 10;

                float sendSum = 0;
                float receiveSum = 0;

                float lastRec = ni.GetIPv4Statistics().BytesReceived;
                float lastSent = ni.GetIPv4Statistics().BytesSent;

                for (int index = 0; index < numberOfIterations; index++)
                {
                    System.Threading.Thread.Sleep(10);
                    float br = ni.GetIPv4Statistics().BytesReceived;
                    float bs = ni.GetIPv4Statistics().BytesSent;
                    receiveSum += br - lastRec;
                    sendSum += bs - lastSent;

                    lastRec = br;
                    lastSent = bs;

                }

                float dataSent = sendSum;
                float dataReceived = receiveSum;

                double dout = (((8 * (dataSent)) / (bandwidth) * 100) * 1024) / 100;
                double din = (((8 * (dataReceived)) / (bandwidth) * 100) * 1024) / 100;

                dout = Math.Round(dout, 2);
                din = Math.Round(din, 2);

                string doutround = Convert.ToString(dout);
                string dinround = Convert.ToString(din);

                String output = dinround + "/" + doutround;


                GlobalsWMI.NetIOs.Add(output);
            }

            String tooutput = String.Join("AND", GlobalsWMI.NetIOs);
            GlobalsWMI.NetIOs.Clear();
            return tooutput;
}

像这样称呼它:

    System.Net.NetworkInformation.NetworkInterface[] nis = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

    // this is you for loop add you sleep or however you do it here
    for (int i = 0; i < int.MaxValue; i++)
    {
        Console.WriteLine(test(nis));
    }

在我的系统上尝试过这个,它使用更少的 CPU 并且速度更快,但我会仔细检查这些数字是否符合您的预期。

于 2013-06-05T14:18:35.693 回答