网络利用率问题 [英] Network utilization problem
问题描述
在计算网络利用率时,我遇到了一个奇怪的问题.代码是
Hi,
I have got a strange problem when calculating network utilization. The code is
bandwidthCounter.CategoryName = "Network Interface";
bandwidthCounter.CounterName = "Current Bandwidth";
bandwidthCounter.InstanceName = networkCard;
bandwidth = bandwidthCounter.NextValue(); // Network card name
totalDataCounter.CategoryName = "Network Interface";
totalDataCounter.CounterName = "Bytes Total/sec";
totalDataCounter.InstanceName = networkCard; // Network card name
receiveSum = totalDataCounter.NextValue();
Console.WriteLine("Bandwidth is " + bandwidth.ToString());
Console.WriteLine("Total bytes is " + receiveSum.ToString());
double utilization = ((8 * receiveSum) / (bandwidth)) * 100;
作为控制台应用程序运行时,此代码可以正常工作.我已将控制台应用程序更改为服务,并开始将使用情况输出到调试窗口.奇怪的是,它完美地用作控制台应用程序,将利用率与任务管理器利用率相匹配.但是,当我运行该服务时,它显示出非常不同的值,并且与任务管理器值不匹配.
:((
问候
苏里亚
更多代码在这里.这是在服务应用程序中.
This code is working perfectly fine when running as a console application. I have changed the console application to a service and started outputing the utilization to a debug window. Strange thing is it works perfectly as a console application matching the utilization with task manager utilization. But when I run the service it shows very different values and does not match with task manager values.
:((
Regards
Surya
More code is here. This is in service application.
namespace NetworkUtilService
{
public partial class NetworkUtil : ServiceBase
{
System.Timers.Timer aTimer;
public NetworkUtil()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
public void GetNetWorkUtil()
{
float sendSum = 0;
float receiveSum = 0;
float bandwidth = 0;
string networkCard = "Broadcom NetXtreme 57xx Gigabit Controller";
PerformanceCounter bandwidthCounter = new PerformanceCounter();
bandwidthCounter.CategoryName = "Network Interface";
bandwidthCounter.CounterName = "Current Bandwidth";
bandwidthCounter.InstanceName = networkCard;
bandwidth = bandwidthCounter.NextValue();
PerformanceCounter totalDataCounter = new PerformanceCounter();
totalDataCounter.CategoryName = "Network Interface";
totalDataCounter.CounterName = "Bytes Total/sec";
totalDataCounter.InstanceName = networkCard;
receiveSum = totalDataCounter.NextValue();
Debug.OutputDebugString("Total bytes: " + receiveSum.ToString() + " at Time " + DateTime.Now.Second.ToString());
double utilization = ((8 * receiveSum) / (bandwidth)) * 100;
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
GetNetWorkUtil();
}
}
控制台应用程序代码为
And the console application code is
namespace NetworkUtilization
{
class Calculate
{
PerformanceCounter bandwidthCounter = null;
PerformanceCounter totalDataCounter = null;
string networkCard = null;
public Calculate()
{
bandwidthCounter = new PerformanceCounter();
totalDataCounter = new PerformanceCounter();
networkCard = "Broadcom NetXtreme 57xx Gigabit Controller";
SetTimer();
Console.ReadLine();
}
public void SetTimer()
{
Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Enabled = true;
myTimer.Interval = 1000;
}
public void myTimer_Elapsed(object sender, ElapsedEventArgs e)
{
float receiveSum = 0;
float bandwidth = 0;
bandwidthCounter.CategoryName = "Network Interface";
bandwidthCounter.CounterName = "Current Bandwidth";
bandwidthCounter.InstanceName = networkCard;
bandwidth = bandwidthCounter.NextValue();
totalDataCounter.CategoryName = "Network Interface";
totalDataCounter.CounterName = "Bytes Total/sec";
totalDataCounter.InstanceName = networkCard;
receiveSum = totalDataCounter.NextValue();
Console.WriteLine("Bandwidth is " + bandwidth.ToString());
Console.WriteLine("Total bytes is " + receiveSum.ToString());
double utilization = ((8 * receiveSum) / (bandwidth)) * 100;
Console.WriteLine("Utilization is " + utilization.ToString() + " " + DateTime.Now.ToString());
}
}
谢谢
Surya
Thanks
Surya
推荐答案
我不确定我的解释,但我认为这是正确的原因.
从服务中检索数据需要花费一些时间.
例如,您在11:00:05的时间获取了网络数据,但是服务在11:00:06将数据返回给客户端,因此存在不匹配的情况.
您可以尝试将相同的代码在线程中运行,并以一定的睡眠时间作为间隔运行.
I am not sure about my explanation but I guess this is correct reason.
It takes some amount time to retrieve data from the service.
For example you took data of network at time 11:00:05 time but the service returned the data to the client on 11:00:06, hence there is a mismatch.
You can try the same code to run in a thread with some amount of sleep which will work as interval.
Debojyoti,
这不仅是不匹配,而且实际上还在急剧下降.当控制台应用程序显示利用率为9%时,服务应用程序将显示为1%.我如何无法理解?
问候
苏里亚
Hi Debojyoti,
It is not only mismatch, actually it is dropping drastically. When the console aplication shows 9% of utilization, service application shows 1%. How I am unable to understand?
Regards
Surya
这篇关于网络利用率问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!