2011-12-28 68 views
8

我正在尝试为在2个不同服务器上运行的程序构建负载均衡器。如何知道给定时间使用的网络带宽?

到目前为止,我的负载均衡器只检查每个服务器程序中使用PerformanceCounter实例的每个服务器的CPU使用情况。

我还想检查每个服务器的带宽使用情况,我该如何检查?

(这大概也做过使用的PerformanceCounter,但我与它的使用不熟悉)

+2

看看这个问题: http://stackoverflow.com/questions/442409/c-bandwidth – Moonlight 2011-12-28 15:05:11

回答

17

由于月光,我发现这个http://www.keyvan.ms/how-to-calculate-network-utilization-in-net

public double getNetworkUtilization(string networkCard){ 

      const int numberOfIterations = 10; 

      PerformanceCounter bandwidthCounter = new PerformanceCounter("Network Interface", "Current Bandwidth", networkCard); 
      float bandwidth = bandwidthCounter.NextValue();//valor fixo 10Mb/100Mn/ 

      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 utilization = (8 * (dataSent + dataReceived))/(bandwidth * numberOfIterations) * 100; 
      return utilization; 
     } 

找到可用的网络卡验证码帮我:

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

      foreach (string name in instancename) 
      { 
       Console.WriteLine(name); 
      } 
     }