如何在网络带宽脚本中降低CPU使用率
本文关键字:CPU 使用率 脚本 网络带宽 | 更新日期: 2023-09-27 18:17:07
我编写这个类是为了监视通过所有网络接口的带宽。虽然这一切都工作得很好,但我想尝试减少查询期间的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;
好的,我已经尝试了各种各样的东西,我认为这个版本快得多:
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更少,速度更快,但我会仔细检查数字是否如您所期望的。
您可以在某些时候使用System.Threading.Thread.Sleep(x)来减少CPU使用-您的检查将持续更长时间,但平均CPU使用更少