如何减少网络带宽脚本中的CPU使用率

时间:2013-06-05 13:13:35

标签: c# networking interface system cpu

我正在编写此类来监视通过所有网络接口的带宽。虽然整个过程非常有效,但我想尝试在查询期间减少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;

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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并且更快但我会仔细检查数字是否符合您的预期。